@wordpress/components
Version:
UI components for WordPress.
43 lines (42 loc) • 1.33 kB
JavaScript
/**
* WordPress dependencies
*/
import { useMergeRefs } from '@wordpress/compose';
import { forwardRef, useRef } from '@wordpress/element';
/**
* Internal dependencies
*/
import { ControlWithError } from '../control-with-error';
import RadioControl from '../../radio-control';
import { jsx as _jsx } from "react/jsx-runtime";
const UnforwardedValidatedRadioControl = ({
required,
customValidator,
onChange,
markWhenOptional,
...restProps
}, forwardedRef) => {
const validityTargetRef = useRef(null);
const mergedRefs = useMergeRefs([forwardedRef, validityTargetRef]);
const valueRef = useRef(restProps.selected);
return /*#__PURE__*/_jsx(ControlWithError, {
required: required,
markWhenOptional: markWhenOptional
// TODO: Upstream limitation - RadioControl does not accept a ref.
,
ref: mergedRefs,
customValidator: () => {
return customValidator?.(valueRef.current);
},
getValidityTarget: () => validityTargetRef.current?.querySelector('input[type="radio"]'),
children: /*#__PURE__*/_jsx(RadioControl, {
onChange: value => {
valueRef.current = value;
onChange?.(value);
},
...restProps
})
});
};
export const ValidatedRadioControl = forwardRef(UnforwardedValidatedRadioControl);
//# sourceMappingURL=radio-control.js.map