UNPKG

@vtaits/react-hook-form-schema

Version:

Integration of react-hook-form and @vtaits/form-schema

112 lines (110 loc) 2.66 kB
import { useOptionsCache } from "./chunk-W54VP7QK.js"; import { wrapOnChange } from "./chunk-7ZPD7GZF.js"; import { renderError } from "./chunk-XUSORK34.js"; // src/fields/radioGroup/radioGroup.tsx import { DEFAULT_LABEL_KEY, DEFAULT_VALUE_KEY, select as selectBase } from "@vtaits/form-schema/fields/select"; import { useUI } from "@vtaits/react-form-schema-base-ui"; import { useMemo } from "react"; import { Controller, get } from "react-hook-form"; import { jsx } from "react/jsx-runtime"; function RadioGroupComponent({ renderParams: { fieldPath, fieldSchema: { disabled, hint, autoFocus, label, required, getOptionLabel: getOptionLabelParam, getOptionValue: getOptionValueParam, onChange = void 0, options, labelKey = DEFAULT_LABEL_KEY, valueKey = DEFAULT_VALUE_KEY } }, formResult, formResult: { control, formState: { errors } } }) { const { renderRadioGroup, renderWrapper } = useUI(); const error = renderError(get(errors, fieldPath)); const wrapperParams = { error, hint, label, name: fieldPath, required }; const getOptionLabel = useMemo(() => { if (getOptionLabelParam) { return getOptionLabelParam; } return (option) => option[labelKey]; }, [getOptionLabelParam, labelKey]); const getOptionValue = useMemo(() => { if (getOptionValueParam) { return getOptionValueParam; } return (option) => option[valueKey]; }, [getOptionValueParam, valueKey]); const optionsCacheRef = useOptionsCache(options, getOptionValue); return renderWrapper({ ...wrapperParams, children: /* @__PURE__ */ jsx( Controller, { control, name: fieldPath, render: ({ field }) => { const wrappedOnChange = wrapOnChange( field.onChange, onChange, formResult, field.value ); return renderRadioGroup({ clearable: !required, disabled, value: field.value, name: fieldPath, getOptionLabel, getOptionValue, handleClear: () => { wrappedOnChange(null); }, autoFocus, onChange: wrappedOnChange, options, optionsCacheRef, wrapper: wrapperParams }); } } ) }); } var radioGroup = { ...selectBase, render: (renderParams, formResult) => /* @__PURE__ */ jsx(RadioGroupComponent, { renderParams, formResult }) }; export { radioGroup }; //# sourceMappingURL=chunk-SHJ4YCQW.js.map