@paragrav/rhf-utils 0.62.0 → 0.64.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.
@@ -1,605 +1,350 @@
1
- import { jsx as c, jsxs as L } from "react/jsx-runtime";
2
- import u from "react";
3
- import { F as Y, c as B, u as v, g as tt, i as D, _ as rt, a as et } from "./FormRelaySetter.mjs";
4
- import { Controller as ot, useFormContext as b, useFormState as k, useWatch as st, useForm as nt, FormProvider as it } from "react-hook-form";
5
- import { zodResolver as ct } from "@hookform/resolvers/zod";
6
- const M = u.createContext(void 0), nr = ({
7
- config: t,
8
- children: r
9
- }) => {
10
- const [e] = u.useState(t ?? {});
11
- return /* @__PURE__ */ c(M.Provider, { value: e, children: /* @__PURE__ */ c(Y, { children: r }) });
12
- }, {
13
- Provider: ut,
14
- useRequired: N
15
- } = B(), ir = () => {
16
- const t = N();
17
- return u.useMemo(() => !t.hasErrors || !t.hasOrphans ? !1 : Object.keys(t.orphans).length === Object.keys(t.all).length, [t]);
18
- }, q = "data-rhfutils-nonfield-error-marker-path", cr = ({ path: t }) => {
19
- const r = {
20
- [q]: t
21
- };
22
- return /* @__PURE__ */ c(
23
- "div",
24
- {
25
- role: "alert",
26
- "aria-hidden": "true",
27
- style: { display: "none" },
28
- ...r
29
- }
30
- );
31
- }, lt = (t) => u.useCallback(
32
- (r) => {
33
- if (!t.current) throw new Error();
34
- at(t.current, r);
35
- },
36
- [t]
37
- ), at = (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: mt,
47
- useRequired: H
48
- } = B(), ur = () => H().current, dt = u.lazy(
49
- () => import("@hookform/devtools").then((t) => ({
50
- default: t.DevTool
51
- }))
52
- ), ft = ({ config: t }) => {
53
- const [r] = u.useState(t);
54
- if (!r) return null;
55
- const e = typeof r == "object" ? r : void 0;
56
- return /* @__PURE__ */ c(u.Suspense, { children: /* @__PURE__ */ c(dt, { ...e }) });
57
- }, ht = () => {
58
- const t = v();
59
- return /* @__PURE__ */ c(ft, { config: t.options.devTool });
60
- }, z = (t) => /* @__PURE__ */ c(ot, { ...t }), R = () => {
61
- const t = u.useContext(M);
62
- if (t === void 0) throw new Error();
63
- return t;
64
- }, Ft = () => {
65
- const t = v(), r = b();
66
- return {
67
- utils: t,
68
- rhf: r,
69
- FormSubmitError: E
70
- };
71
- }, bt = (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
- }, St = (t) => {
80
- const { useCanFormBeCancelled: r } = R(), e = Ft();
81
- return bt(
82
- t,
83
- r,
84
- e
85
- );
86
- }, pt = ({
87
- onCancel: t,
88
- Children: r
89
- }) => {
90
- const e = b(), o = v(), s = St(t);
91
- return /* @__PURE__ */ c(
92
- r,
93
- {
94
- ...o,
95
- rhf: e,
96
- onCancel: s,
97
- Controller: z,
98
- FormSubmitError: E
99
- }
100
- );
101
- }, Et = (t, r) => {
102
- Object.entries(r).forEach(([e, o]) => {
103
- t.setError(e, {
104
- type: "FormSubmitFieldError",
105
- // default
106
- ...o
107
- });
108
- });
109
- }, vt = ({
110
- children: t
111
- }) => t, Rt = (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
- }), Ct = (t, r, e) => {
117
- const o = R();
118
- return {
119
- id: r,
120
- ...Rt(
121
- o.defaults?.form,
122
- t,
123
- e
124
- )
125
- };
126
- }, gt = (t) => {
127
- const r = H();
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
- }, Ot = ({
136
- onValid: t,
137
- onInvalid: r,
138
- onError: e,
139
- onFinally: o
140
- }) => {
141
- const s = b(), { options: n, lastSubmitStateRef: i } = v(), l = s.handleSubmit(t, r), m = gt(l);
142
- return (a) => {
143
- i.current = "submitting", n.stopSubmitPropagation && a.stopPropagation(), Promise.resolve(m(a)).then(() => {
144
- i.current = "success";
145
- }).catch((h) => {
146
- i.current = "error", e(h, a);
147
- }).finally(() => {
148
- o?.();
149
- });
150
- };
151
- }, Pt = ({
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: l,
161
- // other
162
- form: m,
163
- className: a,
164
- //
165
- children: h
166
- }) => {
167
- const d = b(), f = v(), S = R(), P = Ct(
168
- m,
169
- f.formId,
170
- {
171
- className: a
172
- }
173
- ), y = async (F, w) => {
174
- const p = d.getValues(), K = t?.(F), C = { input: p, output: F, api: K }, g = {
175
- utils: f,
176
- rhf: d,
177
- FormSubmitError: E
178
- }, O = w;
179
- e && await yt(e, C, g, O), await Promise.resolve(o?.(C, g, O));
180
- const X = await Promise.resolve(
181
- s?.(C, g, O)
182
- );
183
- f.lastSubmitStateRef.current = "success", await Promise.resolve(
184
- n?.(X, C, g, O)
185
- );
186
- };
187
- function x(F, w) {
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 && Et(d, p), i?.(
196
- F,
197
- {
198
- utils: f,
199
- errors: p,
200
- rhf: d
201
- },
202
- w
203
- // `as` required due to conditional generic
204
- );
205
- }
206
- const U = Ot({
207
- onValid: y,
208
- onInvalid: r,
209
- onError: x,
210
- onFinally: l
211
- }), W = {
212
- ...f,
213
- rhf: d,
214
- Controller: z,
215
- FormSubmitError: E
216
- }, _ = S.FormComponent ?? "form", G = S.FormChildren ?? vt;
217
- return /* @__PURE__ */ c(
218
- _,
219
- {
220
- ...P,
221
- id: f.formId,
222
- ref: f.formRef,
223
- onSubmit: U,
224
- children: /* @__PURE__ */ c(G, { ...W, children: h })
225
- }
226
- );
227
- }, yt = async (t, r, e, o) => {
228
- const n = (await Promise.resolve(
229
- t(r, e, o)
230
- )).filter(({ valid: i }) => !i).map(({ field: i, message: l }) => [i, { message: l }]);
231
- if (n.length)
232
- throw new e.FormSubmitError(
233
- Object.fromEntries(n)
234
- );
235
- }, A = ({
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: l,
245
- onSubmitFinally: m,
246
- //
247
- Children: a,
248
- // attrs
249
- form: h,
250
- className: d
251
- }) => /* @__PURE__ */ L(
252
- Pt,
253
- {
254
- getApiData: t,
255
- onSubmitInvalid: e,
256
- onBeforeSubmitInvariants: o,
257
- onBeforeSubmit: s,
258
- onSubmit: n,
259
- onSubmitSuccess: i,
260
- onSubmitError: l,
261
- onSubmitFinally: m,
262
- form: h,
263
- className: d,
264
- children: [
265
- /* @__PURE__ */ c(ht, {}),
266
- /* @__PURE__ */ c(
267
- pt,
268
- {
269
- onCancel: r,
270
- Children: a
271
- }
272
- )
273
- ]
274
- }
275
- ), lr = (t) => /* @__PURE__ */ c(A, { ...t }), V = (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, xt = ([
282
- ,
283
- t
284
- ]) => Z(t), Ut = V(
285
- xt
286
- ), wt = (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]) => It(
300
- q + e,
301
- t + o
302
- )
303
- ).join(", ")
304
- ), It = (t, r) => `[role="alert"][${t}="${r}"]`, kt = (t, r) => !!r.querySelector(
305
- wt(t)
306
- ), T = "root", J = (t) => t === T || t.startsWith(T + "."), Vt = (t, r, e) => (
307
- // non-field
308
- !Z(r) && // non-root
309
- !J(t) && // not marked in DOM
310
- !kt(t, e)
311
- ), Wt = (t) => ([r, e]) => Vt(r, e, t), _t = (t, r) => V(
312
- Wt(r)
313
- )(t), Dt = ([t]) => J(t), Tt = V(
314
- Dt
315
- ), jt = ({
316
- formRef: t,
317
- children: r
318
- }) => {
319
- const { errors: e } = k(), o = tt(e), s = (
320
- // eslint-disable-next-line react-hooks/refs
321
- t.current ? _t(
322
- o,
323
- // eslint-disable-next-line react-hooks/refs
324
- t.current
325
- ) : {}
326
- ), n = {
327
- all: o,
328
- fields: Ut(o),
329
- roots: Tt(o),
330
- orphans: s,
331
- // computed
332
- hasErrors: !D(e),
333
- hasOrphans: !D(s)
334
- };
335
- return /* @__PURE__ */ c(ut, { value: n, children: r });
336
- }, Lt = ({
337
- children: t
338
- }) => {
339
- const [r, e] = u.useState(void 0);
340
- return /* @__PURE__ */ c(
341
- mt,
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
- }, Bt = (t, r, e, o = "debug") => {
356
- console[o](t, { values: r, errors: e });
357
- }, Mt = (t) => {
358
- const r = b(), e = N();
359
- u.useEffect(() => {
360
- if (!e.hasErrors) return;
361
- const o = t?.console?.(e);
362
- o && Bt(
363
- o.message ?? j,
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 : j);
371
- }, [e]);
372
- }, j = "Form errors", Nt = (t) => {
373
- const r = u.useRef(!1);
374
- return u.useEffect(() => {
375
- t && (r.current = !0);
376
- }, [t]), [r, () => {
377
- r.current = !1;
378
- }];
379
- }, qt = (t, r) => {
380
- const { isSubmitSuccessful: e, isSubmitting: o } = k(), [s, n] = Nt(o);
381
- u.useEffect(() => {
382
- t && (o || s.current && (n(), t(e)));
383
- }, [o]);
384
- }, Ht = (t) => {
385
- const { getValues: r, reset: e } = b();
386
- qt((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
- }, zt = () => {
414
- const t = u.useRef(I);
415
- return u.useEffect(() => (t.current && (t.current = !I), () => {
416
- t.current = I;
417
- }), []), t;
418
- }, I = !0, At = ({
419
- onChange: t,
420
- delay: r
421
- }) => {
422
- const e = u.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 u.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 = lt(t), [o] = At({
441
- delay: r?.debounce ?? 0,
442
- // on change value after debounce
443
- onChange: () => {
444
- e();
445
- }
446
- });
447
- return o;
448
- }, Jt = (t, r) => {
449
- const e = Zt(t, {
450
- debounce: r?.debounce
451
- }), o = st(), { isValid: s, isDirty: n, isValidating: i, isSubmitting: l } = k(), m = u.useMemo(() => JSON.stringify(o), [o]), a = zt();
452
- u.useEffect(
453
- // on form values/state change, call callback
454
- () => {
455
- a.current || i || l || 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
- }, Qt = ({
465
- formId: t,
466
- formRef: r,
467
- options: e,
468
- //
469
- children: o
470
- }) => {
471
- const [s] = u.useState(e), n = R();
472
- Mt(n.fieldErrors?.output), (s.submitOnWatch ? Jt : void 0)?.(
473
- r,
474
- s.submitOnWatch
475
- ), Ht(s.resetOnSubmitted);
476
- const i = u.useRef(null);
477
- return /* @__PURE__ */ c(
478
- rt,
479
- {
480
- value: {
481
- formId: t,
482
- formRef: r,
483
- options: e,
484
- lastSubmitStateRef: i
485
- },
486
- children: o
487
- }
488
- );
489
- }, $t = (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
- }), Gt = (t, r) => u.useMemo(
500
- () => $t(t.defaults?.options, r),
501
- // eslint-disable-next-line react-hooks/exhaustive-deps
502
- [JSON.stringify(r)]
503
- ), Kt = (t, r) => ({
504
- ...t,
505
- ...r
506
- }), Xt = (t, r, e, o) => nt({
507
- ...Kt(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 }) => /* @__PURE__ */ c("div", {
17
+ role: "alert",
18
+ "aria-hidden": "true",
19
+ style: { display: "none" },
20
+ [S]: e
21
+ }), w = (e) => s.useCallback((t) => {
22
+ if (!e.current) throw Error();
23
+ ee(e.current, t);
24
+ }, [e]), ee = (e, t) => {
25
+ e.requestSubmit(t);
26
+ }, T = class extends Error {
27
+ constructor(e, t) {
28
+ super(t), this.errors = e;
29
+ }
30
+ }, { Provider: te, useRequired: E } = o(), D = () => E().current, O = s.lazy(() => import("@hookform/devtools").then((e) => ({ default: e.DevTool }))), k = ({ config: e }) => {
31
+ let [t] = s.useState(e);
32
+ if (!t) return null;
33
+ let n = typeof t == "object" ? t : void 0;
34
+ return /* @__PURE__ */ c(s.Suspense, { children: /* @__PURE__ */ c(O, { ...n }) });
35
+ }, A = () => /* @__PURE__ */ c(k, { config: r().options.devTool }), j = (e) => /* @__PURE__ */ c(u, { ...e }), M = () => {
36
+ let e = s.useContext(_);
37
+ if (e === void 0) throw Error();
38
+ return e;
39
+ }, ne = () => ({
40
+ utils: r(),
41
+ rhf: p(),
42
+ FormSubmitError: T
43
+ }), re = (e, t, n) => {
44
+ let r = t?.();
45
+ return async () => {
46
+ if (e && await Promise.resolve(r?.(n)) !== !1) return Promise.resolve(e());
47
+ };
48
+ }, ie = (e) => {
49
+ let { useCanFormBeCancelled: t } = M();
50
+ return re(e, t, ne());
51
+ }, ae = ({ onCancel: e, Children: t }) => {
52
+ let n = p(), i = r(), a = ie(e);
53
+ return /* @__PURE__ */ c(t, {
54
+ ...i,
55
+ rhf: n,
56
+ onCancel: a,
57
+ Controller: j,
58
+ FormSubmitError: T
59
+ });
60
+ }, N = (e, t) => {
61
+ Object.entries(t).forEach(([t, n]) => {
62
+ e.setError(t, {
63
+ type: "FormSubmitFieldError",
64
+ ...n
65
+ });
66
+ });
67
+ }, P = (e, t, n) => ({
68
+ ...e,
69
+ ...t,
70
+ className: [
71
+ e?.className,
72
+ t?.className,
73
+ n?.className
74
+ ].filter(Boolean).join(" ").trim() || void 0
75
+ }), F = (e, t, n) => ({
76
+ id: t,
77
+ ...P(M().defaults?.form, e, n)
78
+ }), I = (e) => {
79
+ let t = E();
80
+ return async function(n) {
81
+ try {
82
+ return t.reset(), await e(n);
83
+ } catch (e) {
84
+ throw t.set(e, n), e;
85
+ }
86
+ };
87
+ }, L = ({ onValid: e, onInvalid: t, onError: n, onFinally: i }) => {
88
+ let a = p(), { options: o, lastSubmitStateRef: s } = r(), c = I(a.handleSubmit(e, t));
89
+ return (e) => {
90
+ s.current = "submitting", o.stopSubmitPropagation && e.stopPropagation(), Promise.resolve(c(e)).then(() => {
91
+ s.current = "success";
92
+ }).catch((t) => {
93
+ s.current = "error", n(t, e);
94
+ }).finally(() => {
95
+ i?.();
96
+ });
97
+ };
98
+ }, R = ({ getApiData: e, onSubmitInvalid: t, onBeforeSubmitInvariants: n, onBeforeSubmit: i, onSubmit: a, onSubmitSuccess: o, onSubmitError: l, onSubmitFinally: u, form: d, className: f, children: m }) => {
99
+ let h = p(), g = r(), _ = M(), v = F(d, g.formId, { className: f }), y = async (t, r) => {
100
+ let s = {
101
+ input: h.getValues(),
102
+ output: t,
103
+ api: e?.(t)
104
+ }, c = {
105
+ utils: g,
106
+ rhf: h,
107
+ FormSubmitError: T
108
+ }, l = r;
109
+ n && await z(n, s, c, l), await Promise.resolve(i?.(s, c, l));
110
+ let u = await Promise.resolve(a?.(s, c, l));
111
+ g.lastSubmitStateRef.current = "success", await Promise.resolve(o?.(u, s, c, l));
112
+ };
113
+ function b(e, t) {
114
+ let n = e instanceof T ? e.errors : _.onSubmitErrorUnknown?.(e);
115
+ n && N(h, n), l?.(e, {
116
+ utils: g,
117
+ errors: n,
118
+ rhf: h
119
+ }, t);
120
+ }
121
+ let x = L({
122
+ onValid: y,
123
+ onInvalid: t,
124
+ onError: b,
125
+ onFinally: u
126
+ }), S = s.useCallback(() => m, []), C = {
127
+ ...g,
128
+ rhf: h,
129
+ Controller: j,
130
+ FormSubmitError: T,
131
+ Outlet: S
132
+ };
133
+ return /* @__PURE__ */ c(_.FormComponent ?? "form", {
134
+ ...v,
135
+ id: g.formId,
136
+ ref: g.formRef,
137
+ onSubmit: x,
138
+ children: _.FormInjector ? /* @__PURE__ */ c(_.FormInjector, { ...C }) : m
139
+ });
140
+ }, z = async (e, t, n, r) => {
141
+ let i = (await Promise.resolve(e(t, n, r))).filter(({ validate: e }) => !e).map(({ field: e, message: t }) => [e, { message: t }]);
142
+ if (i.length) throw new n.FormSubmitError(Object.fromEntries(i));
143
+ }, 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, {
144
+ getApiData: e,
145
+ onSubmitInvalid: n,
146
+ onBeforeSubmitInvariants: r,
147
+ onBeforeSubmit: i,
148
+ onSubmit: a,
149
+ onSubmitSuccess: o,
150
+ onSubmitError: s,
151
+ onSubmitFinally: u,
152
+ form: f,
153
+ className: p,
154
+ children: [/* @__PURE__ */ c(A, {}), /* @__PURE__ */ c(ae, {
155
+ onCancel: t,
156
+ Children: d
157
+ })]
158
+ }), 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."), oe = (e, t, n) => !U(t) && !J(e) && !q(e, n), se = (e) => ([t, n]) => oe(t, n, e), ce = (e, t) => H(se(t))(e), le = H(([e]) => J(e)), ue = ({ formRef: e, children: t }) => {
159
+ let { errors: r } = m(), a = i(r), o = e.current ? ce(a, e.current) : {};
160
+ return /* @__PURE__ */ c(y, {
161
+ value: {
162
+ all: a,
163
+ fields: W(a),
164
+ roots: le(a),
165
+ orphans: o,
166
+ hasErrors: !n(r),
167
+ hasOrphans: !n(o)
168
+ },
169
+ children: t
170
+ });
171
+ }, de = ({ children: e }) => {
172
+ let [t, n] = s.useState(void 0);
173
+ return /* @__PURE__ */ c(te, {
174
+ value: {
175
+ current: t,
176
+ reset: () => {
177
+ n(void 0);
178
+ },
179
+ set: (e, t) => {
180
+ n({
181
+ error: e,
182
+ event: t
183
+ });
184
+ }
185
+ },
186
+ children: e
187
+ });
188
+ }, fe = (e, t, n, r = "debug") => {
189
+ console[r](e, {
190
+ values: t,
191
+ errors: n
192
+ });
193
+ }, pe = (e) => {
194
+ let t = p(), n = b();
195
+ s.useEffect(() => {
196
+ if (!n.hasErrors) return;
197
+ let r = e?.console?.(n);
198
+ r && fe(r.message ?? Y, t.getValues(), n, r.type);
199
+ let i = e?.throw?.(n);
200
+ if (i) throw Error(typeof i == "string" ? i : Y);
201
+ }, [n]);
202
+ }, Y = "Form errors", me = (e) => {
203
+ let t = s.useRef(!1);
204
+ return s.useEffect(() => {
205
+ e && (t.current = !0);
206
+ }, [e]), [t, () => {
207
+ t.current = !1;
208
+ }];
209
+ }, he = (e, t) => {
210
+ let { isSubmitSuccessful: n, isSubmitting: r } = m(), [i, a] = me(r);
211
+ s.useEffect(() => {
212
+ e && (r || i.current && (a(), (t?.successful === void 0 || n === t.successful) && e(n)));
213
+ }, [r]);
214
+ }, ge = (e) => {
215
+ let { getValues: t, reset: n } = p();
216
+ he((r) => {
217
+ if (!e) return;
218
+ let i = e[r ? "success" : "error"]?.values;
219
+ if (!i) return;
220
+ let a = i === "current";
221
+ n(a ? t() : void 0, {
222
+ keepIsSubmitSuccessful: !0,
223
+ keepIsSubmitted: !0,
224
+ keepSubmitCount: !0,
225
+ keepValues: a,
226
+ keepErrors: !0
227
+ });
228
+ });
229
+ }, _e = () => {
230
+ let e = s.useRef(X);
231
+ return s.useEffect(() => (e.current &&= !X, () => {
232
+ e.current = X;
233
+ }), []), e;
234
+ }, X = !0, ve = ({ onChange: e, delay: t }) => {
235
+ let n = s.useRef(null), r = () => {
236
+ n.current &&= (e(n.current.value), null);
237
+ }, i = () => {
238
+ n.current && clearTimeout(n.current.timer);
239
+ };
240
+ return s.useEffect(() => i, []), [(e) => {
241
+ i(), n.current = {
242
+ value: e,
243
+ timer: setTimeout(r, t)
244
+ };
245
+ }];
246
+ }, ye = (e, t) => {
247
+ let n = w(e), [r] = ve({
248
+ delay: t?.debounce ?? 0,
249
+ onChange: () => {
250
+ n();
251
+ }
252
+ });
253
+ return r;
254
+ }, be = (e, t) => {
255
+ let n = ye(e, { debounce: t?.debounce }), r = h(), { isValid: i, isDirty: a, isValidating: o, isSubmitting: c } = m(), l = s.useMemo(() => JSON.stringify(r), [r]), u = _e();
256
+ s.useEffect(() => {
257
+ u.current || o || c || i && a && n(r);
258
+ }, [
259
+ i,
260
+ a,
261
+ l,
262
+ o
263
+ ]);
264
+ }, xe = ({ formId: t, formRef: n, options: r, children: i }) => {
265
+ let [a] = s.useState(r);
266
+ return pe(M().fieldErrors?.output), (a.submitOnWatch ? be : void 0)?.(n, a.submitOnWatch), ge(a.resetOnSubmitted), /* @__PURE__ */ c(e, {
267
+ value: {
268
+ formId: t,
269
+ formRef: n,
270
+ options: r,
271
+ lastSubmitStateRef: s.useRef(null)
272
+ },
273
+ children: i
274
+ });
275
+ }, Se = (e, t) => ({
276
+ ...e,
277
+ ...t,
278
+ ...(e?.resetOnSubmitted || t?.resetOnSubmitted) && { resetOnSubmitted: {
279
+ ...e?.resetOnSubmitted,
280
+ ...t?.resetOnSubmitted
281
+ } }
282
+ }), Z = (e, t) => s.useMemo(() => Se(e.defaults?.options, t), [JSON.stringify(t)]), Ce = (e, t) => ({
283
+ ...e,
284
+ ...t
285
+ }), we = (e, t, n, r) => f({
286
+ ...Ce(t.defaults?.rhf, e),
287
+ resolver: r,
288
+ defaultValues: n
510
289
  });
511
- function Q({
512
- formId: t,
513
- rhf: r,
514
- resolver: e,
515
- defaultValues: o,
516
- options: s,
517
- relay: n,
518
- //
519
- children: i
520
- }) {
521
- const l = R(), m = u.useId(), a = t || m, h = u.useRef(null), d = Xt(
522
- r,
523
- l,
524
- o,
525
- e
526
- ), f = Gt(l, s);
527
- return /* @__PURE__ */ c(it, { ...d, children: /* @__PURE__ */ c(jt, { formRef: h, children: /* @__PURE__ */ L(
528
- Qt,
529
- {
530
- formId: a,
531
- formRef: h,
532
- options: f,
533
- children: [
534
- /* @__PURE__ */ c(et, { options: n }),
535
- /* @__PURE__ */ c(Lt, { children: i })
536
- ]
537
- }
538
- ) }) });
290
+ //#endregion
291
+ //#region src/form/providers/RhfUtilsFormProviders.tsx
292
+ function Q({ formId: e, rhf: t, resolver: n, defaultValues: r, options: i, relay: o, children: u }) {
293
+ let f = M(), p = s.useId(), m = e || p, h = s.useRef(null), g = we(t, f, r, n), _ = Z(f, i);
294
+ return /* @__PURE__ */ c(d, {
295
+ ...g,
296
+ children: /* @__PURE__ */ c(ue, {
297
+ formRef: h,
298
+ children: /* @__PURE__ */ l(xe, {
299
+ formId: m,
300
+ formRef: h,
301
+ options: _,
302
+ children: [/* @__PURE__ */ c(a, { options: o }), /* @__PURE__ */ c(de, { children: u })]
303
+ })
304
+ })
305
+ });
539
306
  }
540
- const $ = (t) => ct(t), ar = ({
541
- schema: t,
542
- children: r,
543
- ...e
544
- }) => /* @__PURE__ */ c(Q, { ...e, resolver: $(t), children: r });
545
- function Yt({
546
- getApiData: t,
547
- // provider
548
- resolver: r,
549
- rhf: e,
550
- defaultValues: o,
551
- options: s,
552
- relay: n,
553
- // form
554
- onCancel: i,
555
- onSubmitInvalid: l,
556
- onBeforeSubmitInvariants: m,
557
- onBeforeSubmit: a,
558
- onSubmit: h,
559
- onSubmitSuccess: d,
560
- onSubmitError: f,
561
- onSubmitFinally: S,
562
- Children: P,
563
- form: y,
564
- formId: x,
565
- className: U
566
- }) {
567
- return /* @__PURE__ */ c(Q, { ...{
568
- formId: x,
569
- resolver: r,
570
- rhf: e,
571
- defaultValues: o,
572
- options: s,
573
- relay: n
574
- }, children: /* @__PURE__ */ c(A, { ...{
575
- getApiData: t,
576
- onCancel: i,
577
- onSubmitInvalid: l,
578
- onBeforeSubmitInvariants: m,
579
- onBeforeSubmit: a,
580
- onSubmit: h,
581
- onSubmitSuccess: d,
582
- onSubmitError: f,
583
- onSubmitFinally: S,
584
- Children: P,
585
- form: y,
586
- className: U
587
- } }) });
307
+ //#endregion
308
+ //#region src/resolvers/zod/getZodResolver.ts
309
+ var $ = (e) => g(e), Te = ({ schema: e, children: t, ...n }) => /* @__PURE__ */ c(Q, {
310
+ ...n,
311
+ resolver: $(e),
312
+ children: t
313
+ });
314
+ //#endregion
315
+ //#region src/form/with-providers/RhfUtilsFormWithProviders.tsx
316
+ function Ee({ 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 }) {
317
+ let y = {
318
+ formId: _,
319
+ resolver: t,
320
+ rhf: n,
321
+ defaultValues: r,
322
+ options: i,
323
+ relay: a
324
+ }, b = {
325
+ getApiData: e,
326
+ onCancel: o,
327
+ onSubmitInvalid: s,
328
+ onBeforeSubmitInvariants: l,
329
+ onBeforeSubmit: u,
330
+ onSubmit: d,
331
+ onSubmitSuccess: f,
332
+ onSubmitError: p,
333
+ onSubmitFinally: m,
334
+ Children: h,
335
+ form: g,
336
+ className: v
337
+ };
338
+ return /* @__PURE__ */ c(Q, {
339
+ ...y,
340
+ children: /* @__PURE__ */ c(B, { ...b })
341
+ });
588
342
  }
589
- const mr = ({
590
- schema: t,
591
- ...r
592
- }) => /* @__PURE__ */ c(Yt, { ...r, resolver: $(t) });
593
- export {
594
- E as FormSubmitError,
595
- nr as RhfUtilsClientConfigProvider,
596
- cr as RhfUtilsNonFieldErrorMarker,
597
- lr as RhfUtilsZodForm,
598
- ar as RhfUtilsZodFormProviders,
599
- mr as RhfUtilsZodFormWithProviders,
600
- N as useFlatFieldErrorsContext,
601
- ir as useFlatFieldErrorsContextHasOnlyOrphans,
602
- lt as useFormRequestSubmit,
603
- ur as useLastSubmitError,
604
- v as useRhfUtilsContext
605
- };
343
+ //#endregion
344
+ //#region src/resolvers/zod/with-providers/RhfUtilsZodFormWithProviders.tsx
345
+ var De = ({ schema: e, ...t }) => /* @__PURE__ */ c(Ee, {
346
+ ...t,
347
+ resolver: $(e)
348
+ });
349
+ //#endregion
350
+ export { T as FormSubmitError, v as RhfUtilsClientConfigProvider, C as RhfUtilsNonFieldErrorMarker, V as RhfUtilsZodForm, Te as RhfUtilsZodFormProviders, De as RhfUtilsZodFormWithProviders, b as useFlatFieldErrorsContext, x as useFlatFieldErrorsContextHasOnlyOrphans, w as useFormRequestSubmit, D as useLastSubmitError, r as useRhfUtilsContext };