@paragrav/rhf-utils 0.0.143 → 0.0.145

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
@@ -2,7 +2,7 @@
2
2
 
3
3
  ## About
4
4
 
5
- Integration and utility library for [react-hook-form](https://www.react-hook-form.com/) and [zod](https://www.npmjs.com/package/zod). Declaratively configure your forms via global and form-level options.
5
+ Integration and utility library for [react-hook-form](https://www.react-hook-form.com/) (and [zod](https://www.npmjs.com/package/zod)). Declaratively configure your forms via global and form-level options.
6
6
 
7
7
  ## Features
8
8
 
@@ -69,7 +69,7 @@ export const rhfUtilsClientConfig: RhfUtilsClientConfig = {
69
69
  // optional form component (defaults to primitive HTML form)
70
70
  FormComponent: Form.Root,
71
71
 
72
- // optional wrapper to inject your own hooks and components
72
+ // optional wrapper component to inject your own hooks and components
73
73
  // around all RhfUtilsZodForm instances
74
74
  FormChildrenWrapper: (
75
75
  // UseRhfUtilsFormChildrenProps
@@ -93,7 +93,7 @@ export const rhfUtilsClientConfig: RhfUtilsClientConfig = {
93
93
 
94
94
  return (
95
95
  <>
96
- {/* RhfUtilsZodForm.Children outlet (see "Component Hierarchy" section) */}
96
+ {/* RhfUtilsZodForm.Children "outlet" (see "Component Hierarchy" section) */}
97
97
  {children}
98
98
 
99
99
  {/* root errors list */}
@@ -103,7 +103,7 @@ export const rhfUtilsClientConfig: RhfUtilsClientConfig = {
103
103
  },
104
104
 
105
105
  // non-FormSubmitError thrown in onSubmit
106
- // use case: handle and transform error data for frontend
106
+ // use case: handle and transform server error data for frontend
107
107
  onSubmitErrorUnknown: (
108
108
  error, // unknown
109
109
  ) => {
@@ -166,7 +166,7 @@ Currently, only `zod` is supported.
166
166
  // handle error declaratively (i.e., no throw/catch)
167
167
  onSubmitError={({
168
168
  error, // unknown
169
- context, // UseRhfUtilsFormOnSubmitErrorContext (id, ref, rhf context, utils options)
169
+ context, // UseRhfUtilsFormOnSubmitErrorContext (id, ref, rhf context, utils options, `FormSubmitFieldErrors` from `onSubmitErrorUnknown`)
170
170
  event, // SubmitEvent
171
171
  }) => {
172
172
  props.onError(error);
@@ -227,12 +227,14 @@ function Children({...}: RhfUtilsUseFormChildrenZodProps<typeof schema>) { }
227
227
 
228
228
  ## Form Component Hierarchy
229
229
 
230
+ `RhfUtilsZodForm` renders as:
231
+
230
232
  ```tsx
231
- <ReactHookForm.FormProvider>
232
- <RhfUtilsProviders>
233
- <RhfUtilsClientConfig.FormComponent>
234
- <RhfUtilsClientConfig.FormChildrenWrapper>
235
- <RhfUtilsZodForm.Children />
233
+ <ReactHookForm.FormProvider> // standard rhf provider
234
+ <RhfUtilsProviders> // rhf utils internal providers
235
+ <RhfUtilsClientConfig.FormComponent> // from global config (or default)
236
+ <RhfUtilsClientConfig.FormChildrenWrapper> // from global config (if provided)
237
+ <RhfUtilsZodForm.Children /> // form instance component prop
236
238
  </RhfUtilsClientConfig.FormChildrenWrapper>
237
239
  </RhfUtilsClientConfig.FormComponent>
238
240
  <RhfUtilsProviders>
@@ -266,7 +268,7 @@ Most common use case will be transforming backend errors to frontend shape.
266
268
 
267
269
  ## `RhfUtilsFormOptions`
268
270
 
269
- These options can be set globally and/or per form.
271
+ These built-in options can be set globally and/or per form.
270
272
 
271
273
  ```ts
272
274
  type RhfUtilsFormOptions = {
@@ -301,7 +303,7 @@ Use `useRhfUtilsContextRequestSubmit` hook to get `requestSubmit` function for c
301
303
 
302
304
  ## Extend `RhfUtilsFormOptions`
303
305
 
304
- Extend `RhfUtilsFormOptions` with custom options, which get passed to `Children` component in `options` prop. These can take any shape, and allow you to override your own functionality at form-level.
306
+ Extend `RhfUtilsFormOptions` with custom options, which get passed to `RhfUtilsClientConfig`'s `ChildrenWrapper` and `RhfUtilsZodForm`'s `Children` components via `options` prop. These can take any shape, and allow you to override your own functionality at form-level.
305
307
 
306
308
  ```tsx
307
309
  import '@paragrav/rhf-utils';
@@ -309,8 +311,9 @@ import '@paragrav/rhf-utils';
309
311
  declare module '@paragrav/rhf-utils' {
310
312
  export interface Register {
311
313
  RhfUtilsFormOptions: {
312
- /** Enable user prompt to confirm navigating away from dirty form. */
314
+ /** Custom props for your custom hooks/behaviors. */
313
315
  enableMyOptionalFormHook?: boolean;
316
+ configMyOptionalFormHook?: MyCustomFormHookConfig;
314
317
  };
315
318
  }
316
319
  }
@@ -1,6 +1,6 @@
1
1
  import { jsx as i, Fragment as B, jsxs as Z } from "react/jsx-runtime";
2
2
  import a from "react";
3
- import { c as E } from "./createContext.mjs";
3
+ import { c as C } from "./createContext.mjs";
4
4
  import { zodResolver as I } from "@hookform/resolvers/zod";
5
5
  import { useFormContext as b, Controller as L, get as z, useFormState as x, FormProvider as A, useForm as $ } from "react-hook-form";
6
6
  import { i as O, _ as H, u as g } from "./useFlatFieldErrorsContext.mjs";
@@ -8,7 +8,7 @@ import { flatten as Q } from "flat";
8
8
  const {
9
9
  Provider: G,
10
10
  useMaybe: _
11
- } = E(), J = a.lazy(
11
+ } = C(), J = a.lazy(
12
12
  () => import("@hookform/devtools").then((r) => ({
13
13
  default: r.DevTool
14
14
  }))
@@ -51,11 +51,11 @@ function er({
51
51
  const m = b(), d = _(), c = async (h) => {
52
52
  u != null && u.stopSubmitPropagation && h.stopPropagation();
53
53
  try {
54
- await m.handleSubmit(v, s)(h);
54
+ await m.handleSubmit(p, s)(h);
55
55
  } catch (f) {
56
- p(f, h);
56
+ P(f, h);
57
57
  }
58
- }, v = (h, f) => e == null ? void 0 : e(
58
+ }, p = (h, f) => e == null ? void 0 : e(
59
59
  h,
60
60
  {
61
61
  formId: r,
@@ -65,33 +65,34 @@ function er({
65
65
  FormSubmitError: F
66
66
  },
67
67
  f
68
- ), p = (h, f) => {
68
+ ), P = (h, f) => {
69
69
  var y;
70
- const R = h instanceof F ? (
70
+ const E = h instanceof F ? (
71
71
  // consumer can manually throw FormSubmitError (e.g., manual validation)
72
72
  h.errors
73
73
  ) : (
74
74
  // if error is not FormSubmitError, consumer can provide global handler
75
75
  (y = d == null ? void 0 : d.onSubmitErrorUnknown) == null ? void 0 : y.call(d, h)
76
76
  );
77
- R && X(m, R), o == null || o(
77
+ E && X(m, E), o == null || o(
78
78
  h,
79
79
  {
80
80
  formId: r,
81
81
  formRef: t,
82
82
  context: m,
83
- utils: u
83
+ utils: u,
84
+ errors: E
84
85
  },
85
86
  f
86
87
  );
87
- }, P = {
88
+ }, R = {
88
89
  formId: r,
89
90
  formRef: t,
90
91
  context: m,
91
92
  utils: u,
92
93
  Controller: rr,
93
94
  FormSubmitError: F
94
- }, V = P, N = (d == null ? void 0 : d.FormComponent) ?? "form", q = (d == null ? void 0 : d.FormChildrenWrapper) ?? Y;
95
+ }, V = R, N = (d == null ? void 0 : d.FormComponent) ?? "form", q = (d == null ? void 0 : d.FormChildrenWrapper) ?? Y;
95
96
  return /* @__PURE__ */ i(
96
97
  N,
97
98
  {
@@ -106,18 +107,18 @@ function er({
106
107
  props: typeof (u == null ? void 0 : u.devTool) == "object" ? u.devTool : void 0
107
108
  }
108
109
  ),
109
- /* @__PURE__ */ i(n, { ...P })
110
+ /* @__PURE__ */ i(n, { ...R })
110
111
  ] })
111
112
  }
112
113
  );
113
114
  }
114
- const C = (r) => (t) => (
115
+ const v = (r) => (t) => (
115
116
  // re-create object from entries
116
117
  Object.fromEntries(
117
118
  // break down to entries
118
119
  Object.entries(t).filter(r)
119
120
  )
120
- ), k = (r) => !!r.ref, tr = ([, r]) => k(r), or = C(
121
+ ), k = (r) => !!r.ref, tr = ([, r]) => k(r), or = v(
121
122
  tr
122
123
  ), W = "data-rhfutils-nonfield-error-marker-path", sr = (r, t) => (
123
124
  // list of selector variants
@@ -144,9 +145,9 @@ const C = (r) => (t) => (
144
145
  !k(t) && // non-root
145
146
  !j(r) && // not marked in DOM
146
147
  !ur(r, e)
147
- ), ir = (r) => ([t, e]) => lr(t, e, r), dr = (r, t) => C(
148
+ ), ir = (r) => ([t, e]) => lr(t, e, r), dr = (r, t) => v(
148
149
  ir(t)
149
- )(r), ar = ([r]) => j(r), mr = C(
150
+ )(r), ar = ([r]) => j(r), mr = v(
150
151
  ar
151
152
  ), hr = (r) => Q(r), cr = (r) => {
152
153
  const t = hr(r), e = Object.keys(t);
@@ -289,7 +290,7 @@ const C = (r) => (t) => (
289
290
  n.removeEventListener("change", l);
290
291
  };
291
292
  }, []);
292
- }, { Provider: Sr, useRequired: Ur } = E(), Zr = Ur, xr = ({
293
+ }, { Provider: Sr, useRequired: Ur } = C(), Zr = Ur, xr = ({
293
294
  formId: r,
294
295
  formRef: t,
295
296
  options: e,
@@ -438,7 +439,7 @@ const _r = (r, ...t) => (
438
439
  }, jr = kr(I), {
439
440
  Provider: wr,
440
441
  useRequired: D
441
- } = E(), Ir = ({
442
+ } = C(), Ir = ({
442
443
  config: r,
443
444
  children: t
444
445
  }) => {
@@ -1,6 +1,7 @@
1
1
  import { default as React } from 'react';
2
2
  import { UseFormReturn } from 'react-hook-form';
3
3
  import { FormSubmitError } from '../submit/error/FormSubmitError';
4
+ import { FormSubmitFieldErrors } from '../submit/error/FormSubmitFieldErrors';
4
5
  import { RhfUtilsFormOptions } from './RhfUtilsFormOptions';
5
6
  import { SafeFieldValues } from './SafeFieldValues';
6
7
  /**
@@ -23,4 +24,5 @@ export type UseRhfUtilsFormOnSubmitErrorContext<TFieldValues extends SafeFieldVa
23
24
  formRef: React.RefObject<HTMLFormElement>;
24
25
  context: UseFormReturn<TFieldValues, unknown, TTransformedValues>;
25
26
  utils?: RhfUtilsFormOptions;
27
+ errors?: FormSubmitFieldErrors;
26
28
  };
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.143",
5
+ "version": "0.0.145",
6
6
  "description": "Integration utilities for react-hook-form.",
7
7
  "type": "module",
8
8
  "sideEffects": false,