@wordpress/components
Version:
UI components for WordPress.
44 lines (43 loc) • 1.42 kB
JavaScript
/**
* WordPress dependencies
*/
import { useMergeRefs } from '@wordpress/compose';
import { forwardRef, useRef } from '@wordpress/element';
/**
* Internal dependencies
*/
import { ControlWithError } from '../control-with-error';
import CheckboxControl from '../../checkbox-control';
import { jsx as _jsx } from "react/jsx-runtime";
const UnforwardedValidatedCheckboxControl = ({
required,
customValidator,
onChange,
markWhenOptional,
...restProps
}, forwardedRef) => {
const validityTargetRef = useRef(null);
const mergedRefs = useMergeRefs([forwardedRef, validityTargetRef]);
const valueRef = useRef(restProps.checked);
return /*#__PURE__*/_jsx(ControlWithError, {
required: required,
markWhenOptional: markWhenOptional,
ref: mergedRefs,
customValidator: () => {
return customValidator?.(valueRef.current);
},
getValidityTarget: () => validityTargetRef.current?.querySelector('input[type="checkbox"]'),
children: /*#__PURE__*/_jsx(CheckboxControl, {
__nextHasNoMarginBottom: true,
onChange: value => {
valueRef.current = value;
onChange?.(value);
}
// TODO: Upstream limitation - CheckboxControl doesn't support uncontrolled mode, visually.
,
...restProps
})
});
};
export const ValidatedCheckboxControl = forwardRef(UnforwardedValidatedCheckboxControl);
//# sourceMappingURL=checkbox-control.js.map