UNPKG

@vtaits/react-hook-form-schema

Version:

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

94 lines (92 loc) 2.13 kB
import { wrapOnChange } from "./chunk-7ZPD7GZF.js"; import { renderError } from "./chunk-XUSORK34.js"; // src/fields/file/file.tsx import { file as fileBase } from "@vtaits/form-schema/fields/file"; import { useUI } from "@vtaits/react-form-schema-base-ui"; import { Controller, get } from "react-hook-form"; import { jsx } from "react/jsx-runtime"; function FileComponent({ renderParams: { fieldPath, fieldSchema: { disabled, accept, text = "Select file", hint, label, onChange = void 0, required } }, formResult, formResult: { control, formState: { errors } } }) { const { renderFileInput, 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 wrappedOnChange = wrapOnChange( field.onChange, onChange, formResult, field.value ); const currentValue = field.value || { file: null }; return renderFileInput({ accept, children: text, disabled, onSelectFile: (nextFile) => { wrappedOnChange( nextFile ? { ...currentValue, file: nextFile, name: nextFile instanceof File ? nextFile.name : void 0 } : { file: null, hasPreviousFile: false } ); }, name: fieldPath, selectedFile: currentValue?.name, wrapper: wrapperParams }); } } ) }); } var file = { ...fileBase, render: (renderParams, formResult) => /* @__PURE__ */ jsx(FileComponent, { renderParams, formResult }) }; export { file }; //# sourceMappingURL=chunk-TDH2XHLI.js.map