@paragrav/rhf-utils 0.61.0 → 0.63.0

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 (45) hide show
  1. package/README.md +49 -89
  2. package/dist/dts/_exports/relay.d.ts +7 -8
  3. package/dist/dts/errors/flat/getFlatFieldErrors.d.ts +5 -1
  4. package/dist/dts/form/RhfUtilsFormComponentPropsType.d.ts +2 -2
  5. package/dist/dts/form/UseRhfUtilsFormChildrenPropsType.d.ts +2 -2
  6. package/dist/dts/form/context/utils/LastSubmitStateType.d.ts +1 -0
  7. package/dist/dts/form/context/utils/{RhfUtilsContextPropsType.d.ts → RhfUtilsContextType.d.ts} +3 -2
  8. package/dist/dts/form/context/utils/useRhfUtilsContext.d.ts +4 -4
  9. package/dist/dts/form/defaults/form/useRhfUtilsFormResolvedFormProps.d.ts +10 -10
  10. package/dist/dts/form/providers/RhfUtilsFormProviders.d.ts +1 -1
  11. package/dist/dts/form/providers/RhfUtilsFormProvidersPropsType.d.ts +2 -0
  12. package/dist/dts/form/relay/FormRelayOptions.d.ts +16 -0
  13. package/dist/dts/form/{state/relay/FormStateRelayContextProvider.d.ts → relay/context/FormRelayContextProvider.d.ts} +1 -1
  14. package/dist/dts/form/relay/context/useFormRelayContext.d.ts +12 -0
  15. package/dist/dts/form/relay/context/utils.d.ts +4 -0
  16. package/dist/dts/form/relay/get/FormRelayGroupType.d.ts +16 -0
  17. package/dist/dts/form/relay/get/grouper/context/hooks.d.ts +8 -0
  18. package/dist/dts/form/relay/get/grouper/context/index.d.ts +5 -0
  19. package/dist/dts/form/relay/get/grouper/context/provider.d.ts +9 -0
  20. package/dist/dts/form/relay/get/grouper/useFirstFormStateGroup.d.ts +9 -0
  21. package/dist/dts/form/relay/get/useFormRelay.d.ts +13 -0
  22. package/dist/dts/form/relay/set/FormRelaySetter.d.ts +6 -0
  23. package/dist/dts/form/relay/set/useFormRelaySet.d.ts +2 -0
  24. package/dist/dts/form/relay/types.d.ts +12 -0
  25. package/dist/dts/form/with-handlers-and-children/RhfUtilsFormProps.d.ts +5 -1
  26. package/dist/dts/form/with-handlers-and-children/_useHandleSubmit.d.ts +1 -1
  27. package/dist/dts/form/with-providers/RhfUtilsFormWithProviders.d.ts +1 -1
  28. package/dist/dts/submit/UseRhfUtilsFormOnSubmitContextType.d.ts +3 -3
  29. package/dist/dts/submit/last-error/useLastSubmitErrorContextWith.d.ts +1 -1
  30. package/dist/esm/FormRelaySetter.mjs +89 -0
  31. package/dist/esm/index.mjs +352 -596
  32. package/dist/esm/relay.mjs +48 -221
  33. package/dist/esm/trpc.mjs +24 -40
  34. package/package.json +28 -29
  35. package/dist/dts/form/RhfUtilsFormStateWithUtilsType.d.ts +0 -7
  36. package/dist/dts/form/state/relay/FormStateRelayedAggregatedType.d.ts +0 -10
  37. package/dist/dts/form/state/relay/FormStateRelayedItemType.d.ts +0 -11
  38. package/dist/dts/form/state/relay/FormStateRelayedOptionsType.d.ts +0 -20
  39. package/dist/dts/form/state/relay/FormStateRelayedType.d.ts +0 -3
  40. package/dist/dts/form/state/relay/pull/hooks.d.ts +0 -31
  41. package/dist/dts/form/state/relay/push/FormStateRelayPusher.d.ts +0 -4
  42. package/dist/dts/form/state/relay/push/useFormStateRelayPusher.d.ts +0 -3
  43. package/dist/dts/form/state/relay/useFormStateRelayContext.d.ts +0 -20
  44. package/dist/dts/form/state/relay/utils.d.ts +0 -4
  45. package/dist/esm/getFlatFieldErrors.mjs +0 -55
