@paragrav/rhf-utils 0.0.117 → 0.0.119

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -16,7 +16,7 @@ To configure, create a file `config.tsx` with desired configuration settings. Th
16
16
  export const rhfUtilsClientConfig: RhfUtilsClientConfig = {
17
17
  // defaults -- overridable per form instance
18
18
  defaults: {
19
- // globally relevant subset of RHF's `UseFormProps` options
19
+ // globally-relevant subset of RHF's `UseFormProps` options
20
20
  rhf: {
21
21
  mode: 'onBlur',
22
22
  },
@@ -26,54 +26,62 @@ export const rhfUtilsClientConfig: RhfUtilsClientConfig = {
26
26
  // enable dev tool on dev env
27
27
  devTool: import.meta.env.DEV,
28
28
  },
29
+
30
+ // default form props
31
+ form: {
32
+ noValidate: true,
33
+ className: 'my-form-class',
34
+ },
29
35
  },
30
36
 
31
37
  // optional (defaults to primitive HTML form)
32
38
  FormComponent: Form.Root,
33
39
 
40
+ // optional wrapper to inject your own hooks and components
41
+ // `children` prop is provided by RhfUtilsZodForm's Children prop
42
+ FormChildren: (
43
+ // UseRhfUtilsFormChildrenProps
44
+ {
45
+ formId, // unique id string
46
+ formRef, // ref
47
+ context, // rhf UseFormReturn
48
+ options, // RhfUtilsFormOptions
49
+ Controller, // strictly-typed controller
50
+ FormSubmitError, // strictly-typed error class
51
+ children, // RhfUtilsZodForm.Children
52
+ },
53
+ ) => {
54
+ // navigation blocker (using custom option)
55
+ useFormRouterBlocker(!!options?.prompter);
56
+
57
+ return (
58
+ <>
59
+ {/* specific form children -- e.g., fields, etc. */}
60
+ {children}
61
+
62
+ {/* global error list */}
63
+ <RootErrorsListFromFlatFieldErrorsContext />
64
+ </>
65
+ );
66
+ },
67
+
34
68
  // non-FormSubmitError throw in onSubmit
35
- // use case: transform backend error
69
+ // example use case: transform backend error
36
70
  onSubmitUnknownError: (
37
71
  error, // unknown
38
72
  ) => {
39
73
  // if other error, re-throw to handle in ErrorBoundary
40
74
  if (!(error instanceof MyServerError)) throw error;
41
75
 
42
- // return to be merged into RHF context errors
76
+ // return FormSubmitErrors object to be merged into RHF context errors
43
77
  return transformServerErrorToFormSubmitErrors(error);
44
78
  },
45
79
 
46
- Children: {
47
- top: (
48
- // UseRhfUtilsFormChildrenProps
49
- {
50
- formId, // unique id string
51
- formRef, // ref
52
- context, // rhf UseFormReturn
53
- options, // RhfUtilsFormOptions
54
- Controller, // strictly-typed controller
55
- FormSubmitError, // strictly-typed error class
56
- },
57
- ) => {
58
- // navigation blocker (using custom option)
59
- useFormRouterBlocker(!!options?.prompter);
60
-
61
- return null;
62
- },
63
-
64
- bottom: () => (
65
- <>
66
- {/* global error list */}
67
- <RootErrorsListFromFlatFieldErrorsContext />
68
- </>
69
- ),
70
- },
71
-
72
80
  errors: {
73
81
  output: {
74
82
  // callback to determine when to output form context errors to console ("debug" or "error")
75
83
  console: ({ hasOrphans }) =>
76
- (hasOrphans && { type: 'error' }) || // report all orphan errors
84
+ (hasOrphans && { type: 'error' }) || // console error for reporting on prod
77
85
  (import.meta.env.DEV && { type: 'debug' }), // facilitate debugging on dev
78
86
 
79
87
  // callback to determine when to throw an error based on context
@@ -159,9 +167,9 @@ function Children({...}: RhfUtilsUseFormChildrenZodProps<typeof schema>) { }
159
167
 
160
168
  ## `FormSubmitError`
161
169
 
162
- This is an `Error`-based class you can use to throw a structured error in your submit handler. It is strictly-typed, so it only allows field names in schema and `root`/`root.{string}` keys.
170
+ This is an `Error`-based class you can use to throw a structured error in your submit handler. It is strictly-typed, so it only allows field names from your schema and `root`/`root.{string}` keys.
163
171
 
164
- It uses `FormSubmitErrors` shape, which is a flat (dot notation) equivalent of RHF's `FieldErrors`.
172
+ It uses `FormSubmitErrors` type's structure, which is a flat, simplified version of RHF's `FieldErrors`.
165
173
 
166
174
  Example:
167
175
 
@@ -189,11 +197,6 @@ These options can be set globally and/or per form.
189
197
 
190
198
  ```ts
191
199
  type RhfUtilsFormOptions = {
192
- /**
193
- * Style via your own classes.
194
- */
195
- classNames?: string[];
196
-
197
200
  /** Request submit via listener on form change. */
198
201
  submitOnChange?: boolean;
199
202
 
@@ -1,52 +1,51 @@
1
- import { jsx as h } from "react/jsx-runtime";
2
- import f from "../form/FormWithProviders.mjs";
3
- import d from "../form/useRhfUtilsForm.mjs";
4
- const R = (e) => (r) => {
5
- const m = F(e, r);
1
+ import { jsx as u } from "react/jsx-runtime";
2
+ import a from "../form/FormWithProviders.mjs";
3
+ import F from "../form/useRhfUtilsForm.mjs";
4
+ const v = (e) => (r) => {
5
+ const m = d(e, r);
6
6
  return {
7
- useForm: (i, s) => {
8
- const t = m(i, s);
7
+ useForm: (s, t) => {
8
+ const o = m(s, t);
9
9
  return {
10
- ...d(t),
11
- FormWithProviders: ({
12
- children: o
13
- }) => /* @__PURE__ */ h(f, { ...t, children: o })
10
+ ...F(o),
11
+ FormWithProviders: () => /* @__PURE__ */ u(a, { ...o })
14
12
  };
15
13
  },
16
- FormWithProviders: a(e, r)
14
+ FormWithProviders: P(e, r)
17
15
  };
18
- }, F = (e, r) => function(i, s) {
19
- var t, o, l, n, u;
16
+ }, d = (e, r) => function(s, t) {
17
+ var o, i, f, l, n, h;
20
18
  return {
21
- ...s,
19
+ ...t,
22
20
  rhf: {
23
- ...(t = r.defaults) == null ? void 0 : t.rhf,
24
- ...s.rhf,
25
- resolver: e(i)
21
+ ...(o = r.defaults) == null ? void 0 : o.rhf,
22
+ ...t.rhf,
23
+ resolver: e(s)
26
24
  },
27
25
  utils: {
28
- ...(o = r.defaults) == null ? void 0 : o.utils,
29
- ...s.utils,
30
- classNames: [
31
- ...((n = (l = r.defaults) == null ? void 0 : l.utils) == null ? void 0 : n.classNames) ?? [],
32
- ...((u = s.utils) == null ? void 0 : u.classNames) ?? []
33
- ]
26
+ ...(i = r.defaults) == null ? void 0 : i.utils,
27
+ ...t.utils
28
+ },
29
+ form: {
30
+ ...(f = r.defaults) == null ? void 0 : f.form,
31
+ ...t.form,
32
+ className: [(n = (l = r.defaults) == null ? void 0 : l.form) == null ? void 0 : n.className, (h = t.form) == null ? void 0 : h.className].filter(Boolean).join(" ")
34
33
  }
35
34
  };
36
- }, a = (e, r) => function({
37
- schema: i,
38
- children: s,
39
- ...t
35
+ }, P = (e, r) => function({
36
+ schema: s,
37
+ children: t,
38
+ ...o
40
39
  }) {
41
- const o = F(e, r);
42
- return /* @__PURE__ */ h(
43
- f,
40
+ const i = d(e, r);
41
+ return /* @__PURE__ */ u(
42
+ a,
44
43
  {
45
- ...o(i, t),
46
- children: s
44
+ ...i(s, o),
45
+ children: t
47
46
  }
48
47
  );
49
48
  };
50
49
  export {
51
- R as createRhfUtilsClient
50
+ v as createRhfUtilsClient
52
51
  };
@@ -1,98 +1,82 @@
1
- import { jsxs as _, jsx as d } from "react/jsx-runtime";
1
+ import { jsx as a, jsxs as v, Fragment as E } from "react/jsx-runtime";
2
2
  import "react";
3
- import { useFormContext as v } from "react-hook-form";
4
- import N from "../client/config/useRhfUtilsClientConfigContext.mjs";
5
- import P from "../devtool/LazyDevTool.mjs";
6
- import { FormSubmitError as x } from "../submit/error/FormSubmitError.mjs";
7
- import S from "../submit/error/setCtxErrorsByFormSubmitErrors.mjs";
8
- import w from "./_Controller.mjs";
9
- import E from "./_FormBase.mjs";
10
- function A({
11
- formId: s,
12
- formRef: m,
3
+ import { useFormContext as P } from "react-hook-form";
4
+ import w from "../client/config/useRhfUtilsClientConfigContext.mjs";
5
+ import S from "../devtool/LazyDevTool.mjs";
6
+ import { FormSubmitError as h } from "../submit/error/FormSubmitError.mjs";
7
+ import U from "../submit/error/setCtxErrorsByFormSubmitErrors.mjs";
8
+ import _ from "./_Controller.mjs";
9
+ function G({
10
+ formId: n,
11
+ formRef: p,
13
12
  //
14
13
  onSubmit: c,
15
- onSubmitError: C,
16
- onSubmitInvalid: b,
17
- Children: h,
14
+ onSubmitError: d,
15
+ onSubmitInvalid: F,
16
+ Children: i,
18
17
  //
19
- form: p,
20
- utils: o,
21
- //
22
- children: i
18
+ form: l,
19
+ utils: o
23
20
  }) {
24
- const n = v(), r = N(), j = async (t) => {
21
+ const e = P(), r = w(), y = async (t) => {
25
22
  o != null && o.stopSubmitPropagation && t.stopPropagation();
26
23
  try {
27
- await n.handleSubmit(B, b)(t);
28
- } catch (e) {
29
- T(e, t);
24
+ await e.handleSubmit(f, F)(t);
25
+ } catch (m) {
26
+ b(m, t);
30
27
  }
31
- }, B = (t, e) => c == null ? void 0 : c(
28
+ }, f = (t, m) => c == null ? void 0 : c(
32
29
  t,
33
30
  {
34
- formId: s,
35
- formRef: m,
36
- context: n,
31
+ formId: n,
32
+ formRef: p,
33
+ context: e,
37
34
  utils: o,
38
- FormSubmitError: x
35
+ FormSubmitError: h
39
36
  },
40
- e
41
- ), T = (t, e) => {
42
- var F;
43
- const a = t instanceof x ? (
37
+ m
38
+ ), b = (t, m) => {
39
+ var x;
40
+ const C = t instanceof h ? (
44
41
  // consumer can manually throw FormSubmitError (e.g., manual validation)
45
42
  t.errors
46
43
  ) : (
47
44
  // if error is not FormSubmitError, consumer can provide global handler
48
- (F = r == null ? void 0 : r.onSubmitUnknownError) == null ? void 0 : F.call(r, t)
45
+ (x = r == null ? void 0 : r.onSubmitUnknownError) == null ? void 0 : x.call(r, t)
49
46
  );
50
- a && S(n, a), C == null || C(
47
+ C && U(e, C), d == null || d(
51
48
  t,
52
49
  {
53
- formId: s,
54
- formRef: m,
55
- context: n,
50
+ formId: n,
51
+ formRef: p,
52
+ context: e,
56
53
  utils: o
57
54
  },
58
- e
55
+ m
59
56
  );
60
- }, l = {
61
- formId: s,
62
- formRef: m,
63
- context: n,
57
+ }, s = {
58
+ formId: n,
59
+ formRef: p,
60
+ context: e,
64
61
  utils: o,
65
- Controller: w,
66
- FormSubmitError: x
67
- }, y = (t) => {
68
- var a;
69
- const e = (a = r == null ? void 0 : r.Children) == null ? void 0 : a[t];
70
- return e && /* @__PURE__ */ d(e, { ...l });
71
- };
72
- return /* @__PURE__ */ _(
73
- E,
74
- {
75
- noValidate: !0,
76
- Component: r == null ? void 0 : r.FormComponent,
77
- formRef: m,
78
- ...p,
79
- className: [o == null ? void 0 : o.classNames, p == null ? void 0 : p.className].filter(Boolean).join(" "),
80
- onSubmit: j,
81
- children: [
82
- y("top"),
83
- (o == null ? void 0 : o.devTool) && /* @__PURE__ */ d(
84
- P,
85
- {
86
- props: typeof o.devTool == "object" ? o.devTool : void 0
87
- }
88
- ),
89
- h && /* @__PURE__ */ d(h, { ...l }),
90
- i,
91
- y("bottom")
92
- ]
93
- }
94
- );
62
+ Controller: _,
63
+ FormSubmitError: h
64
+ }, T = (r == null ? void 0 : r.FormComponent) ?? "form", j = (r == null ? void 0 : r.FormChildren) ?? k;
65
+ return /* @__PURE__ */ a(T, { ...l, ref: p, onSubmit: y, children: /* @__PURE__ */ v(j, { ...s, children: [
66
+ (o == null ? void 0 : o.devTool) && /* @__PURE__ */ a(
67
+ S,
68
+ {
69
+ props: typeof o.devTool == "object" ? o.devTool : void 0
70
+ }
71
+ ),
72
+ /* @__PURE__ */ a(i, { ...s })
73
+ ] }) });
74
+ }
75
+ function k({
76
+ children: n
77
+ }) {
78
+ return /* @__PURE__ */ a(E, { children: n });
95
79
  }
96
80
  export {
97
- A as default
81
+ G as default
98
82
  };
@@ -1,14 +1,11 @@
1
1
  import { jsx as o } from "react/jsx-runtime";
2
- import t from "./Form.mjs";
3
- import f from "./RhfUtilsFormProviders.mjs";
4
- import s from "./useRhfUtilsForm.mjs";
5
- function h({
6
- children: m,
7
- ...i
8
- }) {
9
- const r = s(i);
10
- return /* @__PURE__ */ o(f, { ...r.providerProps, children: /* @__PURE__ */ o(t, { ...r.formProps, children: m }) });
2
+ import i from "./Form.mjs";
3
+ import t from "./RhfUtilsFormProviders.mjs";
4
+ import f from "./useRhfUtilsForm.mjs";
5
+ function n(m) {
6
+ const r = f(m);
7
+ return /* @__PURE__ */ o(t, { ...r.providerProps, children: /* @__PURE__ */ o(i, { ...r.formProps }) });
11
8
  }
12
9
  export {
13
- h as default
10
+ n as default
14
11
  };
@@ -1,7 +1,7 @@
1
1
  import { UseFormProps } from 'react-hook-form';
2
2
  import { FlatFieldErrorsOutputConfig } from '../../errors/flat/context/FlatFieldErrorsOutputConfig';
3
- import { FormChildrenSlots } from '../../form/Form';
4
3
  import { RhfUtilsFormOptions } from '../../form/RhfUtilsFormOptions';
4
+ import { SafeFieldValues } from '../../form/SafeFieldValues';
5
5
  import { UseRhfUtilsFormChildrenProps } from '../../form/UseRhfUtilsFormChildrenProps';
6
6
  import { FormSubmitErrors } from '../../submit/error/FormSubmitErrors';
7
7
 
@@ -27,6 +27,10 @@ export type RhfUtilsClientConfig = {
27
27
  * (Extend this type via `Register` type to inject custom options/functionality via `Children` component.)
28
28
  */
29
29
  utils?: RhfUtilsFormOptions;
30
+ /**
31
+ * Default props for form element.
32
+ */
33
+ form?: Omit<React.PropsWithoutRef<JSX.IntrinsicElements['form']>, 'onSubmit'>;
30
34
  };
31
35
  /**
32
36
  * Supply your own `<form>` component.
@@ -34,6 +38,14 @@ export type RhfUtilsClientConfig = {
34
38
  * By default, a primitive `<form>` element is used.
35
39
  */
36
40
  FormComponent?: React.FC<React.PropsWithChildren<React.HTMLAttributes<HTMLFormElement>>>;
41
+ /**
42
+ * Wrap all form bodies with your own hooks, components, etc.
43
+ *
44
+ * Use cases:
45
+ * - custom hooks/behaviors based on `options`
46
+ * - shared header, footer
47
+ */
48
+ FormChildren?: <TFieldValues extends SafeFieldValues, TTransformedValues extends SafeFieldValues>(props: React.PropsWithChildren<UseRhfUtilsFormChildrenProps<TFieldValues, TTransformedValues>>) => React.ReactNode;
37
49
  /**
38
50
  * Global submit error handler when/if submit handler throws an error other than {@link FormSubmitError}.
39
51
  *
@@ -45,14 +57,6 @@ export type RhfUtilsClientConfig = {
45
57
  * - if `undefined`: do nothing.
46
58
  */
47
59
  onSubmitUnknownError?: (error: unknown) => FormSubmitErrors | undefined;
48
- /**
49
- * Components injected by slot across all forms.
50
- * (Per-form injection can be done via `Children` prop.)
51
- *
52
- * - `top`: e.g., block navigation on dirty form.
53
- * - `bottom`: e.g., root errors list.
54
- */
55
- Children?: Partial<Record<FormChildrenSlots, React.FC<UseRhfUtilsFormChildrenProps>>>;
56
60
  errors?: {
57
61
  /**
58
62
  * Configure how/if errors should be outputted.
@@ -3,9 +3,9 @@ import { SafeFieldValues } from './SafeFieldValues';
3
3
  import { UseRhfUtilsFormProps } from './UseRhfUtilsFormProps';
4
4
 
5
5
  export type FormChildrenSlots = 'top' | 'bottom';
6
- export type FormProps<TFieldValues extends SafeFieldValues, TTransformedValues extends SafeFieldValues> = React.PropsWithChildren<{
6
+ export type FormProps<TFieldValues extends SafeFieldValues, TTransformedValues extends SafeFieldValues> = {
7
7
  formId: string;
8
8
  formRef: React.RefObject<HTMLFormElement>;
9
- } & Pick<UseRhfUtilsFormProps<TFieldValues, TTransformedValues>, 'onSubmit' | 'onSubmitError' | 'onSubmitInvalid' | 'Children' | 'form' | 'utils'>>;
10
- declare function _Form<TFieldValues extends SafeFieldValues, TTransformedValues extends SafeFieldValues>({ formId, formRef, onSubmit, onSubmitError, onSubmitInvalid, Children, form, utils, children, }: FormProps<TFieldValues, TTransformedValues>): import("react/jsx-runtime").JSX.Element;
9
+ } & Pick<UseRhfUtilsFormProps<TFieldValues, TTransformedValues>, 'onSubmit' | 'onSubmitError' | 'onSubmitInvalid' | 'Children' | 'form' | 'utils'>;
10
+ declare function _Form<TFieldValues extends SafeFieldValues, TTransformedValues extends SafeFieldValues>({ formId, formRef, onSubmit, onSubmitError, onSubmitInvalid, Children, form, utils, }: FormProps<TFieldValues, TTransformedValues>): import("react/jsx-runtime").JSX.Element;
11
11
  export default _Form;
@@ -9,5 +9,5 @@ export type FormWithProvidersProps<TFieldValues extends SafeFieldValues, TTransf
9
9
  * Use {@link useRhfUtilsForm} if you need to specific nesting hierarchy.
10
10
  * (e.g., wrapping `<tr>` with provider and nesting `<form>` within `<td>` to appease HTML rules.)
11
11
  */
12
- declare function FormWithProviders<TFieldValues extends SafeFieldValues, TTransformedValues extends SafeFieldValues>({ children, ...props }: FormWithProvidersProps<TFieldValues, TTransformedValues>): import("react/jsx-runtime").JSX.Element;
12
+ declare function FormWithProviders<TFieldValues extends SafeFieldValues, TTransformedValues extends SafeFieldValues>(props: FormWithProvidersProps<TFieldValues, TTransformedValues>): import("react/jsx-runtime").JSX.Element;
13
13
  export default FormWithProviders;
@@ -8,11 +8,6 @@ import { Register } from '../register';
8
8
  * Can be extended using {@link Register.RhfUtilsFormOptions}.
9
9
  */
10
10
  export type RhfUtilsFormOptions = {
11
- /**
12
- * Style via your own classes.
13
- * (Defaults merged with individual class names.)
14
- */
15
- classNames?: string[];
16
11
  /** Request submit via listener on form change. */
17
12
  submitOnChange?: boolean;
18
13
  /** Reset form values if submit throw errors. (Use case: optimistic UI toggle form that submits on each change.) */
@@ -12,11 +12,11 @@ export type UseRhfUtilsFormProps<TFieldValues extends SafeFieldValues, TTransfor
12
12
  /** Submit handler when form is invalid. */
13
13
  onSubmitInvalid?: SubmitErrorHandler<TFieldValues>;
14
14
  defaultValues?: UseFormProps<TFieldValues>['defaultValues'];
15
- Children?: React.FC<UseRhfUtilsFormChildrenProps<TFieldValues, TTransformedValues>>;
15
+ Children: React.FC<UseRhfUtilsFormChildrenProps<TFieldValues, TTransformedValues>>;
16
16
  /** {@link RhfUseFormProps} props for RHF's {@link useForm}. */
17
17
  rhf?: Omit<UseFormProps<TFieldValues>, 'defaultValues'>;
18
- /** {@link FormProps} props for Form. */
19
- form?: React.PropsWithoutRef<JSX.IntrinsicElements['form']>;
20
18
  /** {@link RhfUtilsFormOptions} to enabled utils functionality. */
21
19
  utils?: RhfUtilsFormOptions;
20
+ /** {@link FormProps} props for Form. */
21
+ form?: Omit<React.PropsWithoutRef<JSX.IntrinsicElements['form']>, 'onSubmit'>;
22
22
  };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@paragrav/rhf-utils",
3
3
  "author": "paragrav.dev",
4
4
  "license": "MIT",
5
- "version": "0.0.117",
5
+ "version": "0.0.119",
6
6
  "description": "Integration utilities for react-hook-form.",
7
7
  "type": "module",
8
8
  "sideEffects": false,
@@ -1,11 +0,0 @@
1
- import { jsx as m } from "react/jsx-runtime";
2
- import "react";
3
- const s = ({
4
- formRef: o,
5
- Component: r,
6
- children: t,
7
- ...e
8
- }) => /* @__PURE__ */ m(r ?? "form", { role: "form", ...e, ref: o, children: t });
9
- export {
10
- s as default
11
- };