UNPKG

@vtaits/react-hook-form-schema

Version:

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

92 lines (90 loc) 2.04 kB
import { wrapOnChange } from "./chunk-7ZPD7GZF.js"; import { renderError } from "./chunk-XUSORK34.js"; // src/fields/tags/tags.tsx import { tags as tagsBase } from "@vtaits/form-schema/fields/tags"; import { useUI } from "@vtaits/react-form-schema-base-ui"; import { Controller, get } from "react-hook-form"; import { jsx } from "react/jsx-runtime"; function prepareValue(rawValue) { const arrayValue = rawValue ? Array.isArray(rawValue) ? rawValue : [rawValue] : []; return arrayValue.map((rawValue2) => { if (typeof rawValue2 === "number" || typeof rawValue2 === "boolean") { return String(rawValue2); } if (typeof rawValue2 === "string") { return rawValue2; } return ""; }); } function TagsComponent({ renderParams: { fieldPath, fieldSchema: { disabled, options, hint, autoFocus, label, onChange = void 0, required } }, formResult, formResult: { control, formState: { errors } } }) { const { renderTags, renderWrapper } = useUI(); const error = renderError(get(errors, fieldPath)); const wrapperParams = { error, hint, label, name: fieldPath, required }; return renderWrapper({ ...wrapperParams, children: /* @__PURE__ */ jsx( Controller, { name: fieldPath, control, render: ({ field }) => { const preparedValue = prepareValue(field.value); return renderTags({ disabled, autoFocus, name: fieldPath, value: preparedValue, onChange: wrapOnChange( field.onChange, onChange, formResult, preparedValue ), options, wrapper: wrapperParams }); } } ) }); } var tags = { ...tagsBase, render: (renderParams, formResult) => /* @__PURE__ */ jsx(TagsComponent, { renderParams, formResult }) }; export { tags }; //# sourceMappingURL=chunk-LSQMRSTF.js.map