@paragrav/rhf-utils 0.0.138 → 0.0.139

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.
Files changed (78) hide show
  1. package/README.md +10 -17
  2. package/dist/esm/{utils/createContext.mjs → createContext-Jzs0Nsql.js} +1 -1
  3. package/dist/esm/exports.mjs +483 -44
  4. package/dist/esm/form/context/group/exports.mjs +74 -0
  5. package/dist/types/devtool/LazyDevTool.d.ts +1 -1
  6. package/dist/types/errors/nonfield/{FormNonFieldErrorMarker.d.ts → RhfUtilsNonFieldErrorMarker.d.ts} +2 -2
  7. package/dist/types/errors/nonfield/{FormNonFieldErrorMarker.utils.d.ts → RhfUtilsNonFieldErrorMarker.utils.d.ts} +1 -1
  8. package/dist/types/errors/nonfield/RhfUtilsNonFieldErrorMarkerHtmlAttribute.d.ts +5 -0
  9. package/dist/types/exports.d.ts +1 -15
  10. package/dist/types/form/context/group/exports.d.ts +9 -0
  11. package/package.json +7 -1
  12. package/dist/esm/client/config/useRhfUtilsClientConfigContext.mjs +0 -9
  13. package/dist/esm/client/index.mjs +0 -49
  14. package/dist/esm/client/utils.mjs +0 -38
  15. package/dist/esm/client/zod/context/RhfUtilsClientForZodContextProvider.mjs +0 -17
  16. package/dist/esm/client/zod/context/RhfUtilsZodForm.mjs +0 -12
  17. package/dist/esm/client/zod/context/useRhfUtilsClientForZodContext.mjs +0 -9
  18. package/dist/esm/client/zod/context/useRhfUtilsZodForm.mjs +0 -5
  19. package/dist/esm/client/zod/createRhfUtilsClientForZod.mjs +0 -6
  20. package/dist/esm/devtool/LazyDevTool.mjs +0 -15
  21. package/dist/esm/errors/flat/FlatFieldErrorsList.mjs +0 -17
  22. package/dist/esm/errors/flat/context/FlatFieldErrorsContextProvider.mjs +0 -27
  23. package/dist/esm/errors/flat/context/useFlatFieldErrorsContext.mjs +0 -9
  24. package/dist/esm/errors/flat/context/useFlatFieldErrorsContextHasOnlyOrphans.mjs +0 -9
  25. package/dist/esm/errors/flat/context/useFlatFieldErrorsContextOutput.mjs +0 -24
  26. package/dist/esm/errors/flat/filterFlatFieldErrors.mjs +0 -10
  27. package/dist/esm/errors/flat/flattenFieldErrors.mjs +0 -5
  28. package/dist/esm/errors/flat/getFlatFieldErrors.mjs +0 -24
  29. package/dist/esm/errors/getRefdFromFlatFieldErrors.mjs +0 -8
  30. package/dist/esm/errors/isFieldErrorRefd.mjs +0 -4
  31. package/dist/esm/errors/isFlatFieldErrorEntryRefd.mjs +0 -5
  32. package/dist/esm/errors/message/FormErrorMessage.mjs +0 -14
  33. package/dist/esm/errors/message/RhfUtilsFieldErrorMessage.mjs +0 -14
  34. package/dist/esm/errors/nonfield/FormNonFieldErrorMarker.mjs +0 -19
  35. package/dist/esm/errors/nonfield/FormNonFieldErrorMarker.utils.mjs +0 -23
  36. package/dist/esm/errors/nonfield/FormNonFieldErrorMarkerHtmlAttribute.mjs +0 -4
  37. package/dist/esm/errors/nonfield/isNonFieldErrorMarkerInDOM.mjs +0 -5
  38. package/dist/esm/errors/orphan/getIsOrphanFormErrorWithParentElement.mjs +0 -5
  39. package/dist/esm/errors/orphan/getOrphansFromFlatFieldErrors.mjs +0 -8
  40. package/dist/esm/errors/orphan/isOrphanFormError.mjs +0 -12
  41. package/dist/esm/errors/output/consoleErrors.mjs +0 -6
  42. package/dist/esm/errors/root/RootErrorsListFromFlatFieldErrorsContext.mjs +0 -10
  43. package/dist/esm/errors/root/consts.mjs +0 -4
  44. package/dist/esm/errors/root/getRootsFromFlatFieldErrors.mjs +0 -8
  45. package/dist/esm/errors/root/isFlatFieldErrorEntryPathRoot.mjs +0 -5
  46. package/dist/esm/errors/root/isFormErrorPathRoot.mjs +0 -5
  47. package/dist/esm/form/Form.mjs +0 -86
  48. package/dist/esm/form/FormWithProviders.mjs +0 -11
  49. package/dist/esm/form/RhfUtilsFormProviders.mjs +0 -24
  50. package/dist/esm/form/_Controller.mjs +0 -8
  51. package/dist/esm/form/context/group/FormGroupContextProvider.mjs +0 -30
  52. package/dist/esm/form/context/group/index.mjs +0 -11
  53. package/dist/esm/form/context/group/useFormGroupChildIsMountedTracker.mjs +0 -6
  54. package/dist/esm/form/context/group/useFormGroupChildIsSubmittingTracker.mjs +0 -10
  55. package/dist/esm/form/context/group/useFormGroupChildTracker.mjs +0 -13
  56. package/dist/esm/form/context/group/useFormGroupIsAnyBusy.mjs +0 -8
  57. package/dist/esm/form/context/group/useFormGroupIsChildBusy.mjs +0 -8
  58. package/dist/esm/form/context/group/useFormGroupIsParentBusy.mjs +0 -8
  59. package/dist/esm/form/context/group/useFormGroupParentTracker.mjs +0 -13
  60. package/dist/esm/form/context/group/useFormOrParentIsBusy.mjs +0 -10
  61. package/dist/esm/form/context/utils/RhfUtilsContextProvider.mjs +0 -23
  62. package/dist/esm/form/context/utils/useRhfUtilsContext.mjs +0 -6
  63. package/dist/esm/form/context/utils/useRhfUtilsContextRequestSubmit.mjs +0 -9
  64. package/dist/esm/form/useFormIsBusy.mjs +0 -8
  65. package/dist/esm/form/useRhfUtilsForm.mjs +0 -43
  66. package/dist/esm/form/utils/getSubmitterButtonData.mjs +0 -12
  67. package/dist/esm/form/utils/useFormRequestSubmit.mjs +0 -13
  68. package/dist/esm/submit/error/FormSubmitError.mjs +0 -8
  69. package/dist/esm/submit/error/setCtxErrorsByFormSubmitFieldErrors.mjs +0 -12
  70. package/dist/esm/submit/useFormOnSubmitted.mjs +0 -12
  71. package/dist/esm/submit/useResetFormOnSubmitted.mjs +0 -36
  72. package/dist/esm/submit/useSubmitFormOnChange.mjs +0 -26
  73. package/dist/esm/utils/PassthroughChildren.mjs +0 -7
  74. package/dist/esm/utils/isEmptyObject.mjs +0 -4
  75. package/dist/esm/utils/useDebouncedOnChangeValue.mjs +0 -26
  76. package/dist/esm/utils/useRefIfValueWasTrue.mjs +0 -14
  77. package/dist/types/errors/nonfield/FormNonFieldErrorMarkerHtmlAttribute.d.ts +0 -5
  78. /package/dist/types/errors/nonfield/{FormNonFieldErrorMarker.spec.d.ts → RhfUtilsNonFieldErrorMarker.spec.d.ts} +0 -0
