UNPKG

@vtaits/react-hook-form-schema

Version:

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

97 lines (95 loc) 2.37 kB
import { wrapOnChange } from "./chunk-7ZPD7GZF.js"; import { renderError } from "./chunk-XUSORK34.js"; // src/fields/datetime/datetime.tsx import { parseValueAndValidate, serializeDate } from "@vtaits/form-schema/fields/date-base"; import { DEFAULT_CLIENT_DATE_FORMAT, DEFAULT_DISPLAY_DATE_FORMAT, datetime as datetimeBase } from "@vtaits/form-schema/fields/datetime"; import { useUI } from "@vtaits/react-form-schema-base-ui"; import { Controller, get } from "react-hook-form"; import { jsx } from "react/jsx-runtime"; function DateTimeComponent({ renderParams: { fieldPath, fieldSchema: { clientDateFormat: clientDateFormatParam, disabled, displayDateFormat: displayDateFormatParam, autoFocus, label, hint, inputProps, onChange = void 0, required } }, formResult, formResult: { control, formState: { errors } } }) { const { renderDateTimePicker, renderWrapper } = useUI(); const error = renderError(get(errors, fieldPath)); const wrapperParams = { error, hint, label, name: fieldPath, required }; const clientDateFormat = clientDateFormatParam || DEFAULT_CLIENT_DATE_FORMAT; const displayDateFormat = displayDateFormatParam || DEFAULT_DISPLAY_DATE_FORMAT; return renderWrapper({ ...wrapperParams, children: /* @__PURE__ */ jsx( Controller, { name: fieldPath, control, render: ({ field }) => { const wrappedOnChange = wrapOnChange( field.onChange, onChange, formResult, field.value ); return renderDateTimePicker({ disabled, displayDateFormat, name: fieldPath, inputProps: inputProps || {}, autoFocus, onChange: (nextValue) => { wrappedOnChange( nextValue ? serializeDate(nextValue, clientDateFormat) : null ); }, value: parseValueAndValidate(field.value, clientDateFormat), wrapper: wrapperParams }); } } ) }); } var datetime = { ...datetimeBase, render: (renderParams, formResult) => /* @__PURE__ */ jsx(DateTimeComponent, { renderParams, formResult }) }; export { datetime }; //# sourceMappingURL=chunk-ZPOJASMW.js.map