@paragrav/rhf-utils 0.0.138 → 0.0.140

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