@paragrav/rhf-utils 0.0.139 → 0.0.141

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
@@ -24,7 +24,7 @@ Integration and utility library for [react-hook-form](https://www.react-hook-for
24
24
  - context groups errors into `all`, `fields`, `roots`, and `orphans` (`useFlatFieldErrorsContext`)
25
25
  - safer `FieldValues` type (`SafeFieldValues`)
26
26
  - `zod` support, including input/output types for transformations
27
- - ~3.6kB min+gzip (excluding [peer dependencies](#peer-dependencies))
27
+ - ~3.2kB min+gzip core (excluding [peer dependencies](#peer-dependencies))
28
28
 
29
29
  ## Install
30
30
 
@@ -381,7 +381,9 @@ You should incorporate `RhfUtilsNonFieldErrorMarker` into your own component lib
381
381
 
382
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._
383
383
 
384
- ## Form Groups
384
+ ## Other Features
385
+
386
+ ### Form Groups _(0.65kB min+gzip)_
385
387
 
386
388
  Sometimes you need to group multiple forms together.
387
389
 
@@ -406,6 +408,22 @@ In children forms, there are two options. You can choose to consider form "busy"
406
408
 
407
409
  Both hooks return `boolean` value indicating whether parent form is busy.
408
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
+
409
427
  ## Other
410
428
 
411
429
  ### SafeFieldValues
@@ -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
+ };
@@ -1,27 +1,28 @@
1
- import { jsx as u, Fragment as $, jsxs as q } from "react/jsx-runtime";
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 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";
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";
7
8
  const {
8
- Provider: Q,
9
- useMaybe: g
10
- } = C(), G = a.lazy(
9
+ Provider: G,
10
+ useMaybe: _
11
+ } = E(), J = a.lazy(
11
12
  () => import("@hookform/devtools").then((r) => ({
12
13
  default: r.DevTool
13
14
  }))
14
- ), J = ({ enabled: r, props: t }) => {
15
+ ), K = ({ enabled: r, props: t }) => {
15
16
  if (!r) return null;
16
- const { control: e } = E();
17
- return /* @__PURE__ */ u(a.Suspense, { fallback: null, children: /* @__PURE__ */ u(G, { control: e, ...t }) });
17
+ const { control: e } = b();
18
+ return /* @__PURE__ */ i(a.Suspense, { fallback: null, children: /* @__PURE__ */ i(J, { control: e, ...t }) });
18
19
  };
19
20
  class F extends Error {
20
21
  constructor(t, e) {
21
22
  super(e), this.errors = t;
22
23
  }
23
24
  }
24
- const K = (r, t) => {
25
+ const X = (r, t) => {
25
26
  Object.entries(t).forEach(([e, o]) => {
26
27
  r.setError(e, {
27
28
  type: "FormSubmitFieldError",
@@ -29,13 +30,13 @@ const K = (r, t) => {
29
30
  ...o
30
31
  });
31
32
  });
32
- }, X = ({
33
+ }, Y = ({
33
34
  children: r
34
- }) => /* @__PURE__ */ u($, { children: r });
35
- function Y(r) {
36
- return /* @__PURE__ */ u(I, { ...r });
35
+ }) => /* @__PURE__ */ i(B, { children: r });
36
+ function rr(r) {
37
+ return /* @__PURE__ */ i(L, { ...r });
37
38
  }
38
- function rr({
39
+ function er({
39
40
  formId: r,
40
41
  formRef: t,
41
42
  //
@@ -44,81 +45,81 @@ function rr({
44
45
  onSubmitInvalid: s,
45
46
  Children: n,
46
47
  //
47
- form: i,
48
- utils: l
48
+ form: l,
49
+ utils: u
49
50
  }) {
50
- const h = E(), d = g(), c = async (m) => {
51
- l != null && l.stopSubmitPropagation && m.stopPropagation();
51
+ const m = b(), d = _(), c = async (h) => {
52
+ u != null && u.stopSubmitPropagation && h.stopPropagation();
52
53
  try {
53
- await h.handleSubmit(v, s)(m);
54
+ await m.handleSubmit(v, s)(h);
54
55
  } catch (f) {
55
- R(f, m);
56
+ p(f, h);
56
57
  }
57
- }, v = (m, f) => e == null ? void 0 : e(
58
- m,
58
+ }, v = (h, f) => e == null ? void 0 : e(
59
+ h,
59
60
  {
60
61
  formId: r,
61
62
  formRef: t,
62
- context: h,
63
- utils: l,
63
+ context: m,
64
+ utils: u,
64
65
  FormSubmitError: F
65
66
  },
66
67
  f
67
- ), R = (m, f) => {
68
- var O;
69
- const P = m instanceof F ? (
68
+ ), p = (h, f) => {
69
+ var y;
70
+ const R = h instanceof F ? (
70
71
  // consumer can manually throw FormSubmitError (e.g., manual validation)
71
- m.errors
72
+ h.errors
72
73
  ) : (
73
74
  // 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
+ (y = d == null ? void 0 : d.onSubmitErrorUnknown) == null ? void 0 : y.call(d, h)
75
76
  );
76
- P && K(h, P), o == null || o(
77
- m,
77
+ R && X(m, R), o == null || o(
78
+ h,
78
79
  {
79
80
  formId: r,
80
81
  formRef: t,
81
- context: h,
82
- utils: l
82
+ context: m,
83
+ utils: u
83
84
  },
84
85
  f
85
86
  );
86
- }, p = {
87
+ }, P = {
87
88
  formId: r,
88
89
  formRef: t,
89
- context: h,
90
- utils: l,
91
- Controller: Y,
90
+ context: m,
91
+ utils: u,
92
+ Controller: rr,
92
93
  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,
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,
96
97
  {
97
- ...i,
98
+ ...l,
98
99
  ref: t,
99
- onSubmit: (m) => void c(m),
100
- children: /* @__PURE__ */ q(V, { ...Z, children: [
101
- /* @__PURE__ */ u(
102
- J,
100
+ onSubmit: (h) => void c(h),
101
+ children: /* @__PURE__ */ Z(q, { ...V, children: [
102
+ /* @__PURE__ */ i(
103
+ K,
103
104
  {
104
- enabled: !!(l != null && l.devTool),
105
- props: typeof (l == null ? void 0 : l.devTool) == "object" ? l.devTool : void 0
105
+ enabled: !!(u != null && u.devTool),
106
+ props: typeof (u == null ? void 0 : u.devTool) == "object" ? u.devTool : void 0
106
107
  }
107
108
  ),
108
- /* @__PURE__ */ u(n, { ...p })
109
+ /* @__PURE__ */ i(n, { ...P })
109
110
  ] })
110
111
  }
111
112
  );
112
113
  }
113
- const b = (r) => (t) => (
114
+ const C = (r) => (t) => (
114
115
  // re-create object from entries
115
116
  Object.fromEntries(
116
117
  // break down to entries
117
118
  Object.entries(t).filter(r)
118
119
  )
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) => (
120
+ ), k = (r) => !!r.ref, tr = ([, r]) => k(r), or = C(
121
+ tr
122
+ ), W = "data-rhfutils-nonfield-error-marker-path", sr = (r, t) => (
122
123
  // list of selector variants
123
124
  [
124
125
  // equals
@@ -131,75 +132,72 @@ const b = (r) => (t) => (
131
132
  "."
132
133
  ]
133
134
  ].map(
134
- ([e, o]) => sr(
135
- _ + e,
135
+ ([e, o]) => nr(
136
+ W + e,
136
137
  r + o
137
138
  )
138
139
  ).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) => (
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) => (
142
143
  // non-field
143
144
  !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(
145
+ !j(r) && // not marked in DOM
146
+ !ur(r, e)
147
+ ), ir = (r) => ([t, e]) => lr(t, e, r), dr = (r, t) => C(
147
148
  ir(t)
148
- )(r), dr = ([r]) => W(r), ar = b(
149
- dr
150
- ), hr = (r) => A(r), mr = (r) => {
149
+ )(r), ar = ([r]) => j(r), mr = C(
150
+ ar
151
+ ), hr = (r) => Q(r), cr = (r) => {
151
152
  const t = hr(r), e = Object.keys(t);
152
153
  return Object.fromEntries(
153
154
  e.reduce(
154
155
  (o, s) => {
155
156
  const n = s.replace(
156
- cr,
157
+ fr,
157
158
  ""
158
159
  );
159
160
  if (n === s) return o;
160
- const i = L(r, n, void 0);
161
- return i && o.push([n, i]), o;
161
+ const l = z(r, n, void 0);
162
+ return l && o.push([n, l]), o;
162
163
  },
163
164
  []
164
165
  // entries
165
166
  )
166
167
  );
167
- }, cr = /\.(?:type|message)\b$/, {
168
- Provider: fr,
169
- useRequired: Fr
170
- } = C(), j = Fr, Cr = ({
168
+ }, fr = /\.(?:type|message)\b$/, Fr = ({
171
169
  formRef: r,
172
170
  children: t
173
171
  }) => {
174
- const { errors: e } = S(), o = mr(e), s = r.current ? ur(o, r.current) : {}, n = {
172
+ const { errors: e } = x(), o = cr(e), s = r.current ? dr(o, r.current) : {}, n = {
175
173
  all: o,
176
- fields: tr(o),
177
- roots: ar(o),
174
+ fields: or(o),
175
+ roots: mr(o),
178
176
  orphans: s,
179
177
  // computed
180
- hasErrors: !y(e),
181
- hasOrphans: !y(s)
178
+ hasErrors: !O(e),
179
+ hasOrphans: !O(s)
182
180
  };
183
- return /* @__PURE__ */ u(fr, { value: n, children: t });
184
- }, Er = (r, t, e, o = "debug") => {
181
+ return /* @__PURE__ */ i(H, { value: n, children: t });
182
+ }, br = (r, t, e, o = "debug") => {
185
183
  console[o](r, { values: t, errors: e });
186
- }, br = (r) => {
187
- const t = E(), e = j();
184
+ }, Er = (r) => {
185
+ const t = b(), e = g();
188
186
  a.useEffect(() => {
189
- var n, i;
187
+ var n, l;
190
188
  if (!e.hasErrors) return;
191
189
  const o = (n = r == null ? void 0 : r.console) == null ? void 0 : n.call(r, e);
192
- o && Er(
193
- o.message ?? x,
190
+ o && br(
191
+ o.message ?? U,
194
192
  t.getValues(),
195
193
  e,
196
194
  o.type
197
195
  );
198
- const s = (i = r == null ? void 0 : r.throw) == null ? void 0 : i.call(r, e);
196
+ const s = (l = r == null ? void 0 : r.throw) == null ? void 0 : l.call(r, e);
199
197
  if (s)
200
- throw new Error(typeof s == "string" ? s : x);
198
+ throw new Error(typeof s == "string" ? s : U);
201
199
  }, [e]);
202
- }, x = "Form errors", vr = (r) => {
200
+ }, U = "Form errors", Cr = (r) => {
203
201
  const t = a.useRef(!1);
204
202
  a.useEffect(() => {
205
203
  r && (t.current = !0);
@@ -208,20 +206,20 @@ const b = (r) => (t) => (
208
206
  t.current = !1;
209
207
  };
210
208
  return [t.current, e];
211
- }, Rr = (r, t) => {
212
- const { isSubmitSuccessful: e, isSubmitting: o } = S(), [s, n] = vr(o);
209
+ }, vr = (r, t) => {
210
+ const { isSubmitSuccessful: e, isSubmitting: o } = x(), [s, n] = Cr(o);
213
211
  a.useEffect(() => {
214
212
  r && (o || s && (n(), r(e)));
215
213
  }, [o]);
216
214
  }, pr = (r) => {
217
- const { getValues: t, reset: e } = E();
218
- Rr((o) => {
219
- var l;
215
+ const { getValues: t, reset: e } = b();
216
+ vr((o) => {
217
+ var u;
220
218
  if (!r) return;
221
- const s = (l = r[o ? "success" : "error"]) == null ? void 0 : l.values;
219
+ const s = (u = r[o ? "success" : "error"]) == null ? void 0 : u.values;
222
220
  if (!s) return;
223
- const n = s === "current", i = n ? t() : void 0;
224
- e(i, {
221
+ const n = s === "current", l = n ? t() : void 0;
222
+ e(l, {
225
223
  // keep submit stuff for tracking relevant state (e.g., `useFormOnSubmitSuccessful`)
226
224
  keepIsSubmitSuccessful: !0,
227
225
  keepIsSubmitted: !0,
@@ -246,10 +244,10 @@ const b = (r) => (t) => (
246
244
  }, Pr = (r) => a.useCallback(
247
245
  (t) => {
248
246
  if (!r.current) throw new Error();
249
- Or(r.current, t);
247
+ Rr(r.current, t);
250
248
  },
251
249
  [r]
252
- ), Or = (r, t) => {
250
+ ), Rr = (r, t) => {
253
251
  r.requestSubmit(t);
254
252
  }, yr = ({
255
253
  onChange: r,
@@ -264,15 +262,15 @@ const b = (r) => (t) => (
264
262
  // return timeout clearer
265
263
  () => s,
266
264
  []
267
- ), [(i) => {
265
+ ), [(l) => {
268
266
  s(), e.current = {
269
267
  // value being tracked
270
- value: i,
268
+ value: l,
271
269
  // new timer
272
270
  timer: setTimeout(o, t)
273
271
  };
274
272
  }];
275
- }, Ur = (r, t) => {
273
+ }, Or = (r, t) => {
276
274
  const e = !(t != null && t.disabled), o = Pr(r), [s] = yr({
277
275
  // on change value after debounce
278
276
  onChange: () => {
@@ -284,36 +282,36 @@ const b = (r) => (t) => (
284
282
  if (!e) return;
285
283
  const n = r.current;
286
284
  if (!n) throw new Error();
287
- const i = (l) => {
288
- s(l);
285
+ const l = (u) => {
286
+ s(u);
289
287
  };
290
- return n.addEventListener("change", i), () => {
291
- n.removeEventListener("change", i);
288
+ return n.addEventListener("change", l), () => {
289
+ n.removeEventListener("change", l);
292
290
  };
293
291
  }, []);
294
- }, { Provider: xr, useRequired: Sr } = C(), gr = Sr, Hr = gr, kr = ({
292
+ }, { Provider: Sr, useRequired: Ur } = E(), Zr = Ur, xr = ({
295
293
  formId: r,
296
294
  formRef: t,
297
295
  options: e,
298
296
  children: o
299
297
  }) => {
300
298
  var n;
301
- const s = g();
302
- return br((n = s == null ? void 0 : s.fieldErrors) == null ? void 0 : n.output), Ur(t, {
299
+ const s = _();
300
+ return Er((n = s == null ? void 0 : s.fieldErrors) == null ? void 0 : n.output), Or(t, {
303
301
  // check for false/undefined
304
302
  disabled: ((e == null ? void 0 : e.submitOnChange) ?? !1) === !1,
305
303
  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 });
304
+ }), pr(e == null ? void 0 : e.resetOnSubmitted), /* @__PURE__ */ i(Sr, { value: { formId: r, formRef: t, options: e }, children: o });
307
305
  };
308
- function _r({
306
+ function gr({
309
307
  formId: r,
310
308
  formRef: t,
311
309
  rhfForm: e,
312
310
  utils: o,
313
311
  children: s
314
312
  }) {
315
- return /* @__PURE__ */ u(z, { ...e, children: /* @__PURE__ */ u(Cr, { formRef: t, children: /* @__PURE__ */ u(
316
- kr,
313
+ return /* @__PURE__ */ i(A, { ...e, children: /* @__PURE__ */ i(Fr, { formRef: t, children: /* @__PURE__ */ i(
314
+ xr,
317
315
  {
318
316
  formId: r,
319
317
  formRef: t,
@@ -329,45 +327,45 @@ const w = ({
329
327
  onSubmitInvalid: o,
330
328
  Children: s,
331
329
  rhf: n,
332
- form: i,
333
- utils: l
330
+ form: l,
331
+ utils: u
334
332
  }) => {
335
- const h = a.useId(), d = H({
333
+ const m = a.useId(), d = $({
336
334
  ...n,
337
335
  defaultValues: r
338
336
  }), c = a.useRef(null);
339
337
  return {
340
- id: h,
338
+ id: m,
341
339
  ref: c,
342
340
  context: d,
343
341
  providerProps: {
344
- formId: h,
342
+ formId: m,
345
343
  formRef: c,
346
344
  rhfForm: d,
347
- utils: l
345
+ utils: u
348
346
  },
349
347
  formProps: {
350
348
  Children: s,
351
349
  onSubmit: t,
352
350
  onSubmitError: e,
353
351
  onSubmitInvalid: o,
354
- formId: h,
352
+ formId: m,
355
353
  formRef: c,
356
- form: i,
357
- utils: l
354
+ form: l,
355
+ utils: u
358
356
  },
359
357
  FormSubmitError: F
360
358
  };
361
359
  };
362
- function M(r) {
360
+ function T(r) {
363
361
  const t = w(r);
364
- return /* @__PURE__ */ u(_r, { ...t.providerProps, children: /* @__PURE__ */ u(rr, { ...t.formProps }) });
362
+ return /* @__PURE__ */ i(gr, { ...t.providerProps, children: /* @__PURE__ */ i(er, { ...t.formProps }) });
365
363
  }
366
- const Wr = (r, ...t) => (
364
+ const _r = (r, ...t) => (
367
365
  // sequentially merge each set of props into defaults
368
366
  t.reduce(
369
367
  (e, o) => {
370
- var s, n, i, l, h, d;
368
+ var s, n, l, u, m, d;
371
369
  return {
372
370
  rhf: {
373
371
  ...e == null ? void 0 : e.rhf,
@@ -380,8 +378,8 @@ const Wr = (r, ...t) => (
380
378
  // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- false positive
381
379
  ...(((s = e == null ? void 0 : e.utils) == null ? void 0 : s.resetOnSubmitted) || ((n = o.utils) == null ? void 0 : n.resetOnSubmitted)) && {
382
380
  resetOnSubmitted: {
383
- ...(i = e == null ? void 0 : e.utils) == null ? void 0 : i.resetOnSubmitted,
384
- ...(l = o.utils) == null ? void 0 : l.resetOnSubmitted
381
+ ...(l = e == null ? void 0 : e.utils) == null ? void 0 : l.resetOnSubmitted,
382
+ ...(u = o.utils) == null ? void 0 : u.resetOnSubmitted
385
383
  }
386
384
  }
387
385
  },
@@ -389,7 +387,7 @@ const Wr = (r, ...t) => (
389
387
  ...e == null ? void 0 : e.form,
390
388
  ...o.form,
391
389
  // 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
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
393
391
  // stringify (replace empty string with undefined)
394
392
  }
395
393
  };
@@ -397,22 +395,22 @@ const Wr = (r, ...t) => (
397
395
  // start with defaults
398
396
  r
399
397
  )
400
- ), jr = (r) => (t) => {
401
- const e = T(r, t);
398
+ ), kr = (r) => (t) => {
399
+ const e = M(r, t);
402
400
  return {
403
401
  useForm: (o, s) => {
404
402
  const n = e(o, s);
405
403
  return {
406
404
  ...w(n),
407
- FormWithProviders: () => /* @__PURE__ */ u(M, { ...n })
405
+ FormWithProviders: () => /* @__PURE__ */ i(T, { ...n })
408
406
  };
409
407
  },
410
- FormWithProviders: wr(r, t)
408
+ FormWithProviders: Wr(r, t)
411
409
  };
412
- }, T = (r, t) => function(o, s) {
410
+ }, M = (r, t) => function(o, s) {
413
411
  return {
414
412
  ...s,
415
- ...Wr(
413
+ ..._r(
416
414
  t.defaults,
417
415
  s,
418
416
  {
@@ -422,48 +420,48 @@ const Wr = (r, ...t) => (
422
420
  }
423
421
  )
424
422
  };
425
- }, wr = (r, t) => {
426
- const e = T(r, t);
423
+ }, Wr = (r, t) => {
424
+ const e = M(r, t);
427
425
  return function({
428
426
  schema: s,
429
427
  children: n,
430
- ...i
428
+ ...l
431
429
  }) {
432
- return /* @__PURE__ */ u(
433
- M,
430
+ return /* @__PURE__ */ i(
431
+ T,
434
432
  {
435
- ...e(s, i),
433
+ ...e(s, l),
436
434
  children: n
437
435
  }
438
436
  );
439
437
  };
440
- }, Mr = jr(B), {
441
- Provider: Tr,
442
- useRequired: N
443
- } = C(), Nr = ({
438
+ }, jr = kr(I), {
439
+ Provider: wr,
440
+ useRequired: D
441
+ } = E(), Ir = ({
444
442
  config: r,
445
443
  children: t
446
444
  }) => {
447
445
  const [e] = a.useState(r), [o] = a.useState(
448
- () => Mr(e ?? {})
446
+ () => jr(e ?? {})
449
447
  );
450
- return /* @__PURE__ */ u(Q, { value: e, children: /* @__PURE__ */ u(Tr, { value: o, children: t }) });
451
- }, Ar = Nr;
452
- function Qr({
448
+ return /* @__PURE__ */ i(G, { value: e, children: /* @__PURE__ */ i(wr, { value: o, children: t }) });
449
+ };
450
+ function Lr({
453
451
  schema: r,
454
452
  ...t
455
453
  }) {
456
- const { FormWithProviders: e } = N();
457
- return /* @__PURE__ */ u(e, { schema: r, ...t });
454
+ const { FormWithProviders: e } = D();
455
+ return /* @__PURE__ */ i(e, { schema: r, ...t });
458
456
  }
459
- const Zr = (r, t) => N().useForm(r, t), Gr = Zr, Dr = () => {
460
- const r = j();
457
+ const zr = (r, t) => D().useForm(r, t), Ar = () => {
458
+ const r = g();
461
459
  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 }) => {
460
+ }, $r = ({ path: r }) => {
463
461
  const t = {
464
- [_]: r
462
+ [W]: r
465
463
  };
466
- return /* @__PURE__ */ u(
464
+ return /* @__PURE__ */ i(
467
465
  "div",
468
466
  {
469
467
  role: "alert",
@@ -472,14 +470,14 @@ const Zr = (r, t) => N().useForm(r, t), Gr = Zr, Dr = () => {
472
470
  ...t
473
471
  }
474
472
  );
475
- }, Kr = Vr;
473
+ };
476
474
  export {
477
475
  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
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
485
483
  };
@@ -0,0 +1,41 @@
1
+ import { TRPCClientError as a } from "@trpc/client";
2
+ import { z as e } from "zod";
3
+ const o = e.object({
4
+ validation: e.string(),
5
+ code: e.string(),
6
+ message: e.string(),
7
+ path: e.array(e.string())
8
+ }), s = e.custom(
9
+ (r) => r instanceof a
10
+ // check instance type
11
+ ).transform((r) => r.message).transform((r) => {
12
+ try {
13
+ return JSON.parse(r);
14
+ } catch {
15
+ return [
16
+ {
17
+ validation: "root",
18
+ code: "server",
19
+ message: r,
20
+ path: ["root"]
21
+ }
22
+ ];
23
+ }
24
+ }).pipe(e.array(o)).transform(
25
+ (r) => r.map(
26
+ (t) => [
27
+ t.path.join("."),
28
+ // flatten path (e.g., "street.name")
29
+ {
30
+ type: t.code,
31
+ message: t.message
32
+ }
33
+ ]
34
+ )
35
+ ).transform((r) => Object.fromEntries(r)), m = (r) => (t) => t instanceof a ? s.parse(
36
+ t
37
+ ) : r(t);
38
+ export {
39
+ m as makeOnSubmitTrpcClientErrorHandler,
40
+ s as trpcClientErrorToFormSubmitFieldErrorsSchemaTransformer
41
+ };
@@ -0,0 +1,10 @@
1
+ import { c as t } from "./createContext.mjs";
2
+ const o = (e) => Object.keys(e).length === 0, {
3
+ Provider: s,
4
+ useRequired: a
5
+ } = t();
6
+ export {
7
+ s as _,
8
+ o as i,
9
+ a as u
10
+ };
@@ -0,0 +1,20 @@
1
+ import { jsx as t } from "react/jsx-runtime";
2
+ import { i as s, u as i } from "./useFlatFieldErrorsContext.mjs";
3
+ const l = ({ errors: r }) => {
4
+ if (r && !s(r))
5
+ return /* @__PURE__ */ t("ul", { children: Object.values(r).map((o, e) => /* @__PURE__ */ t(
6
+ "li",
7
+ {
8
+ role: "alert",
9
+ "aria-live": "polite",
10
+ children: o.message
11
+ },
12
+ e
13
+ )) });
14
+ }, m = () => {
15
+ const r = i().roots;
16
+ return /* @__PURE__ */ t(l, { errors: r });
17
+ };
18
+ export {
19
+ m as RootErrorsListFromFlatFieldErrorsContext
20
+ };
@@ -0,0 +1,9 @@
1
+ export { default as FormGroupContextProvider } from '../form/context/group/FormGroupContextProvider';
2
+ export { default as useFormGroupChildIsMountedTracker } from '../form/context/group/useFormGroupChildIsMountedTracker';
3
+ export { default as useFormGroupChildIsSubmittingTracker } from '../form/context/group/useFormGroupChildIsSubmittingTracker';
4
+ export { default as useFormGroupChildTracker } from '../form/context/group/useFormGroupChildTracker';
5
+ export { default as useFormGroupIsAnyBusy } from '../form/context/group/useFormGroupIsAnyBusy';
6
+ export { default as useFormGroupIsChildBusy } from '../form/context/group/useFormGroupIsChildBusy';
7
+ export { default as useFormGroupIsParentBusy } from '../form/context/group/useFormGroupIsParentBusy';
8
+ export { default as useFormGroupParentTracker } from '../form/context/group/useFormGroupParentTracker';
9
+ export { default as useFormOrParentIsBusy } from '../form/context/group/useFormOrParentIsBusy';
@@ -0,0 +1,15 @@
1
+ export type { Register } from '../register';
2
+ export type { RhfUtilsClientConfig } from '../client/config/RhfUtilsClientConfig';
3
+ export { default as RhfUtilsClientForZodContextProvider } from '../client/zod/context/RhfUtilsClientForZodContextProvider';
4
+ export { default as RhfUtilsZodForm } from '../client/zod/context/RhfUtilsZodForm';
5
+ export { default as useRhfUtilsZodForm } from '../client/zod/context/useRhfUtilsZodForm';
6
+ export type { RhfUtilsUseFormChildrenZodFC } from '../client/zod/RhfUtilsUseFormChildrenZodFC';
7
+ export type { RhfUtilsUseFormChildrenZodProps } from '../client/zod/RhfUtilsUseFormChildrenZodProps';
8
+ export { default as useFlatFieldErrorsContext } from '../errors/flat/context/useFlatFieldErrorsContext';
9
+ export { default as useFlatFieldErrorsContextHasOnlyOrphans } from '../errors/flat/context/useFlatFieldErrorsContextHasOnlyOrphans';
10
+ export { default as RhfUtilsNonFieldErrorMarker } from '../errors/nonfield/RhfUtilsNonFieldErrorMarker';
11
+ export type { RhfUtilsFormOptions } from '../form/RhfUtilsFormOptions';
12
+ export type { SafeFieldValues } from '../form/SafeFieldValues';
13
+ export type { UseRhfUtilsFormChildrenProps } from '../form/UseRhfUtilsFormChildrenProps';
14
+ export { default as useRhfUtilsContext } from '../form/context/utils/useRhfUtilsContext';
15
+ export { FormSubmitError } from '../submit/error/FormSubmitError';
@@ -0,0 +1,2 @@
1
+ export { default as makeOnSubmitTrpcClientErrorHandler } from '../errors/trpc/makeOnSubmitTrpcClientErrorHandler';
2
+ export { default as trpcClientErrorToFormSubmitFieldErrorsSchemaTransformer } from '../errors/trpc/trpcClientErrorToFormSubmitFieldErrorsSchemaTransformer';
@@ -0,0 +1 @@
1
+ export { default as RootErrorsListFromFlatFieldErrorsContext } from '../errors/root/RootErrorsListFromFlatFieldErrorsContext';
@@ -20,8 +20,7 @@ import { FlatFieldErrors } from './types';
20
20
  * }
21
21
  * ```
22
22
  */
23
- declare const getFlatFieldErrors: (errors: FieldErrors) => FlatFieldErrors;
24
- export default getFlatFieldErrors;
23
+ export declare const getFlatFieldErrors: (errors: FieldErrors) => FlatFieldErrors;
25
24
  /**
26
25
  * Regular expression to maybe match the end of a leaf node path.
27
26
  *
@@ -7,5 +7,5 @@ import { FormSubmitFieldErrors } from '../../submit/error/FormSubmitFieldErrors'
7
7
  * @param onError Fallback for handling non-TRPC errors.
8
8
  * @returns FormSubmitError
9
9
  */
10
- declare const getOnSubmitTrpcClientErrorHandler: (onNotTrpcClientError: NonNullable<RhfUtilsClientConfig["onSubmitErrorUnknown"]>) => (error: unknown) => FormSubmitFieldErrors | undefined;
11
- export default getOnSubmitTrpcClientErrorHandler;
10
+ declare const makeOnSubmitTrpcClientErrorHandler: (onNotTrpcClientError: NonNullable<RhfUtilsClientConfig["onSubmitErrorUnknown"]>) => (error: unknown) => FormSubmitFieldErrors | undefined;
11
+ export default makeOnSubmitTrpcClientErrorHandler;
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.139",
5
+ "version": "0.0.141",
6
6
  "description": "Integration utilities for react-hook-form.",
7
7
  "type": "module",
8
8
  "sideEffects": false,
@@ -10,18 +10,30 @@
10
10
  "dist/esm/*",
11
11
  "dist/types/*"
12
12
  ],
13
- "module": "./dist/esm/exports.mjs",
13
+ "module": "./dist/esm/main.mjs",
14
14
  "exports": {
15
15
  ".": {
16
16
  "import": {
17
- "default": "./dist/esm/exports.mjs",
18
- "types": "./dist/types/exports.d.ts"
17
+ "default": "./dist/esm/main.mjs",
18
+ "types": "./dist/types/_exports/main.d.ts"
19
19
  }
20
20
  },
21
- "./form/context/group": {
21
+ "./group": {
22
22
  "import": {
23
- "default": "./dist/esm/form/context/group/exports.mjs",
24
- "types": "./dist/types/form/context/group/exports.d.ts"
23
+ "default": "./dist/esm/group.mjs",
24
+ "types": "./dist/types/_exports/group.d.ts"
25
+ }
26
+ },
27
+ "./trpc": {
28
+ "import": {
29
+ "default": "./dist/esm/trpc.mjs",
30
+ "types": "./dist/types/_exports/trpc.d.ts"
31
+ }
32
+ },
33
+ "./utils": {
34
+ "import": {
35
+ "default": "./dist/esm/utils.mjs",
36
+ "types": "./dist/types/_exports/utils.d.ts"
25
37
  }
26
38
  }
27
39
  },
@@ -1,74 +0,0 @@
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
- };
@@ -1,15 +0,0 @@
1
- export type { Register } from './register';
2
- export type { RhfUtilsClientConfig } from './client/config/RhfUtilsClientConfig';
3
- export { default as RhfUtilsClientForZodContextProvider } from './client/zod/context/RhfUtilsClientForZodContextProvider';
4
- export { default as RhfUtilsZodForm } from './client/zod/context/RhfUtilsZodForm';
5
- export { default as useRhfUtilsZodForm } from './client/zod/context/useRhfUtilsZodForm';
6
- export type { RhfUtilsUseFormChildrenZodFC } from './client/zod/RhfUtilsUseFormChildrenZodFC';
7
- export type { RhfUtilsUseFormChildrenZodProps } from './client/zod/RhfUtilsUseFormChildrenZodProps';
8
- export { default as useFlatFieldErrorsContext } from './errors/flat/context/useFlatFieldErrorsContext';
9
- export { default as useFlatFieldErrorsContextHasOnlyOrphans } from './errors/flat/context/useFlatFieldErrorsContextHasOnlyOrphans';
10
- export { default as RhfUtilsNonFieldErrorMarker } from './errors/nonfield/RhfUtilsNonFieldErrorMarker';
11
- export type { RhfUtilsFormOptions } from './form/RhfUtilsFormOptions';
12
- export type { SafeFieldValues } from './form/SafeFieldValues';
13
- export type { UseRhfUtilsFormChildrenProps } from './form/UseRhfUtilsFormChildrenProps';
14
- export { default as useRhfUtilsContext } from './form/context/utils/useRhfUtilsContext';
15
- export { FormSubmitError } from './submit/error/FormSubmitError';
@@ -1,9 +0,0 @@
1
- export { default as FormGroupContextProvider } from './FormGroupContextProvider';
2
- export { default as useFormGroupChildIsMountedTracker } from './useFormGroupChildIsMountedTracker';
3
- export { default as useFormGroupChildIsSubmittingTracker } from './useFormGroupChildIsSubmittingTracker';
4
- export { default as useFormGroupChildTracker } from './useFormGroupChildTracker';
5
- export { default as useFormGroupIsAnyBusy } from './useFormGroupIsAnyBusy';
6
- export { default as useFormGroupIsChildBusy } from './useFormGroupIsChildBusy';
7
- export { default as useFormGroupIsParentBusy } from './useFormGroupIsParentBusy';
8
- export { default as useFormGroupParentTracker } from './useFormGroupParentTracker';
9
- export { default as useFormOrParentIsBusy } from './useFormOrParentIsBusy';