@@ -1,599 +1,355 @@
1
- import { jsx as u, jsxs as X } from "react/jsx-runtime";
2
- import c from "react";
3
- import { c as j, u as v, g as Y, i as _, _ as tt } from "./getFlatFieldErrors.mjs";
4
- import { Controller as rt, useFormContext as b, useFormState as I, useWatch as et, useForm as ot, FormProvider as st } from "react-hook-form";
5
- import { zodResolver as nt } from "@hookform/resolvers/zod";
6
- const L = c.createContext(void 0), or = ({
7
- config: t,
8
- children: r
9
- }) => {
10
- const [e] = c.useState(t ?? {});
11
- return /* @__PURE__ */ u(L.Provider, { value: e, children: r });
12
- }, {
13
- Provider: it,
14
- useRequired: B
15
- } = j(), sr = () => {
16
- const t = B();
17
- return c.useMemo(() => !t.hasErrors || !t.hasOrphans ? !1 : Object.keys(t.orphans).length === Object.keys(t.all).length, [t]);
18
- }, M = "data-rhfutils-nonfield-error-marker-path", nr = ({ path: t }) => {
19
- const r = {
20
- [M]: t
21
- };
22
- return /* @__PURE__ */ u(
23
- "div",
24
- {
25
- role: "alert",
26
- "aria-hidden": "true",
27
- style: { display: "none" },
28
- ...r
29
- }
30
- );
31
- }, ct = (t) => c.useCallback(
32
- (r) => {
33
- if (!t.current) throw new Error();
34
- ut(t.current, r);
35
- },
36
- [t]
37
- ), ut = (t, r) => {
38
- t.requestSubmit(r);
39
- };
40
- class E extends Error {
41
- constructor(r, e) {
42
- super(e), this.errors = r;
43
- }
44
- }
45
- const {
46
- Provider: lt,
47
- useRequired: N
48
- } = j(), ir = () => N().current, at = c.lazy(
49
- () => import("@hookform/devtools").then((t) => ({
50
- default: t.DevTool
51
- }))
52
- ), mt = ({ config: t }) => {
53
- const [r] = c.useState(t);
54
- if (!r) return null;
55
- const e = typeof r == "object" ? r : void 0;
56
- return /* @__PURE__ */ u(c.Suspense, { children: /* @__PURE__ */ u(at, { ...e }) });
57
- }, ft = () => {
58
- const t = v();
59
- return /* @__PURE__ */ u(mt, { config: t.options.devTool });
60
- }, q = (t) => /* @__PURE__ */ u(rt, { ...t }), C = () => {
61
- const t = c.useContext(L);
62
- if (t === void 0) throw new Error();
63
- return t;
64
- }, dt = () => {
65
- const t = v(), r = b();
66
- return {
67
- utils: t,
68
- rhf: r,
69
- FormSubmitError: E
70
- };
71
- }, ht = (t, r, e) => {
72
- const o = r?.();
73
- return async () => {
74
- if (!(!t || await Promise.resolve(
75
- o?.(e)
76
- ) === !1))
77
- return Promise.resolve(t());
78
- };
79
- }, Ft = (t) => {
80
- const { useCanFormBeCancelled: r } = C(), e = dt();
81
- return ht(
82
- t,
83
- r,
84
- e
85
- );
86
- }, bt = ({
87
- onCancel: t,
88
- Children: r
89
- }) => {
90
- const e = b(), o = v(), s = Ft(t);
91
- return /* @__PURE__ */ u(
92
- r,
93
- {
94
- ...o,
95
- rhf: e,
96
- onCancel: s,
97
- Controller: q,
98
- FormSubmitError: E
99
- }
100
- );
101
- }, St = (t, r) => {
102
- Object.entries(r).forEach(([e, o]) => {
103
- t.setError(e, {
104
- type: "FormSubmitFieldError",
105
- // default
106
- ...o
107
- });
108
- });
109
- }, pt = ({
110
- children: t
111
- }) => t, Et = (t, r, e) => ({
112
- ...t,
113
- ...r,
114
- className: [t?.className, r?.className, e?.className].filter(Boolean).join(" ").trim() || void 0
115
- // replace empty string with undefined
116
- }), vt = (t, r, e) => {
117
- const o = C();
118
- return {
119
- id: r,
120
- ...Et(
121
- o.defaults?.form,
122
- t,
123
- e
124
- )
125
- };
126
- }, Ct = (t) => {
127
- const r = N();
128
- return async function(e) {
129
- try {
130
- return r.reset(), await t(e);
131
- } catch (o) {
132
- throw r.set(o, e), o;
133
- }
134
- };
135
- }, Rt = ({
136
- onValid: t,
137
- onInvalid: r,
138
- onError: e,
139
- onFinally: o
140
- }) => {
141
- const s = b(), { options: n, lastSubmitStateRef: i } = v(), a = s.handleSubmit(t, r), m = Ct(a);
142
- return (l) => {
143
- i.current = "submitting", n.stopSubmitPropagation && l.stopPropagation(), Promise.resolve(m(l)).then(() => {
144
- i.current = "success";
145
- }).catch((d) => {
146
- i.current = "error", e(d, l);
147
- }).finally(() => {
148
- o?.();
149
- });
150
- };
151
- }, gt = ({
152
- getApiData: t,
153
- // handlers
154
- onSubmitInvalid: r,
155
- onBeforeSubmitInvariants: e,
156
- onBeforeSubmit: o,
157
- onSubmit: s,
158
- onSubmitSuccess: n,
159
- onSubmitError: i,
160
- onSubmitFinally: a,
161
- // other
162
- form: m,
163
- className: l,
164
- //
165
- children: d
166
- }) => {
167
- const f = b(), h = v(), S = C(), P = vt(
168
- m,
169
- h.formId,
170
- {
171
- className: l
172
- }
173
- ), y = async (F, U) => {
174
- const p = f.getValues(), G = t?.(F), R = { input: p, output: F, api: G }, g = {
175
- utils: h,
176
- rhf: f,
177
- FormSubmitError: E
178
- }, O = U;
179
- e && await Ot(e, R, g, O), await Promise.resolve(o?.(R, g, O));
180
- const K = await Promise.resolve(
181
- s?.(R, g, O)
182
- );
183
- h.lastSubmitStateRef.current = "success", await Promise.resolve(
184
- n?.(K, R, g, O)
185
- );
186
- };
187
- function x(F, U) {
188
- const p = F instanceof E ? (
189
- // consumer can manually throw FormSubmitError (e.g., manual validation)
190
- F.errors
191
- ) : (
192
- // if error is not FormSubmitError, consumer can provide global handler
193
- S.onSubmitErrorUnknown?.(F)
194
- );
195
- p && St(f, p), i?.(
196
- F,
197
- {
198
- utils: h,
199
- errors: p,
200
- rhf: f
201
- },
202
- U
203
- // `as` required due to conditional generic
204
- );
205
- }
206
- const V = Rt({
207
- onValid: y,
208
- onInvalid: r,
209
- onError: x,
210
- onFinally: a
211
- }), W = {
212
- ...h,
213
- rhf: f,
214
- Controller: q,
215
- FormSubmitError: E
216
- }, Q = S.FormComponent ?? "form", $ = S.FormChildren ?? pt;
217
- return /* @__PURE__ */ u(
218
- Q,
219
- {
220
- ...P,
221
- id: h.formId,
222
- ref: h.formRef,
223
- onSubmit: V,
224
- children: /* @__PURE__ */ u($, { ...W, children: d })
225
- }
226
- );
227
- }, Ot = async (t, r, e, o) => {
228
- const n = (await Promise.resolve(
229
- t(r, e, o)
230
- )).filter(([, , i]) => !i).map(([i, a]) => [i, { message: a }]);
231
- if (n.length)
232
- throw new e.FormSubmitError(
233
- Object.fromEntries(n)
234
- );
235
- }, H = ({
236
- getApiData: t,
237
- // handlers
238
- onCancel: r,
239
- onSubmitInvalid: e,
240
- onBeforeSubmitInvariants: o,
241
- onBeforeSubmit: s,
242
- onSubmit: n,
243
- onSubmitSuccess: i,
244
- onSubmitError: a,
245
- onSubmitFinally: m,
246
- //
247
- Children: l,
248
- // attrs
249
- form: d,
250
- className: f
251
- }) => /* @__PURE__ */ X(
252
- gt,
253
- {
254
- getApiData: t,
255
- onSubmitInvalid: e,
256
- onBeforeSubmitInvariants: o,
257
- onBeforeSubmit: s,
258
- onSubmit: n,
259
- onSubmitSuccess: i,
260
- onSubmitError: a,
261
- onSubmitFinally: m,
262
- form: d,
263
- className: f,
264
- children: [
265
- /* @__PURE__ */ u(ft, {}),
266
- /* @__PURE__ */ u(
267
- bt,
268
- {
269
- onCancel: r,
270
- Children: l
271
- }
272
- )
273
- ]
274
- }
275
- ), cr = (t) => /* @__PURE__ */ u(H, { ...t }), k = (t) => (r) => (
276
- // re-create object from entries
277
- Object.fromEntries(
278
- // break down to entries
279
- Object.entries(r).filter(t)
280
- )
281
- ), z = (t) => !!t.ref, Pt = ([
282
- ,
283
- t
284
- ]) => z(t), yt = k(
285
- Pt
286
- ), xt = (t, r) => (
287
- // list of selector variants
288
- [
289
- // equals
290
- // e.g., attr="path"
291
- ["", ""],
292
- [
293
- // @see https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors#attrvalue_4
294
- "^",
295
- // only match nested (avoid match 'field' with 'field1`)
296
- "."
297
- ]
298
- ].map(
299
- ([e, o]) => Ut(
300
- M + e,
301
- t + o
302
- )
303
- ).join(", ")
304
- ), Ut = (t, r) => `[role="alert"][${t}="${r}"]`, wt = (t, r) => !!r.querySelector(
305
- xt(t)
306
- ), D = "root", A = (t) => t === D || t.startsWith(D + "."), It = (t, r, e) => (
307
- // non-field
308
- !z(r) && // non-root
309
- !A(t) && // not marked in DOM
310
- !wt(t, e)
311
- ), kt = (t) => ([r, e]) => It(r, e, t), Vt = (t, r) => k(
312
- kt(r)
313
- )(t), Wt = ([t]) => A(t), _t = k(
314
- Wt
315
- ), Dt = ({
316
- formRef: t,
317
- children: r
318
- }) => {
319
- const { errors: e } = I(), o = Y(e), s = (
320
- // eslint-disable-next-line react-hooks/refs
321
- t.current ? Vt(
322
- o,
323
- // eslint-disable-next-line react-hooks/refs
324
- t.current
325
- ) : {}
326
- ), n = {
327
- all: o,
328
- fields: yt(o),
329
- roots: _t(o),
330
- orphans: s,
331
- // computed
332
- hasErrors: !_(e),
333
- hasOrphans: !_(s)
334
- };
335
- return /* @__PURE__ */ u(it, { value: n, children: r });
336
- }, Tt = ({
337
- children: t
338
- }) => {
339
- const [r, e] = c.useState(void 0);
340
- return /* @__PURE__ */ u(
341
- lt,
342
- {
343
- value: {
344
- current: r,
345
- reset: () => {
346
- e(void 0);
347
- },
348
- set: (o, s) => {
349
- e({ error: o, event: s });
350
- }
351
- },
352
- children: t
353
- }
354
- );
355
- }, jt = (t, r, e, o = "debug") => {
356
- console[o](t, { values: r, errors: e });
357
- }, Lt = (t) => {
358
- const r = b(), e = B();
359
- c.useEffect(() => {
360
- if (!e.hasErrors) return;
361
- const o = t?.console?.(e);
362
- o && jt(
363
- o.message ?? T,
364
- r.getValues(),
365
- e,
366
- o.type
367
- );
368
- const s = t?.throw?.(e);
369
- if (s)
370
- throw new Error(typeof s == "string" ? s : T);
371
- }, [e]);
372
- }, T = "Form errors", Bt = (t) => {
373
- const r = c.useRef(!1);
374
- return c.useEffect(() => {
375
- t && (r.current = !0);
376
- }, [t]), [r, () => {
377
- r.current = !1;
378
- }];
379
- }, Mt = (t, r) => {
380
- const { isSubmitSuccessful: e, isSubmitting: o } = I(), [s, n] = Bt(o);
381
- c.useEffect(() => {
382
- t && (o || s.current && (n(), t(e)));
383
- }, [o]);
384
- }, Nt = (t) => {
385
- const { getValues: r, reset: e } = b();
386
- Mt((o) => {
387
- if (!t) return;
388
- const s = t[o ? "success" : "error"]?.values;
389
- if (!s) return;
390
- const n = s === "current", i = n ? r() : void 0;
391
- e(i, {
392
- // keep submit stuff for tracking relevant state (e.g., `useFormOnSubmitSuccessful`)
393
- keepIsSubmitSuccessful: !0,
394
- keepIsSubmitted: !0,
395
- keepSubmitCount: !0,
396
- /**
397
- * This is necessary when setting default values to current values (lest form stop updating).
398
- *
399
- * Technically, we are keeping values and resetting default values to same values.
400
- *
401
- * Steps to repro:
402
- * - RhfUtilsClientConfig with resetOnSubmitted set to success values to current
403
- * - remove `keepValues` prop from reset
404
- * - create valid form, submit
405
- * - form values should be unchanged
406
- * - unexpectedly: can no longer type into input
407
- */
408
- keepValues: n,
409
- // keep errors in the case of unsuccessful submit
410
- keepErrors: !0
411
- });
412
- });
413
- }, qt = () => {
414
- const t = c.useRef(w);
415
- return c.useEffect(() => (t.current && (t.current = !w), () => {
416
- t.current = w;
417
- }), []), t;
418
- }, w = !0, Ht = ({
419
- onChange: t,
420
- delay: r
421
- }) => {
422
- const e = c.useRef(null), o = () => {
423
- e.current && (t(e.current.value), e.current = null);
424
- }, s = () => {
425
- e.current && clearTimeout(e.current.timer);
426
- };
427
- return c.useEffect(
428
- // return timeout clearer
429
- () => s,
430
- []
431
- ), [(i) => {
432
- s(), e.current = {
433
- // value being tracked
434
- value: i,
435
- // new timer
436
- timer: setTimeout(o, r)
437
- };
438
- }];
439
- }, zt = (t, r) => {
440
- const e = ct(t), [o] = Ht({
441
- delay: r?.debounce ?? 0,
442
- // on change value after debounce
443
- onChange: () => {
444
- e();
445
- }
446
- });
447
- return o;
448
- }, At = (t, r) => {
449
- const e = zt(t, {
450
- debounce: r?.debounce
451
- }), o = et(), { isValid: s, isDirty: n, isValidating: i, isSubmitting: a } = I(), m = c.useMemo(() => JSON.stringify(o), [o]), l = qt();
452
- c.useEffect(
453
- // on form values/state change, call callback
454
- () => {
455
- l.current || i || a || s && n && e(o);
456
- },
457
- // eslint-disable-next-line react-hooks/exhaustive-deps
458
- [s, n, m, i]
459
- // - isValidating bc we need to re-trigger after validation complete
460
- // - e.g., single keystroke making input in/valid; we have to wait
461
- // - isSubmitting not included bc would create infinite loop of submits after first submission
462
- // - expect form to block changes while submitting to avoid changes that get swallowed
463
- );
464
- }, Zt = ({
465
- formId: t,
466
- formRef: r,
467
- options: e,
468
- //
469
- children: o
470
- }) => {
471
- const [s] = c.useState(e), n = C();
472
- Lt(n.fieldErrors?.output), (s.submitOnWatch ? At : void 0)?.(
473
- r,
474
- s.submitOnWatch
475
- ), Nt(s.resetOnSubmitted);
476
- const i = c.useRef(null);
477
- return /* @__PURE__ */ u(
478
- tt,
479
- {
480
- value: {
481
- formId: t,
482
- formRef: r,
483
- options: e,
484
- lastSubmitStateRef: i
485
- },
486
- children: o
487
- }
488
- );
489
- }, Jt = (t, r) => ({
490
- ...t,
491
- ...r,
492
- // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- false positive
493
- ...(t?.resetOnSubmitted || r?.resetOnSubmitted) && {
494
- resetOnSubmitted: {
495
- ...t?.resetOnSubmitted,
496
- ...r?.resetOnSubmitted
497
- }
498
- }
499
- }), Qt = (t, r) => c.useMemo(
500
- () => Jt(t.defaults?.options, r),
501
- // eslint-disable-next-line react-hooks/exhaustive-deps
502
- [JSON.stringify(r)]
503
- ), $t = (t, r) => ({
504
- ...t,
505
- ...r
506
- }), Gt = (t, r, e, o) => ot({
507
- ...$t(r.defaults?.rhf, t),
508
- resolver: o,
509
- defaultValues: e
1
+ import { a as e, c as t, i as n, o as r, r as i, t as a, u as o } from "./FormRelaySetter.mjs";
2
+ import s from "react";
3
+ import { jsx as c, jsxs as l } from "react/jsx-runtime";
4
+ import { Controller as u, FormProvider as d, useForm as f, useFormContext as p, useFormState as m, useWatch as h } from "react-hook-form";
5
+ import { zodResolver as g } from "@hookform/resolvers/zod";
6
+ //#region src/client/config/context/RhfUtilsClientConfigContext.tsx
7
+ var _ = s.createContext(void 0), v = ({ config: e, children: n }) => {
8
+ let [r] = s.useState(e ?? {});
9
+ return /* @__PURE__ */ c(_.Provider, {
10
+ value: r,
11
+ children: /* @__PURE__ */ c(t, { children: n })
12
+ });
13
+ }, { Provider: y, useRequired: b } = o(), x = () => {
14
+ let e = b();
15
+ return s.useMemo(() => !e.hasErrors || !e.hasOrphans ? !1 : Object.keys(e.orphans).length === Object.keys(e.all).length, [e]);
16
+ }, S = "data-rhfutils-nonfield-error-marker-path", C = ({ path: e }) => {
17
+ let t = { [S]: e };
18
+ return /* @__PURE__ */ c("div", {
19
+ role: "alert",
20
+ "aria-hidden": "true",
21
+ style: { display: "none" },
22
+ ...t
23
+ });
24
+ }, w = (e) => s.useCallback((t) => {
25
+ if (!e.current) throw Error();
26
+ ee(e.current, t);
27
+ }, [e]), ee = (e, t) => {
28
+ e.requestSubmit(t);
29
+ }, T = class extends Error {
30
+ constructor(e, t) {
31
+ super(t), this.errors = e;
32
+ }
33
+ }, { Provider: te, useRequired: E } = o(), D = () => E().current, O = s.lazy(() => import("@hookform/devtools").then((e) => ({ default: e.DevTool }))), k = ({ config: e }) => {
34
+ let [t] = s.useState(e);
35
+ if (!t) return null;
36
+ let n = typeof t == "object" ? t : void 0;
37
+ return /* @__PURE__ */ c(s.Suspense, { children: /* @__PURE__ */ c(O, { ...n }) });
38
+ }, ne = () => /* @__PURE__ */ c(k, { config: r().options.devTool }), A = (e) => /* @__PURE__ */ c(u, { ...e }), j = () => {
39
+ let e = s.useContext(_);
40
+ if (e === void 0) throw Error();
41
+ return e;
42
+ }, re = () => ({
43
+ utils: r(),
44
+ rhf: p(),
45
+ FormSubmitError: T
46
+ }), ie = (e, t, n) => {
47
+ let r = t?.();
48
+ return async () => {
49
+ if (e && await Promise.resolve(r?.(n)) !== !1) return Promise.resolve(e());
50
+ };
51
+ }, ae = (e) => {
52
+ let { useCanFormBeCancelled: t } = j();
53
+ return ie(e, t, re());
54
+ }, oe = ({ onCancel: e, Children: t }) => {
55
+ let n = p(), i = r(), a = ae(e);
56
+ return /* @__PURE__ */ c(t, {
57
+ ...i,
58
+ rhf: n,
59
+ onCancel: a,
60
+ Controller: A,
61
+ FormSubmitError: T
62
+ });
63
+ }, M = (e, t) => {
64
+ Object.entries(t).forEach(([t, n]) => {
65
+ e.setError(t, {
66
+ type: "FormSubmitFieldError",
67
+ ...n
68
+ });
69
+ });
70
+ }, N = ({ children: e }) => e, P = (e, t, n) => ({
71
+ ...e,
72
+ ...t,
73
+ className: [
74
+ e?.className,
75
+ t?.className,
76
+ n?.className
77
+ ].filter(Boolean).join(" ").trim() || void 0
78
+ }), F = (e, t, n) => ({
79
+ id: t,
80
+ ...P(j().defaults?.form, e, n)
81
+ }), I = (e) => {
82
+ let t = E();
83
+ return async function(n) {
84
+ try {
85
+ return t.reset(), await e(n);
86
+ } catch (e) {
87
+ throw t.set(e, n), e;
88
+ }
89
+ };
90
+ }, L = ({ onValid: e, onInvalid: t, onError: n, onFinally: i }) => {
91
+ let a = p(), { options: o, lastSubmitStateRef: s } = r(), c = I(a.handleSubmit(e, t));
92
+ return (e) => {
93
+ s.current = "submitting", o.stopSubmitPropagation && e.stopPropagation(), Promise.resolve(c(e)).then(() => {
94
+ s.current = "success";
95
+ }).catch((t) => {
96
+ s.current = "error", n(t, e);
97
+ }).finally(() => {
98
+ i?.();
99
+ });
100
+ };
101
+ }, R = ({ getApiData: e, onSubmitInvalid: t, onBeforeSubmitInvariants: n, onBeforeSubmit: i, onSubmit: a, onSubmitSuccess: o, onSubmitError: s, onSubmitFinally: l, form: u, className: d, children: f }) => {
102
+ let m = p(), h = r(), g = j(), _ = F(u, h.formId, { className: d }), v = async (t, r) => {
103
+ let s = {
104
+ input: m.getValues(),
105
+ output: t,
106
+ api: e?.(t)
107
+ }, c = {
108
+ utils: h,
109
+ rhf: m,
110
+ FormSubmitError: T
111
+ }, l = r;
112
+ n && await z(n, s, c, l), await Promise.resolve(i?.(s, c, l));
113
+ let u = await Promise.resolve(a?.(s, c, l));
114
+ h.lastSubmitStateRef.current = "success", await Promise.resolve(o?.(u, s, c, l));
115
+ };
116
+ function y(e, t) {
117
+ let n = e instanceof T ? e.errors : g.onSubmitErrorUnknown?.(e);
118
+ n && M(m, n), s?.(e, {
119
+ utils: h,
120
+ errors: n,
121
+ rhf: m
122
+ }, t);
123
+ }
124
+ let b = L({
125
+ onValid: v,
126
+ onInvalid: t,
127
+ onError: y,
128
+ onFinally: l
129
+ }), x = {
130
+ ...h,
131
+ rhf: m,
132
+ Controller: A,
133
+ FormSubmitError: T
134
+ }, S = g.FormComponent ?? "form", C = g.FormChildren ?? N;
135
+ return /* @__PURE__ */ c(S, {
136
+ ..._,
137
+ id: h.formId,
138
+ ref: h.formRef,
139
+ onSubmit: b,
140
+ children: /* @__PURE__ */ c(C, {
141
+ ...x,
142
+ children: f
143
+ })
144
+ });
145
+ }, z = async (e, t, n, r) => {
146
+ let i = (await Promise.resolve(e(t, n, r))).filter(({ valid: e }) => !e).map(({ field: e, message: t }) => [e, { message: t }]);
147
+ if (i.length) throw new n.FormSubmitError(Object.fromEntries(i));
148
+ }, B = ({ getApiData: e, onCancel: t, onSubmitInvalid: n, onBeforeSubmitInvariants: r, onBeforeSubmit: i, onSubmit: a, onSubmitSuccess: o, onSubmitError: s, onSubmitFinally: u, Children: d, form: f, className: p }) => /* @__PURE__ */ l(R, {
149
+ getApiData: e,
150
+ onSubmitInvalid: n,
151
+ onBeforeSubmitInvariants: r,
152
+ onBeforeSubmit: i,
153
+ onSubmit: a,
154
+ onSubmitSuccess: o,
155
+ onSubmitError: s,
156
+ onSubmitFinally: u,
157
+ form: f,
158
+ className: p,
159
+ children: [/* @__PURE__ */ c(ne, {}), /* @__PURE__ */ c(oe, {
160
+ onCancel: t,
161
+ Children: d
162
+ })]
163
+ }), V = (e) => /* @__PURE__ */ c(B, { ...e }), H = (e) => (t) => Object.fromEntries(Object.entries(t).filter(e)), U = (e) => !!e.ref, W = H(([, e]) => U(e)), G = (e, t) => [["", ""], ...t?.excludeNested ? [] : [["^", "."]]].map(([t, n]) => K(S + t, e + n)).join(", "), K = (e, t) => `[role="alert"][${e}="${t}"]`, q = (e, t) => !!t.querySelector(G(e)), J = (e) => e === "root" || e.startsWith("root."), se = (e, t, n) => !U(t) && !J(e) && !q(e, n), ce = (e) => ([t, n]) => se(t, n, e), le = (e, t) => H(ce(t))(e), ue = H(([e]) => J(e)), de = ({ formRef: e, children: t }) => {
164
+ let { errors: r } = m(), a = i(r), o = e.current ? le(a, e.current) : {};
165
+ return /* @__PURE__ */ c(y, {
166
+ value: {
167
+ all: a,
168
+ fields: W(a),
169
+ roots: ue(a),
170
+ orphans: o,
171
+ hasErrors: !n(r),
172
+ hasOrphans: !n(o)
173
+ },
174
+ children: t
175
+ });
176
+ }, fe = ({ children: e }) => {
177
+ let [t, n] = s.useState(void 0);
178
+ return /* @__PURE__ */ c(te, {
179
+ value: {
180
+ current: t,
181
+ reset: () => {
182
+ n(void 0);
183
+ },
184
+ set: (e, t) => {
185
+ n({
186
+ error: e,
187
+ event: t
188
+ });
189
+ }
190
+ },
191
+ children: e
192
+ });
193
+ }, pe = (e, t, n, r = "debug") => {
194
+ console[r](e, {
195
+ values: t,
196
+ errors: n
197
+ });
198
+ }, me = (e) => {
199
+ let t = p(), n = b();
200
+ s.useEffect(() => {
201
+ if (!n.hasErrors) return;
202
+ let r = e?.console?.(n);
203
+ r && pe(r.message ?? Y, t.getValues(), n, r.type);
204
+ let i = e?.throw?.(n);
205
+ if (i) throw Error(typeof i == "string" ? i : Y);
206
+ }, [n]);
207
+ }, Y = "Form errors", he = (e) => {
208
+ let t = s.useRef(!1);
209
+ return s.useEffect(() => {
210
+ e && (t.current = !0);
211
+ }, [e]), [t, () => {
212
+ t.current = !1;
213
+ }];
214
+ }, ge = (e, t) => {
215
+ let { isSubmitSuccessful: n, isSubmitting: r } = m(), [i, a] = he(r);
216
+ s.useEffect(() => {
217
+ e && (r || i.current && (a(), (t?.successful === void 0 || n === t.successful) && e(n)));
218
+ }, [r]);
219
+ }, _e = (e) => {
220
+ let { getValues: t, reset: n } = p();
221
+ ge((r) => {
222
+ if (!e) return;
223
+ let i = e[r ? "success" : "error"]?.values;
224
+ if (!i) return;
225
+ let a = i === "current";
226
+ n(a ? t() : void 0, {
227
+ keepIsSubmitSuccessful: !0,
228
+ keepIsSubmitted: !0,
229
+ keepSubmitCount: !0,
230
+ keepValues: a,
231
+ keepErrors: !0
232
+ });
233
+ });
234
+ }, ve = () => {
235
+ let e = s.useRef(X);
236
+ return s.useEffect(() => (e.current &&= !X, () => {
237
+ e.current = X;
238
+ }), []), e;
239
+ }, X = !0, ye = ({ onChange: e, delay: t }) => {
240
+ let n = s.useRef(null), r = () => {
241
+ n.current &&= (e(n.current.value), null);
242
+ }, i = () => {
243
+ n.current && clearTimeout(n.current.timer);
244
+ };
245
+ return s.useEffect(() => i, []), [(e) => {
246
+ i(), n.current = {
247
+ value: e,
248
+ timer: setTimeout(r, t)
249
+ };
250
+ }];
251
+ }, be = (e, t) => {
252
+ let n = w(e), [r] = ye({
253
+ delay: t?.debounce ?? 0,
254
+ onChange: () => {
255
+ n();
256
+ }
257
+ });
258
+ return r;
259
+ }, xe = (e, t) => {
260
+ let n = be(e, { debounce: t?.debounce }), r = h(), { isValid: i, isDirty: a, isValidating: o, isSubmitting: c } = m(), l = s.useMemo(() => JSON.stringify(r), [r]), u = ve();
261
+ s.useEffect(() => {
262
+ u.current || o || c || i && a && n(r);
263
+ }, [
264
+ i,
265
+ a,
266
+ l,
267
+ o
268
+ ]);
269
+ }, Se = ({ formId: t, formRef: n, options: r, children: i }) => {
270
+ let [a] = s.useState(r);
271
+ return me(j().fieldErrors?.output), (a.submitOnWatch ? xe : void 0)?.(n, a.submitOnWatch), _e(a.resetOnSubmitted), /* @__PURE__ */ c(e, {
272
+ value: {
273
+ formId: t,
274
+ formRef: n,
275
+ options: r,
276
+ lastSubmitStateRef: s.useRef(null)
277
+ },
278
+ children: i
279
+ });
280
+ }, Z = (e, t) => ({
281
+ ...e,
282
+ ...t,
283
+ ...(e?.resetOnSubmitted || t?.resetOnSubmitted) && { resetOnSubmitted: {
284
+ ...e?.resetOnSubmitted,
285
+ ...t?.resetOnSubmitted
286
+ } }
287
+ }), Ce = (e, t) => s.useMemo(() => Z(e.defaults?.options, t), [JSON.stringify(t)]), we = (e, t) => ({
288
+ ...e,
289
+ ...t
290
+ }), Te = (e, t, n, r) => f({
291
+ ...we(t.defaults?.rhf, e),
292
+ resolver: r,
293
+ defaultValues: n
510
294
  });
511
- function Z({
512
- formId: t,
513
- rhf: r,
514
- resolver: e,
515
- defaultValues: o,
516
- options: s,
517
- //
518
- children: n
519
- }) {
520
- const i = C(), a = c.useId(), m = t || a, l = c.useRef(null), d = Gt(
521
- r,
522
- i,
523
- o,
524
- e
525
- ), f = Qt(i, s);
526
- return /* @__PURE__ */ u(st, { ...d, children: /* @__PURE__ */ u(Dt, { formRef: l, children: /* @__PURE__ */ u(
527
- Zt,
528
- {
529
- formId: m,
530
- formRef: l,
531
- options: f,
532
- children: /* @__PURE__ */ u(Tt, { children: n })
533
- }
534
- ) }) });
295
+ //#endregion
296
+ //#region src/form/providers/RhfUtilsFormProviders.tsx
297
+ function Q({ formId: e, rhf: t, resolver: n, defaultValues: r, options: i, relay: o, children: u }) {
298
+ let f = j(), p = s.useId(), m = e || p, h = s.useRef(null), g = Te(t, f, r, n), _ = Ce(f, i);
299
+ return /* @__PURE__ */ c(d, {
300
+ ...g,
301
+ children: /* @__PURE__ */ c(de, {
302
+ formRef: h,
303
+ children: /* @__PURE__ */ l(Se, {
304
+ formId: m,
305
+ formRef: h,
306
+ options: _,
307
+ children: [/* @__PURE__ */ c(a, { options: o }), /* @__PURE__ */ c(fe, { children: u })]
308
+ })
309
+ })
310
+ });
535
311
  }
536
- const J = (t) => nt(t), ur = ({
537
- schema: t,
538
- children: r,
539
- ...e
540
- }) => /* @__PURE__ */ u(Z, { ...e, resolver: J(t), children: r });
541
- function Kt({
542
- getApiData: t,
543
- // provider
544
- resolver: r,
545
- rhf: e,
546
- defaultValues: o,
547
- options: s,
548
- // form
549
- onCancel: n,
550
- onSubmitInvalid: i,
551
- onBeforeSubmitInvariants: a,
552
- onBeforeSubmit: m,
553
- onSubmit: l,
554
- onSubmitSuccess: d,
555
- onSubmitError: f,
556
- onSubmitFinally: h,
557
- Children: S,
558
- form: P,
559
- formId: y,
560
- className: x
561
- }) {
562
- return /* @__PURE__ */ u(Z, { ...{
563
- formId: y,
564
- resolver: r,
565
- rhf: e,
566
- defaultValues: o,
567
- options: s
568
- }, children: /* @__PURE__ */ u(H, { ...{
569
- getApiData: t,
570
- onCancel: n,
571
- onSubmitInvalid: i,
572
- onBeforeSubmitInvariants: a,
573
- onBeforeSubmit: m,
574
- onSubmit: l,
575
- onSubmitSuccess: d,
576
- onSubmitError: f,
577
- onSubmitFinally: h,
578
- Children: S,
579
- form: P,
580
- className: x
581
- } }) });
312
+ //#endregion
313
+ //#region src/resolvers/zod/getZodResolver.ts
314
+ var $ = (e) => g(e), Ee = ({ schema: e, children: t, ...n }) => /* @__PURE__ */ c(Q, {
315
+ ...n,
316
+ resolver: $(e),
317
+ children: t
318
+ });
319
+ //#endregion
320
+ //#region src/form/with-providers/RhfUtilsFormWithProviders.tsx
321
+ function De({ getApiData: e, resolver: t, rhf: n, defaultValues: r, options: i, relay: a, onCancel: o, onSubmitInvalid: s, onBeforeSubmitInvariants: l, onBeforeSubmit: u, onSubmit: d, onSubmitSuccess: f, onSubmitError: p, onSubmitFinally: m, Children: h, form: g, formId: _, className: v }) {
322
+ let y = {
323
+ formId: _,
324
+ resolver: t,
325
+ rhf: n,
326
+ defaultValues: r,
327
+ options: i,
328
+ relay: a
329
+ }, b = {
330
+ getApiData: e,
331
+ onCancel: o,
332
+ onSubmitInvalid: s,
333
+ onBeforeSubmitInvariants: l,
334
+ onBeforeSubmit: u,
335
+ onSubmit: d,
336
+ onSubmitSuccess: f,
337
+ onSubmitError: p,
338
+ onSubmitFinally: m,
339
+ Children: h,
340
+ form: g,
341
+ className: v
342
+ };
343
+ return /* @__PURE__ */ c(Q, {
344
+ ...y,
345
+ children: /* @__PURE__ */ c(B, { ...b })
346
+ });
582
347
  }
583
- const lr = ({
584
- schema: t,
585
- ...r
586
- }) => /* @__PURE__ */ u(Kt, { ...r, resolver: J(t) });
587
- export {
588
- E as FormSubmitError,
589
- or as RhfUtilsClientConfigProvider,
590
- nr as RhfUtilsNonFieldErrorMarker,
591
- cr as RhfUtilsZodForm,
592
- ur as RhfUtilsZodFormProviders,
593
- lr as RhfUtilsZodFormWithProviders,
594
- B as useFlatFieldErrorsContext,
595
- sr as useFlatFieldErrorsContextHasOnlyOrphans,
596
- ct as useFormRequestSubmit,
597
- ir as useLastSubmitError,
598
- v as useRhfUtilsContext
599
- };
348
+ //#endregion
349
+ //#region src/resolvers/zod/with-providers/RhfUtilsZodFormWithProviders.tsx
350
+ var Oe = ({ schema: e, ...t }) => /* @__PURE__ */ c(De, {
351
+ ...t,
352
+ resolver: $(e)
353
+ });
354
+ //#endregion
355
+ export { T as FormSubmitError, v as RhfUtilsClientConfigProvider, C as RhfUtilsNonFieldErrorMarker, V as RhfUtilsZodForm, Ee as RhfUtilsZodFormProviders, Oe as RhfUtilsZodFormWithProviders, b as useFlatFieldErrorsContext, x as useFlatFieldErrorsContextHasOnlyOrphans, w as useFormRequestSubmit, D as useLastSubmitError, r as useRhfUtilsContext };