@payfit/unity-components
Version:
48 lines (47 loc) • 1.38 kB
JavaScript
import { createContext as e, useContext as t } from "react";
import { jsx as n } from "react/jsx-runtime";
import { useFormState as r } from "react-hook-form";
//#region src/components/form-field/FormField.context.tsx
var i = e({
id: "",
name: ""
});
function a({ children: e, id: t, name: r, isRequired: a, isLoading: o, isReadOnly: s, isDisabled: c, hasHelperText: l, hasContextualLink: u }) {
return /* @__PURE__ */ n(i.Provider, {
value: {
id: t,
name: r,
isRequired: a,
isLoading: o,
isReadOnly: s,
isDisabled: c,
hasHelperText: l,
hasContextualLink: u
},
children: e
});
}
function o() {
let e = t(i), n = r({ name: e.name }), { id: a, isRequired: o, isLoading: s, isReadOnly: c, isDisabled: l, hasHelperText: u, hasContextualLink: d } = e;
if (!e) throw Error("useFormField should be used within <FormField> component");
return {
id: a,
name: e.name,
hasHelperText: u,
hasContextualLink: d,
formLabelId: `${a}-form-field-label`,
formItemId: `${a}-form-item`,
formDescriptionId: u ? `${a}-form-field-description` : void 0,
formMessageId: `${a}-form-field-feedback-text`,
formContextualLinkId: d ? `${a}-form-field-contextual-link` : void 0,
fieldState: {
isRequired: o,
isLoading: s,
isReadOnly: c,
isDisabled: l
},
formState: n
};
}
//#endregion
export { a as FormFieldProvider, o as useFormField };