UNPKG

@wordpress/components

Version:
52 lines (49 loc) 1.68 kB
/** * WordPress dependencies */ import { forwardRef, useRef, useEffect } from '@wordpress/element'; import { useMergeRefs } from '@wordpress/compose'; /** * Internal dependencies */ import { ControlWithError } from '../control-with-error'; import ToggleControl from '../../toggle-control'; import { jsx as _jsx } from "react/jsx-runtime"; // TODO: Should we customize the default `missingValue` message? It says to "check this box". const UnforwardedValidatedToggleControl = ({ required, customValidator, onChange, markWhenOptional, ...restProps }, forwardedRef) => { const validityTargetRef = useRef(null); const mergedRefs = useMergeRefs([forwardedRef, validityTargetRef]); const valueRef = useRef(restProps.checked); // TODO: Upstream limitation - The `required` attribute is not passed down to the input, // so we need to set it manually. useEffect(() => { if (validityTargetRef.current) { validityTargetRef.current.required = required !== null && required !== void 0 ? required : false; } }, [required]); return /*#__PURE__*/_jsx(ControlWithError, { required: required, markWhenOptional: markWhenOptional, customValidator: () => { return customValidator?.(valueRef.current); }, getValidityTarget: () => validityTargetRef.current, children: /*#__PURE__*/_jsx(ToggleControl, { __nextHasNoMarginBottom: true, ref: mergedRefs, onChange: value => { valueRef.current = value; onChange?.(value); }, ...restProps }) }); }; export const ValidatedToggleControl = forwardRef(UnforwardedValidatedToggleControl); //# sourceMappingURL=toggle-control.js.map