package/README.md CHANGED
@@ -22,7 +22,6 @@ Integration and utility library for [react-hook-form](https://www.react-hook-for
22
22
  - schema-typed Controller component and FormSubmitError class
23
23
  - simpler/flatter `FieldErrors` structure (`FlatFieldErrors`)
24
24
  - context groups errors into `all`, `fields`, `roots`, and `orphans` (`useFlatFieldErrorsContext`)
25
- - `RhfUtilsFieldErrorMessage` for displaying error message
26
25
  - safer `FieldValues` type (`SafeFieldValues`)
27
26
  - `zod` support, including input/output types for transformations
28
27
  - ~3.6kB min+gzip (excluding [peer dependencies](#peer-dependencies))
@@ -57,7 +56,7 @@ export const rhfUtilsClientConfig: RhfUtilsClientConfig = {
57
56
  // RhfUtilsFormOptions (see further below)
58
57
  utils: {
59
58
  stopSubmitPropagation: true,
60
- devTool: import.meta.env.DEV,
59
+ devTool: import.meta.env.DEV && { placement: 'top-right' },
61
60
  },
62
61
 
63
62
  // default form props
@@ -189,7 +188,7 @@ Currently, only `zod` is supported.
189
188
  <label>
190
189
  Email
191
190
  <input {...field} disabled={isSubmitting} />
192
- <RhfUtilsFieldErrorMessage path={field.name} />
191
+ <ErrorMessage path={field.name} />
193
192
  </label>
194
193
  )}
195
194
  />
@@ -337,7 +336,7 @@ Programatically, an "orphan" is any form state error that meets ALL of the follo
337
336
 
338
337
  - non-field -- i.e., no RHF-supplied `ref` on `FieldError` object
339
338
  - non-root -- i.e., not `root` or `root.${string}` path
340
- - no corresponding "marker" in DOM (i.e., `FormNonFieldErrorMarker`)
339
+ - no corresponding "marker" in DOM (i.e., `RhfUtilsNonFieldErrorMarker`)
341
340
 
342
341
  This is because:
343
342
 
@@ -345,7 +344,7 @@ This is because:
345
344
  - root errors (non-field, without `ref`s) are assumed to always be listed for display
346
345
  - all other errors must be marked as displayed to distinguish from being an orphan
347
346
 
348
- _See [FormNonFieldErrorMarker](#formnonfielderrormarker) section below for more information about when and why marker is needed._
347
+ _See [RhfUtilsNonFieldErrorMarker](#rhfutilsnonfielderrormarker) section below for more information about when and why marker is needed._
349
348
 
350
349
  #### Using orphan errors
351
350
 
@@ -358,11 +357,11 @@ Detected orphans can be accessed via any of the following:
358
357
  - returns an object with list of errors grouped by `all`, `fields` (with `ref`), `roots`, `orphans` records, and includes computed booleans `hasErrors` and `hasOrphans`
359
358
  - boolean value from `useFlatFieldErrorsContextHasOnlyOrphans`
360
359
 
361
- ### FormNonFieldErrorMarker
360
+ ### RhfUtilsNonFieldErrorMarker
362
361
 
363
362
  _If you don't need orphan detection, you can skip this section. By default, orphan detection still occurs but doesn't otherwise do anything._
364
363
 
365
- To get accurate orphan detection, you must use `FormNonFieldErrorMarker` when displaying any non-root non-field errors. (Example further below.)
364
+ To get accurate orphan detection, you must use `RhfUtilsNonFieldErrorMarker` when displaying any non-root non-field errors. (Example further below.)
366
365
 
367
366
  There is little harm in including it consistently for all individual errors displayed. (DOM traversal to find marker only occurs if error is non-field AND non-root, which is not typical.)
368
367
 
@@ -372,21 +371,15 @@ A typical example is a field array with a required minimum number of items -- e.
372
371
 
373
372
  When there are zero items, the error is non-field and non-root. You would probably display this error (manually) near the field array, using something like RHF's [`ErrorMessage`](https://www.react-hook-form.com/api/useformstate/errormessage/) component.
374
373
 
375
- In order to NOT detect this as an orphan, it must be explicitly "marked" as displayed using `FormNonFieldErrorMarker`.
374
+ In order to NOT detect this as an orphan, it must be explicitly "marked" as displayed using `RhfUtilsNonFieldErrorMarker`.
376
375
 
377
- You can incorporate `FormNonFieldErrorMarker` into your own component library, or use this library's `RhfUtilsFieldErrorMessage` to display error message to user, which includes this marker.
376
+ You should incorporate `RhfUtilsNonFieldErrorMarker` into your own component library's error message display component.
378
377
 
379
378
  ```tsx
380
- <RhfUtilsFieldErrorMessage path="items" />
379
+ <RhfUtilsNonFieldErrorMarker path="items" />
381
380
  ```
382
381
 
383
- Uncommon, but if you are ONLY listing a summary of all errors, without individual displays, you will need to use the marker manually.
384
-
385
- ```tsx
386
- <FormNonFieldErrorMarker path="items" />
387
- ```
388
-
389
- _If you include the marker for error lists, it will defeat the purpose of the marker. Only use marker for individual errors._
382
+ _If you include the marker in bulk error lists, it will defeat the purpose of the marker. Only use marker for individual errors. Therefore, if you are ONLY listing a summary of all errors, without individual displays, you will need to use the marker manually._
390
383
 
391
384
  ## Form Groups
392
385
 
@@ -12,5 +12,5 @@ function i() {
12
12
  };
13
13
  }
14
14
  export {
15
- i as default
15
+ i as c
16
16
  };
@@ -1,46 +1,485 @@
1
- import { default as e } from "./client/zod/context/RhfUtilsClientForZodContextProvider.mjs";
2
- import { default as s } from "./client/zod/context/RhfUtilsZodForm.mjs";
3
- import { default as u } from "./client/zod/context/useRhfUtilsZodForm.mjs";
4
- import { default as m } from "./errors/flat/context/useFlatFieldErrorsContext.mjs";
5
- import { default as d } from "./errors/flat/context/useFlatFieldErrorsContextHasOnlyOrphans.mjs";
6
- import { default as x } from "./errors/message/RhfUtilsFieldErrorMessage.mjs";
7
- import { default as F } from "./errors/nonfield/FormNonFieldErrorMarker.mjs";
8
- import { default as C } from "./errors/root/RootErrorsListFromFlatFieldErrorsContext.mjs";
9
- import { default as R } from "./form/context/utils/useRhfUtilsContext.mjs";
10
- import { default as E } from "./form/context/utils/useRhfUtilsContextRequestSubmit.mjs";
11
- import { default as I } from "./form/context/group/FormGroupContextProvider.mjs";
12
- import { default as b } from "./form/context/group/useFormGroupChildIsMountedTracker.mjs";
13
- import { default as B } from "./form/context/group/useFormGroupChildIsSubmittingTracker.mjs";
14
- import { default as S } from "./form/context/group/useFormGroupChildTracker.mjs";
15
- import { default as T } from "./form/context/group/useFormGroupIsAnyBusy.mjs";
16
- import { default as M } from "./form/context/group/useFormGroupIsChildBusy.mjs";
17
- import { default as Z } from "./form/context/group/useFormGroupIsParentBusy.mjs";
18
- import { default as v } from "./form/context/group/useFormGroupParentTracker.mjs";
19
- import { default as D } from "./form/context/group/useFormOrParentIsBusy.mjs";
20
- import { default as L } from "./form/utils/getSubmitterButtonData.mjs";
21
- import { default as j } from "./form/utils/useFormRequestSubmit.mjs";
22
- import { FormSubmitError as z } from "./submit/error/FormSubmitError.mjs";
1
+ import { jsx as u, Fragment as $, jsxs as q } from "react/jsx-runtime";
2
+ import a from "react";
3
+ import { c as C } from "./createContext-Jzs0Nsql.js";
4
+ import { zodResolver as B } from "@hookform/resolvers/zod";
5
+ import { useFormContext as E, Controller as I, get as L, useFormState as S, FormProvider as z, useForm as H } from "react-hook-form";
6
+ import { flatten as A } from "flat";
7
+ const {
8
+ Provider: Q,
9
+ useMaybe: g
10
+ } = C(), G = a.lazy(
11
+ () => import("@hookform/devtools").then((r) => ({
12
+ default: r.DevTool
13
+ }))
14
+ ), J = ({ enabled: r, props: t }) => {
15
+ if (!r) return null;
16
+ const { control: e } = E();
17
+ return /* @__PURE__ */ u(a.Suspense, { fallback: null, children: /* @__PURE__ */ u(G, { control: e, ...t }) });
18
+ };
19
+ class F extends Error {
20
+ constructor(t, e) {
21
+ super(e), this.errors = t;
22
+ }
23
+ }
24
+ const K = (r, t) => {
25
+ Object.entries(t).forEach(([e, o]) => {
26
+ r.setError(e, {
27
+ type: "FormSubmitFieldError",
28
+ // default
29
+ ...o
30
+ });
31
+ });
32
+ }, X = ({
33
+ children: r
34
+ }) => /* @__PURE__ */ u($, { children: r });
35
+ function Y(r) {
36
+ return /* @__PURE__ */ u(I, { ...r });
37
+ }
38
+ function rr({
39
+ formId: r,
40
+ formRef: t,
41
+ //
42
+ onSubmit: e,
43
+ onSubmitError: o,
44
+ onSubmitInvalid: s,
45
+ Children: n,
46
+ //
47
+ form: i,
48
+ utils: l
49
+ }) {
50
+ const h = E(), d = g(), c = async (m) => {
51
+ l != null && l.stopSubmitPropagation && m.stopPropagation();
52
+ try {
53
+ await h.handleSubmit(v, s)(m);
54
+ } catch (f) {
55
+ R(f, m);
56
+ }
57
+ }, v = (m, f) => e == null ? void 0 : e(
58
+ m,
59
+ {
60
+ formId: r,
61
+ formRef: t,
62
+ context: h,
63
+ utils: l,
64
+ FormSubmitError: F
65
+ },
66
+ f
67
+ ), R = (m, f) => {
68
+ var O;
69
+ const P = m instanceof F ? (
70
+ // consumer can manually throw FormSubmitError (e.g., manual validation)
71
+ m.errors
72
+ ) : (
73
+ // if error is not FormSubmitError, consumer can provide global handler
74
+ (O = d == null ? void 0 : d.onSubmitErrorUnknown) == null ? void 0 : O.call(d, m)
75
+ );
76
+ P && K(h, P), o == null || o(
77
+ m,
78
+ {
79
+ formId: r,
80
+ formRef: t,
81
+ context: h,
82
+ utils: l
83
+ },
84
+ f
85
+ );
86
+ }, p = {
87
+ formId: r,
88
+ formRef: t,
89
+ context: h,
90
+ utils: l,
91
+ Controller: Y,
92
+ FormSubmitError: F
93
+ }, Z = p, D = (d == null ? void 0 : d.FormComponent) ?? "form", V = (d == null ? void 0 : d.FormChildrenWrapper) ?? X;
94
+ return /* @__PURE__ */ u(
95
+ D,
96
+ {
97
+ ...i,
98
+ ref: t,
99
+ onSubmit: (m) => void c(m),
100
+ children: /* @__PURE__ */ q(V, { ...Z, children: [
101
+ /* @__PURE__ */ u(
102
+ J,
103
+ {
104
+ enabled: !!(l != null && l.devTool),
105
+ props: typeof (l == null ? void 0 : l.devTool) == "object" ? l.devTool : void 0
106
+ }
107
+ ),
108
+ /* @__PURE__ */ u(n, { ...p })
109
+ ] })
110
+ }
111
+ );
112
+ }
113
+ const b = (r) => (t) => (
114
+ // re-create object from entries
115
+ Object.fromEntries(
116
+ // break down to entries
117
+ Object.entries(t).filter(r)
118
+ )
119
+ ), k = (r) => !!r.ref, er = ([, r]) => k(r), tr = b(
120
+ er
121
+ ), y = (r) => Object.keys(r).length === 0, _ = "data-rhfutils-nonfield-error-marker-path", or = (r, t) => (
122
+ // list of selector variants
123
+ [
124
+ // equals
125
+ // e.g., attr="path"
126
+ ["", ""],
127
+ [
128
+ // @see https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors#attrvalue_4
129
+ "^",
130
+ // only match nested (avoid match 'field' with 'field1`)
131
+ "."
132
+ ]
133
+ ].map(
134
+ ([e, o]) => sr(
135
+ _ + e,
136
+ r + o
137
+ )
138
+ ).join(", ")
139
+ ), sr = (r, t) => `[role="alert"][${r}="${t}"]`, nr = (r, t) => !!t.querySelector(
140
+ or(r)
141
+ ), U = "root", W = (r) => r === U || r.startsWith(U + "."), lr = (r, t, e) => (
142
+ // non-field
143
+ !k(t) && // non-root
144
+ !W(r) && // not marked in DOM
145
+ !nr(r, e)
146
+ ), ir = (r) => ([t, e]) => lr(t, e, r), ur = (r, t) => b(
147
+ ir(t)
148
+ )(r), dr = ([r]) => W(r), ar = b(
149
+ dr
150
+ ), hr = (r) => A(r), mr = (r) => {
151
+ const t = hr(r), e = Object.keys(t);
152
+ return Object.fromEntries(
153
+ e.reduce(
154
+ (o, s) => {
155
+ const n = s.replace(
156
+ cr,
157
+ ""
158
+ );
159
+ if (n === s) return o;
160
+ const i = L(r, n, void 0);
161
+ return i && o.push([n, i]), o;
162
+ },
163
+ []
164
+ // entries
165
+ )
166
+ );
167
+ }, cr = /\.(?:type|message)\b$/, {
168
+ Provider: fr,
169
+ useRequired: Fr
170
+ } = C(), j = Fr, Cr = ({
171
+ formRef: r,
172
+ children: t
173
+ }) => {
174
+ const { errors: e } = S(), o = mr(e), s = r.current ? ur(o, r.current) : {}, n = {
175
+ all: o,
176
+ fields: tr(o),
177
+ roots: ar(o),
178
+ orphans: s,
179
+ // computed
180
+ hasErrors: !y(e),
181
+ hasOrphans: !y(s)
182
+ };
183
+ return /* @__PURE__ */ u(fr, { value: n, children: t });
184
+ }, Er = (r, t, e, o = "debug") => {
185
+ console[o](r, { values: t, errors: e });
186
+ }, br = (r) => {
187
+ const t = E(), e = j();
188
+ a.useEffect(() => {
189
+ var n, i;
190
+ if (!e.hasErrors) return;
191
+ const o = (n = r == null ? void 0 : r.console) == null ? void 0 : n.call(r, e);
192
+ o && Er(
193
+ o.message ?? x,
194
+ t.getValues(),
195
+ e,
196
+ o.type
197
+ );
198
+ const s = (i = r == null ? void 0 : r.throw) == null ? void 0 : i.call(r, e);
199
+ if (s)
200
+ throw new Error(typeof s == "string" ? s : x);
201
+ }, [e]);
202
+ }, x = "Form errors", vr = (r) => {
203
+ const t = a.useRef(!1);
204
+ a.useEffect(() => {
205
+ r && (t.current = !0);
206
+ }, [r]);
207
+ const e = () => {
208
+ t.current = !1;
209
+ };
210
+ return [t.current, e];
211
+ }, Rr = (r, t) => {
212
+ const { isSubmitSuccessful: e, isSubmitting: o } = S(), [s, n] = vr(o);
213
+ a.useEffect(() => {
214
+ r && (o || s && (n(), r(e)));
215
+ }, [o]);
216
+ }, pr = (r) => {
217
+ const { getValues: t, reset: e } = E();
218
+ Rr((o) => {
219
+ var l;
220
+ if (!r) return;
221
+ const s = (l = r[o ? "success" : "error"]) == null ? void 0 : l.values;
222
+ if (!s) return;
223
+ const n = s === "current", i = n ? t() : void 0;
224
+ e(i, {
225
+ // keep submit stuff for tracking relevant state (e.g., `useFormOnSubmitSuccessful`)
226
+ keepIsSubmitSuccessful: !0,
227
+ keepIsSubmitted: !0,
228
+ keepSubmitCount: !0,
229
+ /**
230
+ * This is necessary when setting default values to current values (lest form stop updating).
231
+ *
232
+ * Technically, we are keeping values and resetting default values to same values.
233
+ *
234
+ * Steps to repro:
235
+ * - RhfUtilsClientConfig with resetOnSubmitted set to success values to current
236
+ * - remove `keepValues` prop from reset
237
+ * - create valid form, submit
238
+ * - form values should be unchanged
239
+ * - unexpectedly: can no longer type into input
240
+ */
241
+ keepValues: n,
242
+ // keep errors in the case of unsuccessful submit
243
+ keepErrors: !0
244
+ });
245
+ });
246
+ }, Pr = (r) => a.useCallback(
247
+ (t) => {
248
+ if (!r.current) throw new Error();
249
+ Or(r.current, t);
250
+ },
251
+ [r]
252
+ ), Or = (r, t) => {
253
+ r.requestSubmit(t);
254
+ }, yr = ({
255
+ onChange: r,
256
+ delay: t
257
+ }) => {
258
+ const e = a.useRef(), o = () => {
259
+ e.current && (r(e.current.value), e.current = void 0);
260
+ }, s = () => {
261
+ e.current && clearTimeout(e.current.timer);
262
+ };
263
+ return a.useEffect(
264
+ // return timeout clearer
265
+ () => s,
266
+ []
267
+ ), [(i) => {
268
+ s(), e.current = {
269
+ // value being tracked
270
+ value: i,
271
+ // new timer
272
+ timer: setTimeout(o, t)
273
+ };
274
+ }];
275
+ }, Ur = (r, t) => {
276
+ const e = !(t != null && t.disabled), o = Pr(r), [s] = yr({
277
+ // on change value after debounce
278
+ onChange: () => {
279
+ o();
280
+ },
281
+ delay: (t == null ? void 0 : t.debounce) ?? 0
282
+ });
283
+ a.useEffect(() => {
284
+ if (!e) return;
285
+ const n = r.current;
286
+ if (!n) throw new Error();
287
+ const i = (l) => {
288
+ s(l);
289
+ };
290
+ return n.addEventListener("change", i), () => {
291
+ n.removeEventListener("change", i);
292
+ };
293
+ }, []);
294
+ }, { Provider: xr, useRequired: Sr } = C(), gr = Sr, Hr = gr, kr = ({
295
+ formId: r,
296
+ formRef: t,
297
+ options: e,
298
+ children: o
299
+ }) => {
300
+ var n;
301
+ const s = g();
302
+ return br((n = s == null ? void 0 : s.fieldErrors) == null ? void 0 : n.output), Ur(t, {
303
+ // check for false/undefined
304
+ disabled: ((e == null ? void 0 : e.submitOnChange) ?? !1) === !1,
305
+ debounce: typeof (e == null ? void 0 : e.submitOnChange) == "number" ? e.submitOnChange : void 0
306
+ }), pr(e == null ? void 0 : e.resetOnSubmitted), /* @__PURE__ */ u(xr, { value: { formId: r, formRef: t, options: e }, children: o });
307
+ };
308
+ function _r({
309
+ formId: r,
310
+ formRef: t,
311
+ rhfForm: e,
312
+ utils: o,
313
+ children: s
314
+ }) {
315
+ return /* @__PURE__ */ u(z, { ...e, children: /* @__PURE__ */ u(Cr, { formRef: t, children: /* @__PURE__ */ u(
316
+ kr,
317
+ {
318
+ formId: r,
319
+ formRef: t,
320
+ options: o,
321
+ children: s
322
+ }
323
+ ) }) });
324
+ }
325
+ const w = ({
326
+ defaultValues: r,
327
+ onSubmit: t,
328
+ onSubmitError: e,
329
+ onSubmitInvalid: o,
330
+ Children: s,
331
+ rhf: n,
332
+ form: i,
333
+ utils: l
334
+ }) => {
335
+ const h = a.useId(), d = H({
336
+ ...n,
337
+ defaultValues: r
338
+ }), c = a.useRef(null);
339
+ return {
340
+ id: h,
341
+ ref: c,
342
+ context: d,
343
+ providerProps: {
344
+ formId: h,
345
+ formRef: c,
346
+ rhfForm: d,
347
+ utils: l
348
+ },
349
+ formProps: {
350
+ Children: s,
351
+ onSubmit: t,
352
+ onSubmitError: e,
353
+ onSubmitInvalid: o,
354
+ formId: h,
355
+ formRef: c,
356
+ form: i,
357
+ utils: l
358
+ },
359
+ FormSubmitError: F
360
+ };
361
+ };
362
+ function M(r) {
363
+ const t = w(r);
364
+ return /* @__PURE__ */ u(_r, { ...t.providerProps, children: /* @__PURE__ */ u(rr, { ...t.formProps }) });
365
+ }
366
+ const Wr = (r, ...t) => (
367
+ // sequentially merge each set of props into defaults
368
+ t.reduce(
369
+ (e, o) => {
370
+ var s, n, i, l, h, d;
371
+ return {
372
+ rhf: {
373
+ ...e == null ? void 0 : e.rhf,
374
+ ...o.rhf
375
+ },
376
+ utils: {
377
+ ...e == null ? void 0 : e.utils,
378
+ ...o.utils,
379
+ // manual merges
380
+ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- false positive
381
+ ...(((s = e == null ? void 0 : e.utils) == null ? void 0 : s.resetOnSubmitted) || ((n = o.utils) == null ? void 0 : n.resetOnSubmitted)) && {
382
+ resetOnSubmitted: {
383
+ ...(i = e == null ? void 0 : e.utils) == null ? void 0 : i.resetOnSubmitted,
384
+ ...(l = o.utils) == null ? void 0 : l.resetOnSubmitted
385
+ }
386
+ }
387
+ },
388
+ form: {
389
+ ...e == null ? void 0 : e.form,
390
+ ...o.form,
391
+ // manual merges
392
+ className: [(h = e == null ? void 0 : e.form) == null ? void 0 : h.className, (d = o.form) == null ? void 0 : d.className].filter(Boolean).join(" ") || void 0
393
+ // stringify (replace empty string with undefined)
394
+ }
395
+ };
396
+ },
397
+ // start with defaults
398
+ r
399
+ )
400
+ ), jr = (r) => (t) => {
401
+ const e = T(r, t);
402
+ return {
403
+ useForm: (o, s) => {
404
+ const n = e(o, s);
405
+ return {
406
+ ...w(n),
407
+ FormWithProviders: () => /* @__PURE__ */ u(M, { ...n })
408
+ };
409
+ },
410
+ FormWithProviders: wr(r, t)
411
+ };
412
+ }, T = (r, t) => function(o, s) {
413
+ return {
414
+ ...s,
415
+ ...Wr(
416
+ t.defaults,
417
+ s,
418
+ {
419
+ rhf: {
420
+ resolver: r(o)
421
+ }
422
+ }
423
+ )
424
+ };
425
+ }, wr = (r, t) => {
426
+ const e = T(r, t);
427
+ return function({
428
+ schema: s,
429
+ children: n,
430
+ ...i
431
+ }) {
432
+ return /* @__PURE__ */ u(
433
+ M,
434
+ {
435
+ ...e(s, i),
436
+ children: n
437
+ }
438
+ );
439
+ };
440
+ }, Mr = jr(B), {
441
+ Provider: Tr,
442
+ useRequired: N
443
+ } = C(), Nr = ({
444
+ config: r,
445
+ children: t
446
+ }) => {
447
+ const [e] = a.useState(r), [o] = a.useState(
448
+ () => Mr(e ?? {})
449
+ );
450
+ return /* @__PURE__ */ u(Q, { value: e, children: /* @__PURE__ */ u(Tr, { value: o, children: t }) });
451
+ }, Ar = Nr;
452
+ function Qr({
453
+ schema: r,
454
+ ...t
455
+ }) {
456
+ const { FormWithProviders: e } = N();
457
+ return /* @__PURE__ */ u(e, { schema: r, ...t });
458
+ }
459
+ const Zr = (r, t) => N().useForm(r, t), Gr = Zr, Dr = () => {
460
+ const r = j();
461
+ return a.useMemo(() => !r.hasErrors || !r.hasOrphans ? !1 : Object.keys(r.orphans).length === Object.keys(r.all).length, [r]);
462
+ }, Jr = Dr, Vr = ({ path: r }) => {
463
+ const t = {
464
+ [_]: r
465
+ };
466
+ return /* @__PURE__ */ u(
467
+ "div",
468
+ {
469
+ role: "alert",
470
+ "aria-hidden": "true",
471
+ style: { display: "none" },
472
+ ...t
473
+ }
474
+ );
475
+ }, Kr = Vr;
23
476
  export {
24
- I as FormGroupContextProvider,
25
- F as FormNonFieldErrorMarker,
26
- z as FormSubmitError,
27
- e as RhfUtilsClientForZodContextProvider,
28
- x as RhfUtilsFieldErrorMessage,
29
- s as RhfUtilsZodForm,
30
- C as RootErrorsListFromFlatFieldErrorsContext,
31
- L as getSubmitterButtonData,
32
- m as useFlatFieldErrorsContext,
33
- d as useFlatFieldErrorsContextHasOnlyOrphans,
34
- b as useFormGroupChildIsMountedTracker,
35
- B as useFormGroupChildIsSubmittingTracker,
36
- S as useFormGroupChildTracker,
37
- T as useFormGroupIsAnyBusy,
38
- M as useFormGroupIsChildBusy,
39
- Z as useFormGroupIsParentBusy,
40
- v as useFormGroupParentTracker,
41
- D as useFormOrParentIsBusy,
42
- j as useFormRequestSubmit,
43
- R as useRhfUtilsContext,
44
- E as useRhfUtilsContextRequestSubmit,
45
- u as useRhfUtilsZodForm
477
+ F as FormSubmitError,
478
+ Ar as RhfUtilsClientForZodContextProvider,
479
+ Kr as RhfUtilsNonFieldErrorMarker,
480
+ Qr as RhfUtilsZodForm,
481
+ j as useFlatFieldErrorsContext,
482
+ Jr as useFlatFieldErrorsContextHasOnlyOrphans,
483
+ Hr as useRhfUtilsContext,
484
+ Gr as useRhfUtilsZodForm
46
485
  };
@@ -0,0 +1,74 @@
1
+ import { jsx as C } from "react/jsx-runtime";
2
+ import o from "react";
3
+ import { c as G } from "../../../createContext-Jzs0Nsql.js";
4
+ import { useFormState as c } from "react-hook-form";
5
+ const {
6
+ Provider: h,
7
+ useRequired: i,
8
+ useMaybe: u
9
+ } = G(), P = ({ children: s }) => {
10
+ const [r, e] = o.useState([]), y = r.length, p = o.useCallback((t, B) => {
11
+ e(
12
+ (n) => B ? (
13
+ // add to array
14
+ [...n, t]
15
+ ) : (
16
+ // remove from array
17
+ n.filter((I) => I !== t)
18
+ )
19
+ );
20
+ }, []), [F, l] = o.useState(!1);
21
+ return /* @__PURE__ */ C(h, { value: {
22
+ children: {
23
+ busyCount: y,
24
+ busyIds: r,
25
+ setBusyId: p
26
+ },
27
+ parent: {
28
+ isBusy: F,
29
+ setIsBusy: l
30
+ }
31
+ }, children: s });
32
+ }, _ = P, f = (s) => {
33
+ const { setBusyId: r } = i().children, e = o.useId();
34
+ o.useEffect(() => {
35
+ r(e, s);
36
+ }, [s, e, r]), o.useEffect(() => () => {
37
+ r(e, !1);
38
+ }, []);
39
+ }, a = f, m = () => {
40
+ const s = u();
41
+ return !!(s != null && s.parent.isBusy);
42
+ }, b = () => (a(!0), m()), R = b, k = () => {
43
+ const { isSubmitting: s } = c();
44
+ return a(s), m();
45
+ }, j = k, T = () => {
46
+ const s = u();
47
+ return !!(s != null && s.children.busyCount) || !!(s != null && s.parent.isBusy);
48
+ }, q = T, S = () => {
49
+ const s = u();
50
+ return !!(s != null && s.children.busyCount);
51
+ }, $ = S, d = () => {
52
+ const { isSubmitting: s, isLoading: r } = c();
53
+ return s || r;
54
+ }, g = () => {
55
+ const s = d(), { setIsBusy: r } = i().parent;
56
+ return o.useEffect(() => {
57
+ r(s);
58
+ }, [r, s]), $();
59
+ }, L = g, v = () => {
60
+ var e;
61
+ const s = d(), r = (e = u()) == null ? void 0 : e.parent.isBusy;
62
+ return !!(s || r);
63
+ }, w = v;
64
+ export {
65
+ _ as FormGroupContextProvider,
66
+ R as useFormGroupChildIsMountedTracker,
67
+ j as useFormGroupChildIsSubmittingTracker,
68
+ a as useFormGroupChildTracker,
69
+ q as useFormGroupIsAnyBusy,
70
+ $ as useFormGroupIsChildBusy,
71
+ m as useFormGroupIsParentBusy,
72
+ L as useFormGroupParentTracker,
73
+ w as useFormOrParentIsBusy
74
+ };