UNPKG

@wordpress/components

Version:
63 lines (62 loc) 2.08 kB
/** * WordPress dependencies */ import { forwardRef, useRef } from '@wordpress/element'; /** * Internal dependencies */ import { ControlWithError } from '../control-with-error'; import CustomSelectControl from '../../custom-select-control'; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; const UnforwardedValidatedCustomSelectControl = ({ required, customValidator, onChange, markWhenOptional, ...restProps }, forwardedRef) => { const validityTargetRef = useRef(null); const valueRef = useRef(restProps.value); return /*#__PURE__*/_jsxs("div", { className: "components-validated-control__wrapper-with-error-delegate", ref: forwardedRef, children: [/*#__PURE__*/_jsx(ControlWithError, { required: required, markWhenOptional: markWhenOptional, customValidator: () => { return customValidator?.(valueRef.current); }, getValidityTarget: () => validityTargetRef.current, children: /*#__PURE__*/_jsx(CustomSelectControl // TODO: Upstream limitation - Required isn't passed down correctly, // so it needs to be set on a delegate element. , { __next40pxDefaultSize: true, onChange: value => { valueRef.current = value.selectedItem; onChange?.(value); }, ...restProps }) }), /*#__PURE__*/_jsxs("select", { className: "components-validated-control__error-delegate", ref: validityTargetRef, required: required, tabIndex: -1, value: restProps.value?.key ? 'hasvalue' : '', onChange: () => {}, onFocus: e => { e.target.previousElementSibling?.querySelector('[role="combobox"]')?.focus(); }, children: [/*#__PURE__*/_jsx("option", { value: "", children: "No selection" }), /*#__PURE__*/_jsx("option", { value: "hasvalue", children: "Has selection" })] })] }); }; export const ValidatedCustomSelectControl = forwardRef(UnforwardedValidatedCustomSelectControl); //# sourceMappingURL=custom-select-control.js.map