1 | import { createContext as He, useContext as Me, memo as Ve, forwardRef as ge, useRef as z, useState as $, useCallback as a, useMemo as G, useImperativeHandle as ye, useLayoutEffect as xe, useEffect as Pe } from "react";
|
2 | import { jsx as Le } from "react/jsx-runtime";
|
3 | const ze = {
|
4 | formFieldsRef: { current: [] },
|
5 | recomputeErrors: () => {
|
6 | },
|
7 | recomputeIsDirty: () => {
|
8 | },
|
9 | recomputeIsTouched: () => {
|
10 | },
|
11 | recomputeIsValidating: () => {
|
12 | },
|
13 | errors: [],
|
14 | errorsMap: {},
|
15 | submit: async () => !0,
|
16 | getFieldValue: () => {
|
17 | },
|
18 | deleteField: () => {
|
19 | },
|
20 | setIsSubmitted: () => {
|
21 | },
|
22 | reset: () => {
|
23 | },
|
24 | onChangeListenerRefs: { current: {} },
|
25 | onBlurListenerRefs: { current: {} },
|
26 | onMountListenerRefs: { current: {} },
|
27 | setIsTouched: () => {
|
28 | },
|
29 | setIsDirty: () => {
|
30 | },
|
31 | isValid: !1,
|
32 | isDirty: !1,
|
33 | isTouched: !1,
|
34 | isValidating: !1,
|
35 | isSubmitted: !1,
|
36 | value: {},
|
37 | recomputeFormValue: () => {
|
38 | }
|
39 | }, Ae = He(ze), oe = () => Me(Ae);
|
40 | function ne(e, n, i) {
|
41 | return ((u) => u instanceof Object && u.parseAsync)(i) ? i.parseAsync(e) : i(e, n);
|
42 | }
|
43 | function se(e) {
|
44 | return ((i) => i instanceof Object && i.errors)(e) ? e.errors.map((i) => i.message) : [e];
|
45 | }
|
46 | const Se = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g, Oe = /\\(\\)?/g, N = (e) => {
|
47 | const n = [];
|
48 | return e.replace(Se, (i, l, u, o) => {
|
49 | const r = u ? o.replace(Oe, "$1") : l || i;
|
50 | r && n.push(r);
|
51 | }), n;
|
52 | }, ve = (e, n, i) => {
|
53 | const l = N(n);
|
54 | let u = e;
|
55 | for (let o = 0; o < l.length; o++) {
|
56 | const r = l[o];
|
57 | o === l.length - 1 ? u[r] = i : (u[r] || (u[r] = {}), u = u[r]);
|
58 | }
|
59 | return e;
|
60 | }, je = (e, n) => {
|
61 | const i = N(n);
|
62 | let l = e;
|
63 | for (let u = 0; u < i.length; u++) {
|
64 | const o = i[u];
|
65 | if (u === i.length - 1)
|
66 | return l[o];
|
67 | l = l[o];
|
68 | }
|
69 | };
|
70 | function Ge(e, n) {
|
71 | const { onSubmit: i, children: l, memoChild: u, submitWhenInvalid: o = !1 } = e, r = z([]), [_, d] = $(!1), s = a(
|
72 | (t) => {
|
73 | const h = r.current, g = N(t).join(".");
|
74 | return h.find(
|
75 | (Z) => Z._normalizedDotName === g
|
76 | );
|
77 | },
|
78 | [r]
|
79 | ), C = a(
|
80 | (t) => {
|
81 | const h = r.current, g = N(t).join("."), Z = h.find(
|
82 | (Y) => Y._normalizedDotName === g
|
83 | );
|
84 | Z && h.splice(h.indexOf(Z), 1);
|
85 | },
|
86 | [r]
|
87 | ), R = z({}), b = z({}), S = z({}), k = a((t) => {
|
88 | r.current.forEach((h) => {
|
89 | h.setIsTouched(t);
|
90 | });
|
91 | }, []), M = a((t) => {
|
92 | r.current.forEach((h) => {
|
93 | h.setIsDirty(t);
|
94 | });
|
95 | }, []), [O, I] = $(null), [L, E] = $(null), [p, B] = $(null), [F, T] = $(null), [x, w] = $(null), [P, H] = $(null), [m, j] = $(null), f = z(!1), D = z(!1), X = z(!1), y = z(!1), c = z(!1), v = z(!1), V = a(() => r.current.reduce((t, h) => t.concat(h.errors), []), [r]), K = a(() => {
|
96 | const t = {};
|
97 | return r.current.forEach((h) => {
|
98 | const g = h.props.name;
|
99 | t[g] = h.errors;
|
100 | }), t;
|
101 | }, [r]), q = a(() => r.current.length === 0 ? !0 : r.current.every((t) => t.errors.length === 0), [r]), A = a(
|
102 | (t) => r.current.some((h) => !!h[t]),
|
103 | [r]
|
104 | ), J = a(() => r.current.reduce((t, h) => (ve(t, h.props.name, h.value), t), {}), [r]), ie = a(() => {
|
105 | if (f.current) {
|
106 | const t = V(), h = K();
|
107 | I(t), E(h);
|
108 | }
|
109 | if (D.current) {
|
110 | const t = q();
|
111 | B(t);
|
112 | }
|
113 | }, [V, K, q]), Te = a(() => {
|
114 | if (X.current) {
|
115 | const t = A("isDirty");
|
116 | T(t);
|
117 | }
|
118 | }, [A]), Fe = a(() => {
|
119 | if (y.current) {
|
120 | const t = A("isTouched");
|
121 | w(t);
|
122 | }
|
123 | }, [A]), De = a(() => {
|
124 | if (c.current) {
|
125 | const t = J();
|
126 | H(t);
|
127 | }
|
128 | }, [J]), Ee = a(() => {
|
129 | if (v.current) {
|
130 | const t = A("isValidating");
|
131 | j(t);
|
132 | }
|
133 | }, [A]), ue = a(() => {
|
134 | if (f.current = !0, O === null) {
|
135 | const t = V();
|
136 | return I(t), t;
|
137 | }
|
138 | return O;
|
139 | }, [O, V]), ae = a(() => {
|
140 | if (f.current = !0, L === null) {
|
141 | const t = K();
|
142 | return E(t), t;
|
143 | }
|
144 | return L;
|
145 | }, [L, K]), ce = a(() => {
|
146 | if (D.current = !0, p === null) {
|
147 | const t = q();
|
148 | return B(t), t;
|
149 | }
|
150 | return p;
|
151 | }, [p, q]), le = a(() => {
|
152 | if (F === null) {
|
153 | X.current = !0;
|
154 | const t = A("isDirty");
|
155 | return T(t), t;
|
156 | }
|
157 | return F;
|
158 | }, [F, A]), de = a(() => {
|
159 | if (x === null) {
|
160 | y.current = !0;
|
161 | const t = A("isTouched");
|
162 | return w(t), t;
|
163 | }
|
164 | return x;
|
165 | }, [x, A]), me = a(() => {
|
166 | if (P === null) {
|
167 | c.current = !0;
|
168 | const t = J();
|
169 | return H(t), t;
|
170 | }
|
171 | return P;
|
172 | }, [P, J]), he = a(() => {
|
173 | r.current.sort((t, h) => N(h.props.name).length - N(t.props.name).length).forEach((t) => {
|
174 | const h = t.props.resetWithValue ?? t.props.initialValue, g = (Y) => !!Y.setValues, Z = (Y) => !Y.setValues;
|
175 | t.setErrors([]), t.setIsTouched(!1), t.setIsDirty(!1), g(t) ? t.setValues({
|
176 | __value: h || [],
|
177 | __isResetting: !0
|
178 | }) : Z(t) && t.setValue({
|
179 | __value: h || "",
|
180 | __isResetting: !0
|
181 | });
|
182 | }), I([]), B(!1), T(!1), w(!1);
|
183 | }, [w, T, I]), fe = a(() => {
|
184 | if (m === null) {
|
185 | v.current = !0;
|
186 | const t = A("isValidating");
|
187 | return j(t), t;
|
188 | }
|
189 | return m;
|
190 | }, [m, A]), Q = G(() => ({
|
191 | getFieldValue: s,
|
192 | deleteField: C,
|
193 | onChangeListenerRefs: R,
|
194 | onBlurListenerRefs: b,
|
195 | onMountListenerRefs: S,
|
196 | isSubmitted: _,
|
197 | setIsSubmitted: d,
|
198 | formFieldsRef: r,
|
199 | setIsTouched: k,
|
200 | setIsDirty: M,
|
201 | recomputeErrors: ie,
|
202 | recomputeIsDirty: Te,
|
203 | recomputeIsTouched: Fe,
|
204 | recomputeFormValue: De,
|
205 | recomputeIsValidating: Ee,
|
206 | reset: he,
|
207 | get errors() {
|
208 | return ue();
|
209 | },
|
210 | get errorsMap() {
|
211 | return ae();
|
212 | },
|
213 | get isValid() {
|
214 | return ce();
|
215 | },
|
216 | get isDirty() {
|
217 | return le();
|
218 | },
|
219 | get isTouched() {
|
220 | return de();
|
221 | },
|
222 | get value() {
|
223 | return me();
|
224 | },
|
225 | get isValidating() {
|
226 | return fe();
|
227 | },
|
228 | submit: () => Promise.resolve(!0)
|
229 | }), [
|
230 | s,
|
231 | C,
|
232 | _,
|
233 | k,
|
234 | M,
|
235 | ie,
|
236 | Te,
|
237 | Fe,
|
238 | De,
|
239 | Ee,
|
240 | ue,
|
241 | ae,
|
242 | ce,
|
243 | le,
|
244 | de,
|
245 | me,
|
246 | fe,
|
247 | he
|
248 | ]), we = a(async () => {
|
249 | d(!0);
|
250 | const t = {}, h = await Promise.all(
|
251 | r.current.map(async (g) => {
|
252 | const Z = async (U) => {
|
253 | const ee = g.props[U];
|
254 | if (!ee)
|
255 | return !0;
|
256 | try {
|
257 | return U === "onSubmitValidate" && g._setIsValidating(!0), await ne(g.value, Q, ee), !0;
|
258 | } catch (_e) {
|
259 | return g.setErrors(se(_e)), !1;
|
260 | } finally {
|
261 | U === "onSubmitValidate" && g._setIsValidating(!1);
|
262 | }
|
263 | }, Y = async (U) => {
|
264 | const ee = g.props[U];
|
265 | if (!ee)
|
266 | return !0;
|
267 | try {
|
268 | return await ne(g.value, Q, ee), !0;
|
269 | } catch (_e) {
|
270 | return g.setHints(se(_e)), !1;
|
271 | }
|
272 | };
|
273 | if (g.setHints([]), await Y("onMountHint"), await Y("onChangeHint"), await Y("onBlurHint"), await Y("onSubmitHint"), g.setErrors([]), !await Z("onMountValidate") || !await Z("onChangeValidate") || !await Z("onBlurValidate") || !await Z("onSubmitValidate"))
|
274 | return !1;
|
275 | const ke = await (async () => {
|
276 | const U = g.props.onSubmitTransform;
|
277 | if (!U)
|
278 | return g.value;
|
279 | try {
|
280 | return await ne(g.value, Q, U);
|
281 | } catch (ee) {
|
282 | g.setErrors(se(ee));
|
283 | }
|
284 | })();
|
285 | return g.errors.length > 0 ? !1 : (ve(t, g.props.name, ke), !0);
|
286 | })
|
287 | );
|
288 | return !o && !h.every((g) => !!g) ? !1 : (i == null || i(t, Q), !0);
|
289 | }, [Q, i, o]), te = G(() => {
|
290 | const t = [
|
291 | "errors",
|
292 | "errorsMap",
|
293 | "isValid",
|
294 | "isDirty",
|
295 | "isTouched",
|
296 | "isValidating",
|
297 | "submit",
|
298 | "value",
|
299 | "reset"
|
300 | ], h = {
|
301 | reset: he,
|
302 | submit: we,
|
303 | get errors() {
|
304 | return ue();
|
305 | },
|
306 | get errorsMap() {
|
307 | return ae();
|
308 | },
|
309 | get isValid() {
|
310 | return ce();
|
311 | },
|
312 | get isDirty() {
|
313 | return le();
|
314 | },
|
315 | get isTouched() {
|
316 | return de();
|
317 | },
|
318 | get value() {
|
319 | return me();
|
320 | },
|
321 | get isValidating() {
|
322 | return fe();
|
323 | }
|
324 | };
|
325 | for (const g of Object.keys(Q)) {
|
326 | if (t.includes(g))
|
327 | continue;
|
328 | const Z = g;
|
329 | h[Z] = Q[Z];
|
330 | }
|
331 | return h;
|
332 | }, [
|
333 | he,
|
334 | Q,
|
335 | ue,
|
336 | ae,
|
337 | le,
|
338 | de,
|
339 | ce,
|
340 | me,
|
341 | fe,
|
342 | we
|
343 | ]);
|
344 | ye(n, () => te, [te]);
|
345 | const be = G(
|
346 | () => l(te),
|
347 |
|
348 | u ? u.concat(te) : [l, te]
|
349 | );
|
350 | return Le(Ae.Provider, { value: te, children: be });
|
351 | }
|
352 | const tt = Ve(ge(Ge)), pe = typeof window < "u", W = pe ? xe : Pe, re = (e) => !Array.isArray(e) && e !== null && typeof e == "object" && "__isResetting" in e;
|
353 | function Ce({
|
354 | listenTo: e,
|
355 | runFieldValidation: n,
|
356 | valueRef: i
|
357 | }) {
|
358 | const l = oe();
|
359 | W(() => {
|
360 | if (!e || e.length === 0)
|
361 | return;
|
362 | function u() {
|
363 | n("onChangeValidate", i.current);
|
364 | }
|
365 | function o() {
|
366 | n("onBlurValidate", i.current);
|
367 | }
|
368 | function r() {
|
369 | n("onMountValidate", i.current);
|
370 | }
|
371 | function _(s, C, R) {
|
372 | return l[s].current[C] = l[s].current[C] ?? [], l[s].current[C].push(R), () => {
|
373 | l[s].current[C].splice(
|
374 | l[s].current[C].indexOf(R),
|
375 | 1
|
376 | );
|
377 | };
|
378 | }
|
379 | const d = e.flatMap((s) => {
|
380 | const C = _(
|
381 | "onChangeListenerRefs",
|
382 | s,
|
383 | u
|
384 | ), R = _(
|
385 | "onBlurListenerRefs",
|
386 | s,
|
387 | o
|
388 | ), b = _(
|
389 | "onMountListenerRefs",
|
390 | s,
|
391 | r
|
392 | );
|
393 | return [C, R, b];
|
394 | });
|
395 | return () => d.forEach((s) => s());
|
396 | }, [l, e, n, i]);
|
397 | }
|
398 | const Re = ({
|
399 | initialValue: e,
|
400 | props: n
|
401 | }) => {
|
402 | const { name: i } = n, l = G(() => N(i).join("."), [i]), u = oe(), o = u.formFieldsRef.current.find(
|
403 | (m) => m._normalizedDotName === l
|
404 | ), [r, _] = $((o == null ? void 0 : o.errors) ?? []), [d, s] = $((o == null ? void 0 : o.hints) ?? []), [C, R] = $(
|
405 | (o == null ? void 0 : o.isTouched) ?? !1
|
406 | ), [b, S] = $(
|
407 | (o == null ? void 0 : o.isDirty) ?? !1
|
408 | ), [k, M] = $(
|
409 | (o == null ? void 0 : o.isValidating) ?? !1
|
410 | ), O = u.isSubmitted, I = a(
|
411 | (m, j) => {
|
412 | let f = n.onChangeValidate;
|
413 | m === "onBlurValidate" && (f = n == null ? void 0 : n.onBlurValidate), m === "onMountValidate" && (f = n == null ? void 0 : n.onMountValidate), f && (M(!0), ne(j, u, f).then(() => {
|
414 | _([]);
|
415 | }).catch((D) => {
|
416 | _(se(D));
|
417 | }).finally(() => {
|
418 | M(!1);
|
419 | }));
|
420 | },
|
421 | [u, n]
|
422 | ), L = a(
|
423 | (m, j) => {
|
424 | let f = n.onChangeHint;
|
425 | m === "onBlurHint" && (f = n == null ? void 0 : n.onBlurHint), m === "onMountHint" && (f = n == null ? void 0 : n.onMountHint), f && (M(!0), ne(j, u, f).then(() => {
|
426 | s([]);
|
427 | }).catch((D) => {
|
428 | s(se(D));
|
429 | }).finally(() => {
|
430 | M(!1);
|
431 | }));
|
432 | },
|
433 | [u, n]
|
434 | ), E = n.initialValue ?? e, p = z(!1), [B, F] = $((o == null ? void 0 : o.value) ?? E);
|
435 | W(() => {
|
436 | p.current || (p.current = !0, L("onMountHint", E), I("onMountValidate", E));
|
437 | });
|
438 | const T = z(B);
|
439 | T.current = B;
|
440 | const x = a(
|
441 | (m) => {
|
442 | F((j) => {
|
443 | const f = re(m) && m.__isResetting, D = re(m) ? m.__value : m, y = ((c) => typeof c == "function")(D) ? D(j) : D;
|
444 | return f || (S(y !== E), setTimeout(() => {
|
445 | var c;
|
446 | (c = u.onChangeListenerRefs.current[n.name]) == null || c.forEach(
|
447 | (v) => v()
|
448 | );
|
449 | }, 0), L("onChangeHint", y), I("onChangeValidate", y)), y;
|
450 | });
|
451 | },
|
452 | [
|
453 | E,
|
454 | I,
|
455 | L,
|
456 | u.onChangeListenerRefs,
|
457 | n.name
|
458 | ]
|
459 | ), w = G(() => r.length === 0, [r]), P = a(
|
460 | (m) => {
|
461 | I(m, T.current);
|
462 | },
|
463 | [I, T]
|
464 | ), H = a(
|
465 | (m) => {
|
466 | L(m, T.current);
|
467 | },
|
468 | [L, T]
|
469 | );
|
470 | return {
|
471 | value: B,
|
472 | setErrors: _,
|
473 | errors: r,
|
474 | hints: d,
|
475 | setHints: s,
|
476 | setIsDirty: S,
|
477 | setIsTouched: R,
|
478 | setValue: x,
|
479 | isTouched: C,
|
480 | isDirty: b,
|
481 | isValid: w,
|
482 | isValidating: k,
|
483 | isSubmitted: O,
|
484 | runFieldValidation: I,
|
485 | runFieldHintCheck: L,
|
486 | valueRef: T,
|
487 | validate: P,
|
488 | checkHint: H,
|
489 | _normalizedDotName: l,
|
490 | _setIsValidating: M
|
491 | };
|
492 | }, Ie = ({
|
493 | fieldInstanceRef: e,
|
494 | props: n,
|
495 | value: i,
|
496 | errors: l,
|
497 | isDirty: u,
|
498 | isValid: o,
|
499 | isTouched: r,
|
500 | isValidating: _,
|
501 | preserveValue: d
|
502 | }) => {
|
503 | const s = oe();
|
504 | W(() => {
|
505 | e.current.props = n;
|
506 | const C = e.current, R = s.formFieldsRef.current;
|
507 | if (s.deleteField(n.name), R.push(C), !d)
|
508 | return () => {
|
509 | s.deleteField(n.name);
|
510 | };
|
511 | }, [
|
512 | s.deleteField,
|
513 | s.formFieldsRef,
|
514 | e,
|
515 | n,
|
516 | d
|
517 | ]), W(() => {
|
518 | e.current.value = i;
|
519 | }, [e, i]), W(() => {
|
520 | e.current.errors = l;
|
521 | }, [l, e]), W(() => {
|
522 | e.current.isDirty = u;
|
523 | }, [u, e]), W(() => {
|
524 | e.current.isValid = o;
|
525 | }, [o, e]), W(() => {
|
526 | e.current.isTouched = r;
|
527 | }, [r, e]), W(() => {
|
528 | e.current.isValidating = _;
|
529 | }, [_, e]), W(() => (s.recomputeErrors(), () => {
|
530 | s.recomputeErrors();
|
531 | }), [l, s.recomputeErrors]), W(() => (s.recomputeIsTouched(), () => {
|
532 | s.recomputeIsTouched();
|
533 | }), [r, s.recomputeIsTouched]), W(() => (s.recomputeIsDirty(), () => {
|
534 | s.recomputeIsDirty();
|
535 | }), [u, s.recomputeIsDirty]), W(() => (s.recomputeIsValidating(), () => {
|
536 | s.recomputeIsValidating();
|
537 | }), [_, s.recomputeIsValidating]), W(() => (s.recomputeFormValue(), () => {
|
538 | s.recomputeFormValue();
|
539 | }), [i, s.recomputeFormValue]);
|
540 | };
|
541 | function Ze(e, n) {
|
542 | const i = oe(), { children: l, memoChild: u, preserveValue: o } = e, {
|
543 | value: r,
|
544 | setErrors: _,
|
545 | errors: d,
|
546 | hints: s,
|
547 | setHints: C,
|
548 | setIsDirty: R,
|
549 | setIsTouched: b,
|
550 | setValue: S,
|
551 | isTouched: k,
|
552 | isDirty: M,
|
553 | isValid: O,
|
554 | isValidating: I,
|
555 | isSubmitted: L,
|
556 | runFieldValidation: E,
|
557 | runFieldHintCheck: p,
|
558 | valueRef: B,
|
559 | _normalizedDotName: F,
|
560 | _setIsValidating: T,
|
561 | validate: x,
|
562 | checkHint: w
|
563 | } = Re({
|
564 | props: e,
|
565 | initialValue: ""
|
566 | });
|
567 | Ce({
|
568 | listenTo: e.listenTo,
|
569 | runFieldValidation: E,
|
570 | valueRef: B
|
571 | });
|
572 | const P = a(() => {
|
573 | b(!0), setTimeout(() => {
|
574 | var f;
|
575 | (f = i.onBlurListenerRefs.current[e.name]) == null || f.forEach((D) => D());
|
576 | }, 0), p("onBlurHint", B.current), E("onBlurValidate", B.current);
|
577 | }, [
|
578 | i.onBlurListenerRefs,
|
579 | e.name,
|
580 | E,
|
581 | b,
|
582 | B
|
583 | ]), H = G(() => ({
|
584 | value: r,
|
585 | props: e,
|
586 | setErrors: _,
|
587 | errors: d,
|
588 | setIsDirty: R,
|
589 | setIsTouched: b,
|
590 | setValue: S,
|
591 | isTouched: k,
|
592 | isDirty: M,
|
593 | isValid: O,
|
594 | isValidating: I,
|
595 | isSubmitted: L,
|
596 | onBlur: P,
|
597 | _normalizedDotName: F,
|
598 | _setIsValidating: T,
|
599 | validate: x,
|
600 | hints: s,
|
601 | setHints: C,
|
602 | checkHint: w
|
603 | }), [
|
604 | e,
|
605 | r,
|
606 | _,
|
607 | d,
|
608 | R,
|
609 | b,
|
610 | S,
|
611 | k,
|
612 | M,
|
613 | O,
|
614 | I,
|
615 | L,
|
616 | P,
|
617 | F,
|
618 | T,
|
619 | x,
|
620 | s,
|
621 | C,
|
622 | w
|
623 | ]), m = z(H);
|
624 | return Ie({
|
625 | fieldInstanceRef: m,
|
626 | props: e,
|
627 | value: r,
|
628 | errors: d,
|
629 | isValid: O,
|
630 | isDirty: M,
|
631 | isTouched: k,
|
632 | isValidating: I,
|
633 | preserveValue: o
|
634 | }), ye(n, () => H, [H]), G(
|
635 | () => l(H),
|
636 | u ? u.concat(H) : [l, H]
|
637 | );
|
638 | }
|
639 | const rt = Ve(ge(Ze)), We = {
|
640 | value: [],
|
641 | setValue: () => {
|
642 | },
|
643 | setValues: () => {
|
644 | },
|
645 | props: {
|
646 | name: ""
|
647 | },
|
648 | errors: [],
|
649 | setErrors: () => {
|
650 | },
|
651 | setIsDirty: () => {
|
652 | },
|
653 | isValid: !1,
|
654 | isValidating: !1,
|
655 | setIsTouched: () => {
|
656 | },
|
657 | isDirty: !1,
|
658 | isTouched: !1,
|
659 | _normalizedDotName: "",
|
660 | add: () => {
|
661 | },
|
662 | remove: () => {
|
663 | },
|
664 | insert: () => {
|
665 | },
|
666 | move: () => {
|
667 | },
|
668 | swap: () => {
|
669 | },
|
670 | replace: () => {
|
671 | },
|
672 | validate: () => {
|
673 | },
|
674 | _setIsValidating: () => {
|
675 | },
|
676 | hints: [],
|
677 | setHints: () => {
|
678 | },
|
679 | checkHint: () => {
|
680 | }
|
681 | }, Be = He(We), $e = () => Me(Be);
|
682 | function Ke(e, n) {
|
683 | const { children: i, memoChild: l, preserveValue: u } = e, {
|
684 | value: o,
|
685 | errors: r,
|
686 | setErrors: _,
|
687 | setValue: d,
|
688 | hints: s,
|
689 | setHints: C,
|
690 | isTouched: R,
|
691 | isDirty: b,
|
692 | isValid: S,
|
693 | isValidating: k,
|
694 | _normalizedDotName: M,
|
695 | _setIsValidating: O,
|
696 | runFieldValidation: I,
|
697 | valueRef: L,
|
698 | setIsDirty: E,
|
699 | setIsTouched: p,
|
700 | validate: B,
|
701 | checkHint: F
|
702 | } = Re({
|
703 | props: e,
|
704 | initialValue: []
|
705 | });
|
706 | Ce({
|
707 | listenTo: e.listenTo,
|
708 | runFieldValidation: I,
|
709 | valueRef: L
|
710 | });
|
711 | const T = a(
|
712 | (y, c) => {
|
713 | const v = re(c) && c.__isResetting, V = re(c) ? c.__value : c, K = (q) => {
|
714 | const A = [...q];
|
715 | return A[y] = V, A;
|
716 | };
|
717 | if (v) {
|
718 | d({
|
719 | __value: K,
|
720 | __isResetting: !0
|
721 | });
|
722 | return;
|
723 | }
|
724 | d(K);
|
725 | },
|
726 | [d]
|
727 | ), x = a(
|
728 | (y) => {
|
729 | d((c) => [...c, y]);
|
730 | },
|
731 | [d]
|
732 | ), w = a(
|
733 | (y) => {
|
734 | d((c) => {
|
735 | const v = [...c];
|
736 | return v.splice(y, 1), v;
|
737 | });
|
738 | },
|
739 | [d]
|
740 | ), P = a(
|
741 | (y, c) => {
|
742 | d((v) => {
|
743 | const V = [...v];
|
744 | return V.splice(y, 0, c), V;
|
745 | });
|
746 | },
|
747 | [d]
|
748 | ), H = a(
|
749 | (y, c) => {
|
750 | d((v) => {
|
751 | const V = [...v], K = V[y];
|
752 | return V.splice(y, 1), V.splice(c, 0, K), V;
|
753 | });
|
754 | },
|
755 | [d]
|
756 | ), m = a(
|
757 | (y, c) => {
|
758 | d((v) => {
|
759 | const V = [...v];
|
760 | return V[y] = c, V;
|
761 | });
|
762 | },
|
763 | [d]
|
764 | ), j = a(
|
765 | (y, c) => {
|
766 | d((v) => {
|
767 | const V = [...v], K = V[y];
|
768 | return V[y] = V[c], V[c] = K, V;
|
769 | });
|
770 | },
|
771 | [d]
|
772 | ), f = G(() => ({
|
773 | value: o,
|
774 | add: x,
|
775 | move: H,
|
776 | insert: P,
|
777 | remove: w,
|
778 | swap: j,
|
779 | replace: m,
|
780 | setValue: T,
|
781 | props: e,
|
782 | _normalizedDotName: M,
|
783 | _setIsValidating: O,
|
784 | errors: r,
|
785 | setErrors: _,
|
786 | hints: s,
|
787 | setHints: C,
|
788 | isValid: S,
|
789 | isValidating: k,
|
790 | setIsDirty: E,
|
791 | isDirty: b,
|
792 | setIsTouched: p,
|
793 | isTouched: R,
|
794 | setValues: d,
|
795 | validate: B,
|
796 | checkHint: F
|
797 | }), [
|
798 | o,
|
799 | x,
|
800 | H,
|
801 | P,
|
802 | w,
|
803 | j,
|
804 | m,
|
805 | T,
|
806 | e,
|
807 | M,
|
808 | O,
|
809 | r,
|
810 | _,
|
811 | s,
|
812 | C,
|
813 | S,
|
814 | k,
|
815 | E,
|
816 | b,
|
817 | p,
|
818 | R,
|
819 | d,
|
820 | B,
|
821 | F
|
822 | ]), D = z(f);
|
823 | Ie({
|
824 | value: o,
|
825 | fieldInstanceRef: D,
|
826 | isValid: S,
|
827 | isValidating: k,
|
828 | isDirty: b,
|
829 | isTouched: R,
|
830 | props: e,
|
831 | errors: r,
|
832 | preserveValue: u
|
833 | }), ye(n, () => f, [f]);
|
834 | const X = G(
|
835 | () => i(f),
|
836 | l ? l.concat(f) : [i, f]
|
837 | );
|
838 | return Le(Be.Provider, { value: f, children: X });
|
839 | }
|
840 | const nt = Ve(ge(Ke));
|
841 | function Ye(e, n) {
|
842 | const { children: i, memoChild: l, preserveValue: u } = e, {
|
843 | _normalizedDotName: o,
|
844 | _setIsValidating: r,
|
845 | errors: _,
|
846 | setErrors: d,
|
847 | hints: s,
|
848 | setHints: C,
|
849 | runFieldValidation: R,
|
850 | runFieldHintCheck: b,
|
851 | isValid: S,
|
852 | isValidating: k,
|
853 | isTouched: M,
|
854 | isSubmitted: O,
|
855 | setIsTouched: I,
|
856 | isDirty: L,
|
857 | setIsDirty: E,
|
858 | validate: p,
|
859 | checkHint: B
|
860 | } = Re({
|
861 | props: e,
|
862 | initialValue: ""
|
863 | }), F = $e(), T = oe(), x = G(() => {
|
864 | const c = N(F.props.name), v = N(e.name);
|
865 | for (const V of c)
|
866 | if (V !== v.shift())
|
867 | throw new Error(
|
868 | "You must prepend the FieldArrayItem name with the name of the parent FieldArray"
|
869 | );
|
870 | return v;
|
871 | }, [F.props.name, e.name]), w = G(() => parseInt(x[0]), [x]), P = G(() => x.slice(1), [x]), H = G(() => je(F.value[w], P.join(".")), [P, F.value, w]), m = z(H);
|
872 | m.current = H, Ce({
|
873 | listenTo: e.listenTo,
|
874 | runFieldValidation: R,
|
875 | valueRef: m
|
876 | });
|
877 | const j = a(
|
878 | (c) => {
|
879 | const v = re(c) && c.__isResetting, V = re(c) ? c.__value : c, q = ((J) => typeof J == "function")(V) ? V(F.value[w]) : V, A = { ...F.value[w] };
|
880 | if (ve(A, P.join("."), q), v) {
|
881 | F.setValue(w, {
|
882 | __value: A,
|
883 | __isResetting: !0
|
884 | });
|
885 | return;
|
886 | }
|
887 | F.setValue(w, A), E(!0), setTimeout(() => {
|
888 | var J;
|
889 | (J = T.onChangeListenerRefs.current[e.name]) == null || J.forEach(
|
890 | (ie) => ie()
|
891 | );
|
892 | }, 0), R("onChangeValidate", q);
|
893 | },
|
894 | [
|
895 | P,
|
896 | F,
|
897 | T.onChangeListenerRefs,
|
898 | w,
|
899 | e.name,
|
900 | R,
|
901 | E
|
902 | ]
|
903 | ), f = a(() => {
|
904 | I(!0), setTimeout(() => {
|
905 | var c;
|
906 | (c = T.onBlurListenerRefs.current[e.name]) == null || c.forEach((v) => v());
|
907 | }, 0), b("onBlurHint", m.current), R("onBlurValidate", m.current);
|
908 | }, [
|
909 | T.onBlurListenerRefs,
|
910 | e.name,
|
911 | b,
|
912 | R,
|
913 | I
|
914 | ]), D = G(() => ({
|
915 | setValue: j,
|
916 | errors: _,
|
917 | value: H,
|
918 | _normalizedDotName: o,
|
919 | _setIsValidating: r,
|
920 | onBlur: f,
|
921 | props: e,
|
922 | isTouched: M,
|
923 | isValid: S,
|
924 | isDirty: L,
|
925 | isValidating: k,
|
926 | setIsDirty: E,
|
927 | setErrors: d,
|
928 | setIsTouched: I,
|
929 | validate: p,
|
930 | isSubmitted: O,
|
931 | hints: s,
|
932 | setHints: C,
|
933 | checkHint: B
|
934 | }), [
|
935 | j,
|
936 | _,
|
937 | H,
|
938 | o,
|
939 | r,
|
940 | f,
|
941 | e,
|
942 | M,
|
943 | S,
|
944 | L,
|
945 | k,
|
946 | E,
|
947 | d,
|
948 | I,
|
949 | p,
|
950 | O,
|
951 | s,
|
952 | C,
|
953 | B
|
954 | ]), X = z(D);
|
955 | return Ie({
|
956 | fieldInstanceRef: X,
|
957 | props: e,
|
958 | value: H,
|
959 | errors: _,
|
960 | isValid: S,
|
961 | isDirty: L,
|
962 | isTouched: M,
|
963 | isValidating: k,
|
964 | preserveValue: u
|
965 | }), ye(n, () => D, [D]), G(
|
966 | () => i(D),
|
967 | l ? l.concat(D) : [i, D]
|
968 | );
|
969 | }
|
970 | const st = Ve(ge(Ye));
|
971 | export {
|
972 | rt as Field,
|
973 | nt as FieldArray,
|
974 | Be as FieldArrayContext,
|
975 | st as FieldArrayItem,
|
976 | Ye as FieldArrayItemComp,
|
977 | tt as Form,
|
978 | Ae as FormContext,
|
979 | ve as fillPath,
|
980 | je as getPath,
|
981 | se as getValidationError,
|
982 | We as initialFieldArrayContext,
|
983 | ze as initialFormContext,
|
984 | re as isInternal,
|
985 | N as stringToPath,
|
986 | $e as useFieldArrayContext,
|
987 | Re as useFieldLike,
|
988 | Ie as useFieldLikeSync,
|
989 | oe as useFormContext,
|
990 | Ce as useListenToListenToArray,
|
991 | ne as validate
|
992 | };
|