UNPKG

@vtaits/react-hook-form-schema

Version:

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

233 lines (228 loc) 5.83 kB
import { wrapOnChange } from "./chunk-7ZPD7GZF.js"; import { renderError } from "./chunk-XUSORK34.js"; // src/fields/select/multiSelect.tsx import { DEFAULT_LABEL_KEY, DEFAULT_VALUE_KEY, multiSelect as multiSelectBase } from "@vtaits/form-schema/fields/select"; import { useUI } from "@vtaits/react-form-schema-base-ui"; import { useMemo as useMemo2 } from "react"; import { Controller, get } from "react-hook-form"; // src/fields/select/useOptionsCache.ts import { useMemo, useRef } from "react"; function useOptionsCache(options, getOptionValue) { const optionsCache = useMemo(() => { const res = {}; for (const option of options) { res[getOptionValue(option)] = option; } return res; }, [getOptionValue, options]); const optionsCacheRef = useRef(optionsCache); optionsCacheRef.current = optionsCache; return optionsCacheRef; } // src/fields/select/multiSelect.tsx import { jsx } from "react/jsx-runtime"; function MultiSelectComponent({ renderParams: { fieldPath, fieldSchema: { disabled, hint, autoFocus, label, getOptionLabel: getOptionLabelParam, getOptionValue: getOptionValueParam, onChange = void 0, options, renderOption, labelKey = DEFAULT_LABEL_KEY, placeholder, valueKey = DEFAULT_VALUE_KEY, required } }, formResult, formResult: { control, formState: { errors } } }) { const { renderMultiSelect, renderWrapper } = useUI(); const error = renderError(get(errors, fieldPath)); const wrapperParams = { error, hint, label, name: fieldPath, required }; const getOptionLabel = useMemo2(() => { if (getOptionLabelParam) { return getOptionLabelParam; } return (option) => option[labelKey]; }, [getOptionLabelParam, labelKey]); const getOptionValue = useMemo2(() => { 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 renderMultiSelect({ disabled, value: Array.isArray(field.value) ? field.value : [], name: fieldPath, getOptionLabel, getOptionValue, handleClear: () => { wrappedOnChange([]); }, autoFocus, onChange: wrappedOnChange, options, optionsCacheRef, placeholder, renderOption, wrapper: wrapperParams }); } } ) }); } var multiSelect = { ...multiSelectBase, render: (renderParams, formResult) => /* @__PURE__ */ jsx(MultiSelectComponent, { renderParams, formResult }) }; // src/fields/select/select.tsx import { DEFAULT_LABEL_KEY as DEFAULT_LABEL_KEY2, DEFAULT_VALUE_KEY as DEFAULT_VALUE_KEY2, select as selectBase } from "@vtaits/form-schema/fields/select"; import { useUI as useUI2 } from "@vtaits/react-form-schema-base-ui"; import { useMemo as useMemo3 } from "react"; import { Controller as Controller2, get as get2 } from "react-hook-form"; import { jsx as jsx2 } from "react/jsx-runtime"; function SelectComponent({ renderParams: { fieldPath, fieldSchema: { disabled, label, hint, autoFocus, required, getOptionLabel: getOptionLabelParam, getOptionValue: getOptionValueParam, onChange = void 0, options, renderOption, labelKey = DEFAULT_LABEL_KEY2, placeholder, valueKey = DEFAULT_VALUE_KEY2 } }, formResult, formResult: { control, formState: { errors } } }) { const { renderSelect, renderWrapper } = useUI2(); const error = renderError(get2(errors, fieldPath)); const wrapperParams = { error, hint, label, name: fieldPath, required }; const getOptionLabel = useMemo3(() => { if (getOptionLabelParam) { return getOptionLabelParam; } return (option) => option[labelKey]; }, [getOptionLabelParam, labelKey]); const getOptionValue = useMemo3(() => { if (getOptionValueParam) { return getOptionValueParam; } return (option) => option[valueKey]; }, [getOptionValueParam, valueKey]); const optionsCacheRef = useOptionsCache(options, getOptionValue); return renderWrapper({ ...wrapperParams, children: /* @__PURE__ */ jsx2( Controller2, { control, name: fieldPath, render: ({ field }) => { const wrappedOnChange = wrapOnChange( field.onChange, onChange, formResult, field.value ); return renderSelect({ clearable: !required, disabled, value: field.value, name: fieldPath, getOptionLabel, getOptionValue, handleClear: () => { wrappedOnChange(null); }, autoFocus, onChange: wrappedOnChange, options, optionsCacheRef, placeholder, renderOption, wrapper: wrapperParams }); } } ) }); } var select = { ...selectBase, render: (renderParams, formResult) => /* @__PURE__ */ jsx2(SelectComponent, { renderParams, formResult }) }; export { useOptionsCache, multiSelect, select }; //# sourceMappingURL=chunk-W54VP7QK.js.map