UNPKG

@payfit/unity-components

Version:

51 lines (50 loc) 1.86 kB
import e from "../../hooks/use-id.js"; import { useUnityFormProvider as t } from "../form/Form.context.js"; import { createSchemaAdapter as n } from "../../utils/createSchemaAdapter.js"; import { FormFieldProvider as r } from "./FormField.context.js"; import { FormHelperText as i } from "./parts/FormHelperText.js"; import { RawFormContextualLink as a } from "./parts/RawFormContextualLink.js"; import { isFieldRequired as o } from "./utils/isFieldRequired.js"; import s, { forwardRef as c, useMemo as l } from "react"; import { uyTv as u } from "@payfit/unity-themes"; import { jsx as d } from "react/jsx-runtime"; import { useController as f } from "react-hook-form"; //#region src/components/form-field/FormField.tsx var p = u({ slots: { base: "uy:flex uy:flex-col uy:gap-100" } }), m = c(({ children: c, control: u, name: m, isLoading: h, isReadOnly: g, className: _, isRequired: v, isDisabled: y }, b) => { let x = e(), { schema: S } = t(), { formState: C } = f({ name: m, control: u }), w = l(() => o(n(S), m), [S, m]), { hasHelperText: T, hasContextualLink: E } = l(() => { let e = !1, t = !1, n = (r) => { s.Children.forEach(r, (r) => { if (!s.isValidElement(r)) return; let o = r; o.type === i ? e = !0 : o.type === a && (t = !0), o.props && typeof o.props == "object" && "children" in o.props && n(o.props.children); }); }; return n(c), { hasHelperText: e, hasContextualLink: t }; }, [c]), D = { name: m, isLoading: h, isReadOnly: g, hasHelperText: T, hasContextualLink: E, id: x, isRequired: v ?? w, isDisabled: y ?? (C.isSubmitting || C.disabled) }, { base: O } = p(); return /* @__PURE__ */ d("div", { ref: b, className: O({ className: _ }), children: /* @__PURE__ */ d(r, { ...D, children: c }) }); }); m.displayName = "FormField"; //#endregion export { m as FormField };