UNPKG

dgz-ui-shared

Version:

Custom ui library using React.js, Shadcn/ui, TailwindCSS, Typescript, dgz-ui library

1,008 lines (1,007 loc) 33.5 kB
import B from "react"; var de = (e) => e.type === "checkbox", re = (e) => e instanceof Date, I = (e) => e == null; const tt = (e) => typeof e == "object"; var S = (e) => !I(e) && !Array.isArray(e) && tt(e) && !re(e), _t = (e) => S(e) && e.target ? de(e.target) ? e.target.checked : e.target.value : e, Vt = (e) => e.substring(0, e.search(/\.\d+(\.|$)/)) || e, Ft = (e, s) => e.has(Vt(s)), At = (e) => { const s = e.constructor && e.constructor.prototype; return S(s) && s.hasOwnProperty("isPrototypeOf"); }, Se = typeof window < "u" && typeof window.HTMLElement < "u" && typeof document < "u"; function C(e) { let s; const r = Array.isArray(e), a = typeof FileList < "u" ? e instanceof FileList : !1; if (e instanceof Date) s = new Date(e); else if (!(Se && (e instanceof Blob || a)) && (r || S(e))) if (s = r ? [] : Object.create(Object.getPrototypeOf(e)), !r && !At(e)) s = e; else for (const u in e) e.hasOwnProperty(u) && (s[u] = C(e[u])); else return e; return s; } var be = (e) => /^\w*$/.test(e), D = (e) => e === void 0, Oe = (e) => Array.isArray(e) ? e.filter(Boolean) : [], Te = (e) => Oe(e.replace(/["|']|\]/g, "").split(/\.|\[/)), h = (e, s, r) => { if (!s || !S(e)) return r; const a = (be(s) ? [s] : Te(s)).reduce((u, n) => I(u) ? u : u[n], e); return D(a) || a === e ? D(e[s]) ? r : e[s] : a; }, Y = (e) => typeof e == "boolean", m = (e, s, r) => { let a = -1; const u = be(s) ? [s] : Te(s), n = u.length, d = n - 1; for (; ++a < n; ) { const y = u[a]; let U = r; if (a !== d) { const p = e[y]; U = S(p) || Array.isArray(p) ? p : isNaN(+u[a + 1]) ? {} : []; } if (y === "__proto__" || y === "constructor" || y === "prototype") return; e[y] = U, e = e[y]; } }; const pe = { BLUR: "blur", FOCUS_OUT: "focusout" }, W = { onBlur: "onBlur", onChange: "onChange", onSubmit: "onSubmit", onTouched: "onTouched", all: "all" }, J = { max: "max", min: "min", maxLength: "maxLength", minLength: "minLength", pattern: "pattern", required: "required", validate: "validate" }, xt = B.createContext(null); xt.displayName = "HookFormContext"; var mt = (e, s, r, a = !0) => { const u = { defaultValues: s._defaultValues }; for (const n in e) Object.defineProperty(u, n, { get: () => { const d = n; return s._proxyFormState[d] !== W.all && (s._proxyFormState[d] = !a || W.all), e[d]; } }); return u; }; const wt = typeof window < "u" ? B.useLayoutEffect : B.useEffect; var N = (e) => typeof e == "string", Dt = (e, s, r, a, u) => N(e) ? (a && s.watch.add(e), h(r, e, u)) : Array.isArray(e) ? e.map((n) => (a && s.watch.add(n), h(r, n))) : (a && (s.watchAll = !0), r), ke = (e) => I(e) || !tt(e); function ee(e, s, r = /* @__PURE__ */ new WeakSet()) { if (ke(e) || ke(s)) return Object.is(e, s); if (re(e) && re(s)) return e.getTime() === s.getTime(); const a = Object.keys(e), u = Object.keys(s); if (a.length !== u.length) return !1; if (r.has(e) || r.has(s)) return !0; r.add(e), r.add(s); for (const n of a) { const d = e[n]; if (!u.includes(n)) return !1; if (n !== "ref") { const y = s[n]; if (re(d) && re(y) || S(d) && S(y) || Array.isArray(d) && Array.isArray(y) ? !ee(d, y, r) : !Object.is(d, y)) return !1; } } return !0; } var kt = (e, s, r, a, u) => s ? { ...r[e], types: { ...r[e] && r[e].types ? r[e].types : {}, [a]: u || !0 } } : {}, oe = (e) => Array.isArray(e) ? e : [e], $e = () => { let e = []; return { get observers() { return e; }, next: (u) => { for (const n of e) n.next && n.next(u); }, subscribe: (u) => (e.push(u), { unsubscribe: () => { e = e.filter((n) => n !== u); } }), unsubscribe: () => { e = []; } }; }; function rt(e, s) { const r = {}; for (const a in e) if (e.hasOwnProperty(a)) { const u = e[a], n = s[a]; if (u && S(u) && n) { const d = rt(u, n); S(d) && (r[a] = d); } else e[a] && (r[a] = n); } return r; } var R = (e) => S(e) && !Object.keys(e).length, Ce = (e) => e.type === "file", H = (e) => typeof e == "function", ge = (e) => { if (!Se) return !1; const s = e ? e.ownerDocument : 0; return e instanceof (s && s.defaultView ? s.defaultView.HTMLElement : HTMLElement); }, st = (e) => e.type === "select-multiple", Le = (e) => e.type === "radio", Et = (e) => Le(e) || de(e), De = (e) => ge(e) && e.isConnected; function St(e, s) { const r = s.slice(0, -1).length; let a = 0; for (; a < r; ) e = D(e) ? a++ : e[s[a++]]; return e; } function Ot(e) { for (const s in e) if (e.hasOwnProperty(s) && !D(e[s])) return !1; return !0; } function E(e, s) { const r = Array.isArray(s) ? s : be(s) ? [s] : Te(s), a = r.length === 1 ? e : St(e, r), u = r.length - 1, n = r[u]; return a && delete a[n], u !== 0 && (S(a) && R(a) || Array.isArray(a) && Ot(a)) && E(e, r.slice(0, -1)), e; } var Tt = (e) => { for (const s in e) if (H(e[s])) return !0; return !1; }; function it(e) { return Array.isArray(e) || S(e) && !Tt(e); } function Ee(e, s = {}) { for (const r in e) { const a = e[r]; it(a) ? (s[r] = Array.isArray(a) ? [] : {}, Ee(a, s[r])) : D(a) || (s[r] = !0); } return s; } function ae(e, s, r) { r || (r = Ee(s)); for (const a in e) { const u = e[a]; if (it(u)) D(s) || ke(r[a]) ? r[a] = Ee(u, Array.isArray(u) ? [] : {}) : ae(u, I(s) ? {} : s[a], r[a]); else { const n = s[a]; r[a] = !ee(u, n); } } return r; } const Ke = { value: !1, isValid: !1 }, ze = { value: !0, isValid: !0 }; var at = (e) => { if (Array.isArray(e)) { if (e.length > 1) { const s = e.filter((r) => r && r.checked && !r.disabled).map((r) => r.value); return { value: s, isValid: !!s.length }; } return e[0].checked && !e[0].disabled ? ( // @ts-expect-error expected to work in the browser e[0].attributes && !D(e[0].attributes.value) ? D(e[0].value) || e[0].value === "" ? ze : { value: e[0].value, isValid: !0 } : ze ) : Ke; } return Ke; }, lt = (e, { valueAsNumber: s, valueAsDate: r, setValueAs: a }) => D(e) ? e : s ? e === "" ? NaN : e && +e : r && N(e) ? new Date(e) : a ? a(e) : e; const Ye = { isValid: !1, value: null }; var nt = (e) => Array.isArray(e) ? e.reduce((s, r) => r && r.checked && !r.disabled ? { isValid: !0, value: r.value } : s, Ye) : Ye; function je(e) { const s = e.ref; return Ce(s) ? s.files : Le(s) ? nt(e.refs).value : st(s) ? [...s.selectedOptions].map(({ value: r }) => r) : de(s) ? at(e.refs).value : lt(D(s.value) ? e.ref.value : s.value, e); } var Ct = (e, s, r, a) => { const u = {}; for (const n of e) { const d = h(s, n); d && m(u, n, d._f); } return { criteriaMode: r, names: [...e], fields: u, shouldUseNativeValidation: a }; }, ve = (e) => e instanceof RegExp, ue = (e) => D(e) ? e : ve(e) ? e.source : S(e) ? ve(e.value) ? e.value.source : e.value : e, Je = (e) => ({ isOnSubmit: !e || e === W.onSubmit, isOnBlur: e === W.onBlur, isOnChange: e === W.onChange, isOnAll: e === W.all, isOnTouch: e === W.onTouched }); const Qe = "AsyncFunction"; var Lt = (e) => !!e && !!e.validate && !!(H(e.validate) && e.validate.constructor.name === Qe || S(e.validate) && Object.values(e.validate).find((s) => s.constructor.name === Qe)), Rt = (e) => e.mount && (e.required || e.min || e.max || e.maxLength || e.minLength || e.pattern || e.validate), Xe = (e, s, r) => !r && (s.watchAll || s.watch.has(e) || [...s.watch].some((a) => e.startsWith(a) && /^\.\w+/.test(e.slice(a.length)))); const fe = (e, s, r, a) => { for (const u of r || Object.keys(e)) { const n = h(e, u); if (n) { const { _f: d, ...y } = n; if (d) { if (d.refs && d.refs[0] && s(d.refs[0], u) && !a) return !0; if (d.ref && s(d.ref, d.name) && !a) return !0; if (fe(y, s)) break; } else if (S(y) && fe(y, s)) break; } } }; function Ze(e, s, r) { const a = h(e, r); if (a || be(r)) return { error: a, name: r }; const u = r.split("."); for (; u.length; ) { const n = u.join("."), d = h(s, n), y = h(e, n); if (d && !Array.isArray(d) && r !== n) return { name: r }; if (y && y.type) return { name: n, error: y }; if (y && y.root && y.root.type) return { name: `${n}.root`, error: y.root }; u.pop(); } return { name: r }; } var Ut = (e, s, r, a) => { r(e); const { name: u, ...n } = e; return R(n) || Object.keys(n).length >= Object.keys(s).length || Object.keys(n).find((d) => s[d] === (!a || W.all)); }, Mt = (e, s, r) => !e || !s || e === s || oe(e).some((a) => a && (r ? a === s : a.startsWith(s) || s.startsWith(a))), It = (e, s, r, a, u) => u.isOnAll ? !1 : !r && u.isOnTouch ? !(s || e) : (r ? a.isOnBlur : u.isOnBlur) ? !e : (r ? a.isOnChange : u.isOnChange) ? e : !0, Bt = (e, s) => !Oe(h(e, s)).length && E(e, s), Nt = (e, s, r) => { const a = oe(h(e, r)); return m(a, "root", s[r]), m(e, r, a), e; }; function Ge(e, s, r = "validate") { if (N(e) || Array.isArray(e) && e.every(N) || Y(e) && !e) return { type: r, message: N(e) ? e : "", ref: s }; } var ie = (e) => S(e) && !ve(e) ? e : { value: e, message: "" }, et = async (e, s, r, a, u, n) => { const { ref: d, refs: y, required: U, maxLength: p, minLength: x, min: k, max: v, pattern: le, validate: Q, name: O, valueAsNumber: X, mount: _e } = e._f, _ = h(r, O); if (!_e || s.has(O)) return {}; const j = y ? y[0] : d, $ = (b) => { u && j.reportValidity && (j.setCustomValidity(Y(b) ? "" : b || ""), j.reportValidity()); }, T = {}, ce = Le(d), Z = de(d), Ve = ce || Z, q = (X || Ce(d)) && D(d.value) && D(_) || ge(d) && d.value === "" || _ === "" || Array.isArray(_) && !_.length, te = kt.bind(null, O, a, T), K = (b, F, w, L = J.maxLength, M = J.minLength) => { const z = b ? F : w; T[O] = { type: b ? L : M, message: z, ref: d, ...te(b ? L : M, z) }; }; if (n ? !Array.isArray(_) || !_.length : U && (!Ve && (q || I(_)) || Y(_) && !_ || Z && !at(y).isValid || ce && !nt(y).isValid)) { const { value: b, message: F } = N(U) ? { value: !!U, message: U } : ie(U); if (b && (T[O] = { type: J.required, message: F, ref: j, ...te(J.required, F) }, !a)) return $(F), T; } if (!q && (!I(k) || !I(v))) { let b, F; const w = ie(v), L = ie(k); if (!I(_) && !isNaN(_)) { const M = d.valueAsNumber || _ && +_; I(w.value) || (b = M > w.value), I(L.value) || (F = M < L.value); } else { const M = d.valueAsDate || new Date(_), z = (ye) => /* @__PURE__ */ new Date((/* @__PURE__ */ new Date()).toDateString() + " " + ye), ne = d.type == "time", se = d.type == "week"; N(w.value) && _ && (b = ne ? z(_) > z(w.value) : se ? _ > w.value : M > new Date(w.value)), N(L.value) && _ && (F = ne ? z(_) < z(L.value) : se ? _ < L.value : M < new Date(L.value)); } if ((b || F) && (K(!!b, w.message, L.message, J.max, J.min), !a)) return $(T[O].message), T; } if ((p || x) && !q && (N(_) || n && Array.isArray(_))) { const b = ie(p), F = ie(x), w = !I(b.value) && _.length > +b.value, L = !I(F.value) && _.length < +F.value; if ((w || L) && (K(w, b.message, F.message), !a)) return $(T[O].message), T; } if (le && !q && N(_)) { const { value: b, message: F } = ie(le); if (ve(b) && !_.match(b) && (T[O] = { type: J.pattern, message: F, ref: d, ...te(J.pattern, F) }, !a)) return $(F), T; } if (Q) { if (H(Q)) { const b = await Q(_, r), F = Ge(b, j); if (F && (T[O] = { ...F, ...te(J.validate, F.message) }, !a)) return $(F.message), T; } else if (S(Q)) { let b = {}; for (const F in Q) { if (!R(b) && !a) break; const w = Ge(await Q[F](_, r), j, F); w && (b = { ...w, ...te(F, w.message) }, $(w.message), a && (T[O] = b)); } if (!R(b) && (T[O] = { ref: j, ...b }, !a)) return T; } } return $(!0), T; }; const Pt = { mode: W.onSubmit, reValidateMode: W.onChange, shouldFocusError: !0 }; function qt(e = {}) { let s = { ...Pt, ...e }, r = { submitCount: 0, isDirty: !1, isReady: !1, isLoading: H(s.defaultValues), isValidating: !1, isSubmitted: !1, isSubmitting: !1, isSubmitSuccessful: !1, isValid: !1, touchedFields: {}, dirtyFields: {}, validatingFields: {}, errors: s.errors || {}, disabled: s.disabled || !1 }, a = {}, u = S(s.defaultValues) || S(s.values) ? C(s.defaultValues || s.values) || {} : {}, n = s.shouldUnregister ? {} : C(u), d = { action: !1, mount: !1, watch: !1 }, y = { mount: /* @__PURE__ */ new Set(), disabled: /* @__PURE__ */ new Set(), unMount: /* @__PURE__ */ new Set(), array: /* @__PURE__ */ new Set(), watch: /* @__PURE__ */ new Set() }, U, p = 0; const x = { isDirty: !1, dirtyFields: !1, validatingFields: !1, touchedFields: !1, isValidating: !1, isValid: !1, errors: !1 }; let k = { ...x }; const v = { array: $e(), state: $e() }, le = s.criteriaMode === W.all, Q = (t) => (i) => { clearTimeout(p), p = setTimeout(t, i); }, O = async (t) => { if (!s.disabled && (x.isValid || k.isValid || t)) { const i = s.resolver ? R((await Z()).errors) : await q(a, !0); i !== r.isValid && v.state.next({ isValid: i }); } }, X = (t, i) => { !s.disabled && (x.isValidating || x.validatingFields || k.isValidating || k.validatingFields) && ((t || Array.from(y.mount)).forEach((l) => { l && (i ? m(r.validatingFields, l, i) : E(r.validatingFields, l)); }), v.state.next({ validatingFields: r.validatingFields, isValidating: !R(r.validatingFields) })); }, _e = (t, i = [], l, c, f = !0, o = !0) => { if (c && l && !s.disabled) { if (d.action = !0, o && Array.isArray(h(a, t))) { const g = l(h(a, t), c.argA, c.argB); f && m(a, t, g); } if (o && Array.isArray(h(r.errors, t))) { const g = l(h(r.errors, t), c.argA, c.argB); f && m(r.errors, t, g), Bt(r.errors, t); } if ((x.touchedFields || k.touchedFields) && o && Array.isArray(h(r.touchedFields, t))) { const g = l(h(r.touchedFields, t), c.argA, c.argB); f && m(r.touchedFields, t, g); } (x.dirtyFields || k.dirtyFields) && (r.dirtyFields = ae(u, n)), v.state.next({ name: t, isDirty: K(t, i), dirtyFields: r.dirtyFields, errors: r.errors, isValid: r.isValid }); } else m(n, t, i); }, _ = (t, i) => { m(r.errors, t, i), v.state.next({ errors: r.errors }); }, j = (t) => { r.errors = t, v.state.next({ errors: r.errors, isValid: !1 }); }, $ = (t, i, l, c) => { const f = h(a, t); if (f) { const o = h(n, t, D(l) ? h(u, t) : l); D(o) || c && c.defaultChecked || i ? m(n, t, i ? o : je(f._f)) : w(t, o), d.mount && !d.action && O(); } }, T = (t, i, l, c, f) => { let o = !1, g = !1; const V = { name: t }; if (!s.disabled) { if (!l || c) { (x.isDirty || k.isDirty) && (g = r.isDirty, r.isDirty = V.isDirty = K(), o = g !== V.isDirty); const A = ee(h(u, t), i); g = !!h(r.dirtyFields, t), A ? E(r.dirtyFields, t) : m(r.dirtyFields, t, !0), V.dirtyFields = r.dirtyFields, o = o || (x.dirtyFields || k.dirtyFields) && g !== !A; } if (l) { const A = h(r.touchedFields, t); A || (m(r.touchedFields, t, l), V.touchedFields = r.touchedFields, o = o || (x.touchedFields || k.touchedFields) && A !== l); } o && f && v.state.next(V); } return o ? V : {}; }, ce = (t, i, l, c) => { const f = h(r.errors, t), o = (x.isValid || k.isValid) && Y(i) && r.isValid !== i; if (s.delayError && l ? (U = Q(() => _(t, l)), U(s.delayError)) : (clearTimeout(p), U = null, l ? m(r.errors, t, l) : E(r.errors, t)), (l ? !ee(f, l) : f) || !R(c) || o) { const g = { ...c, ...o && Y(i) ? { isValid: i } : {}, errors: r.errors, name: t }; r = { ...r, ...g }, v.state.next(g); } }, Z = async (t) => { X(t, !0); const i = await s.resolver(n, s.context, Ct(t || y.mount, a, s.criteriaMode, s.shouldUseNativeValidation)); return X(t), i; }, Ve = async (t) => { const { errors: i } = await Z(t); if (t) for (const l of t) { const c = h(i, l); c ? m(r.errors, l, c) : E(r.errors, l); } else r.errors = i; return i; }, q = async (t, i, l = { valid: !0 }) => { for (const c in t) { const f = t[c]; if (f) { const { _f: o, ...g } = f; if (o) { const V = y.array.has(o.name), A = f._f && Lt(f._f); A && x.validatingFields && X([o.name], !0); const P = await et(f, y.disabled, n, le, s.shouldUseNativeValidation && !i, V); if (A && x.validatingFields && X([o.name]), P[o.name] && (l.valid = !1, i)) break; !i && (h(P, o.name) ? V ? Nt(r.errors, P, o.name) : m(r.errors, o.name, P[o.name]) : E(r.errors, o.name)); } !R(g) && await q(g, i, l); } } return l.valid; }, te = () => { for (const t of y.unMount) { const i = h(a, t); i && (i._f.refs ? i._f.refs.every((l) => !De(l)) : !De(i._f.ref)) && Fe(t); } y.unMount = /* @__PURE__ */ new Set(); }, K = (t, i) => !s.disabled && (t && i && m(n, t, i), !ee(ye(), u)), b = (t, i, l) => Dt(t, y, { ...d.mount ? n : D(i) ? u : N(t) ? { [t]: i } : i }, l, i), F = (t) => Oe(h(d.mount ? n : u, t, s.shouldUnregister ? h(u, t, []) : [])), w = (t, i, l = {}) => { const c = h(a, t); let f = i; if (c) { const o = c._f; o && (!o.disabled && m(n, t, lt(i, o)), f = ge(o.ref) && I(i) ? "" : i, st(o.ref) ? [...o.ref.options].forEach((g) => g.selected = f.includes(g.value)) : o.refs ? de(o.ref) ? o.refs.forEach((g) => { (!g.defaultChecked || !g.disabled) && (Array.isArray(f) ? g.checked = !!f.find((V) => V === g.value) : g.checked = f === g.value || !!f); }) : o.refs.forEach((g) => g.checked = g.value === f) : Ce(o.ref) ? o.ref.value = "" : (o.ref.value = f, o.ref.type || v.state.next({ name: t, values: C(n) }))); } (l.shouldDirty || l.shouldTouch) && T(t, f, l.shouldTouch, l.shouldDirty, !0), l.shouldValidate && se(t); }, L = (t, i, l) => { for (const c in i) { if (!i.hasOwnProperty(c)) return; const f = i[c], o = t + "." + c, g = h(a, o); (y.array.has(t) || S(f) || g && !g._f) && !re(f) ? L(o, f, l) : w(o, f, l); } }, M = (t, i, l = {}) => { const c = h(a, t), f = y.array.has(t), o = C(i); m(n, t, o), f ? (v.array.next({ name: t, values: C(n) }), (x.isDirty || x.dirtyFields || k.isDirty || k.dirtyFields) && l.shouldDirty && v.state.next({ name: t, dirtyFields: ae(u, n), isDirty: K(t, o) })) : c && !c._f && !I(o) ? L(t, o, l) : w(t, o, l), Xe(t, y) && v.state.next({ ...r, name: t }), v.state.next({ name: d.mount ? t : void 0, values: C(n) }); }, z = async (t) => { d.mount = !0; const i = t.target; let l = i.name, c = !0; const f = h(a, l), o = (A) => { c = Number.isNaN(A) || re(A) && isNaN(A.getTime()) || ee(A, h(n, l, A)); }, g = Je(s.mode), V = Je(s.reValidateMode); if (f) { let A, P; const he = i.type ? je(f._f) : _t(t), G = t.type === pe.BLUR || t.type === pe.FOCUS_OUT, gt = !Rt(f._f) && !s.resolver && !h(r.errors, l) && !f._f.deps || It(G, h(r.touchedFields, l), r.isSubmitted, V, g), me = Xe(l, y, G); m(n, l, he), G ? (!i || !i.readOnly) && (f._f.onBlur && f._f.onBlur(t), U && U(0)) : f._f.onChange && f._f.onChange(t); const we = T(l, he, G), vt = !R(we) || me; if (!G && v.state.next({ name: l, type: t.type, values: C(n) }), gt) return (x.isValid || k.isValid) && (s.mode === "onBlur" ? G && O() : G || O()), vt && v.state.next({ name: l, ...me ? {} : we }); if (!G && me && v.state.next({ ...r }), s.resolver) { const { errors: We } = await Z([l]); if (o(he), c) { const bt = Ze(r.errors, a, l), He = Ze(We, a, bt.name || l); A = He.error, l = He.name, P = R(We); } } else X([l], !0), A = (await et(f, y.disabled, n, le, s.shouldUseNativeValidation))[l], X([l]), o(he), c && (A ? P = !1 : (x.isValid || k.isValid) && (P = await q(a, !0))); c && (f._f.deps && (!Array.isArray(f._f.deps) || f._f.deps.length > 0) && se(f._f.deps), ce(l, P, A, we)); } }, ne = (t, i) => { if (h(r.errors, i) && t.focus) return t.focus(), 1; }, se = async (t, i = {}) => { let l, c; const f = oe(t); if (s.resolver) { const o = await Ve(D(t) ? t : f); l = R(o), c = t ? !f.some((g) => h(o, g)) : l; } else t ? (c = (await Promise.all(f.map(async (o) => { const g = h(a, o); return await q(g && g._f ? { [o]: g } : g); }))).every(Boolean), !(!c && !r.isValid) && O()) : c = l = await q(a); return v.state.next({ ...!N(t) || (x.isValid || k.isValid) && l !== r.isValid ? {} : { name: t }, ...s.resolver || !t ? { isValid: l } : {}, errors: r.errors }), i.shouldFocus && !c && fe(a, ne, t ? f : y.mount), c; }, ye = (t, i) => { let l = { ...d.mount ? n : u }; return i && (l = rt(i.dirtyFields ? r.dirtyFields : r.touchedFields, l)), D(t) ? l : N(t) ? h(l, t) : t.map((c) => h(l, c)); }, Re = (t, i) => ({ invalid: !!h((i || r).errors, t), isDirty: !!h((i || r).dirtyFields, t), error: h((i || r).errors, t), isValidating: !!h(r.validatingFields, t), isTouched: !!h((i || r).touchedFields, t) }), ut = (t) => { t && oe(t).forEach((i) => E(r.errors, i)), v.state.next({ errors: t ? r.errors : {} }); }, Ue = (t, i, l) => { const c = (h(a, t, { _f: {} })._f || {}).ref, f = h(r.errors, t) || {}, { ref: o, message: g, type: V, ...A } = f; m(r.errors, t, { ...A, ...i, ref: c }), v.state.next({ name: t, errors: r.errors, isValid: !1 }), l && l.shouldFocus && c && c.focus && c.focus(); }, ot = (t, i) => H(t) ? v.state.subscribe({ next: (l) => "values" in l && t(b(void 0, i), l) }) : b(t, i, !0), Me = (t) => v.state.subscribe({ next: (i) => { Mt(t.name, i.name, t.exact) && Ut(i, t.formState || x, ht, t.reRenderRoot) && t.callback({ values: { ...n }, ...r, ...i, defaultValues: u }); } }).unsubscribe, ft = (t) => (d.mount = !0, k = { ...k, ...t.formState }, Me({ ...t, formState: k })), Fe = (t, i = {}) => { for (const l of t ? oe(t) : y.mount) y.mount.delete(l), y.array.delete(l), i.keepValue || (E(a, l), E(n, l)), !i.keepError && E(r.errors, l), !i.keepDirty && E(r.dirtyFields, l), !i.keepTouched && E(r.touchedFields, l), !i.keepIsValidating && E(r.validatingFields, l), !s.shouldUnregister && !i.keepDefaultValue && E(u, l); v.state.next({ values: C(n) }), v.state.next({ ...r, ...i.keepDirty ? { isDirty: K() } : {} }), !i.keepIsValid && O(); }, Ie = ({ disabled: t, name: i }) => { (Y(t) && d.mount || t || y.disabled.has(i)) && (t ? y.disabled.add(i) : y.disabled.delete(i)); }, Ae = (t, i = {}) => { let l = h(a, t); const c = Y(i.disabled) || Y(s.disabled); return m(a, t, { ...l || {}, _f: { ...l && l._f ? l._f : { ref: { name: t } }, name: t, mount: !0, ...i } }), y.mount.add(t), l ? Ie({ disabled: Y(i.disabled) ? i.disabled : s.disabled, name: t }) : $(t, !0, i.value), { ...c ? { disabled: i.disabled || s.disabled } : {}, ...s.progressive ? { required: !!i.required, min: ue(i.min), max: ue(i.max), minLength: ue(i.minLength), maxLength: ue(i.maxLength), pattern: ue(i.pattern) } : {}, name: t, onChange: z, onBlur: z, ref: (f) => { if (f) { Ae(t, i), l = h(a, t); const o = D(f.value) && f.querySelectorAll && f.querySelectorAll("input,select,textarea")[0] || f, g = Et(o), V = l._f.refs || []; if (g ? V.find((A) => A === o) : o === l._f.ref) return; m(a, t, { _f: { ...l._f, ...g ? { refs: [ ...V.filter(De), o, ...Array.isArray(h(u, t)) ? [{}] : [] ], ref: { type: o.type, name: t } } : { ref: o } } }), $(t, !1, void 0, o); } else l = h(a, t, {}), l._f && (l._f.mount = !1), (s.shouldUnregister || i.shouldUnregister) && !(Ft(y.array, t) && d.action) && y.unMount.add(t); } }; }, xe = () => s.shouldFocusError && fe(a, ne, y.mount), dt = (t) => { Y(t) && (v.state.next({ disabled: t }), fe(a, (i, l) => { const c = h(a, l); c && (i.disabled = c._f.disabled || t, Array.isArray(c._f.refs) && c._f.refs.forEach((f) => { f.disabled = c._f.disabled || t; })); }, 0, !1)); }, Be = (t, i) => async (l) => { let c; l && (l.preventDefault && l.preventDefault(), l.persist && l.persist()); let f = C(n); if (v.state.next({ isSubmitting: !0 }), s.resolver) { const { errors: o, values: g } = await Z(); r.errors = o, f = C(g); } else await q(a); if (y.disabled.size) for (const o of y.disabled) E(f, o); if (E(r.errors, "root"), R(r.errors)) { v.state.next({ errors: {} }); try { await t(f, l); } catch (o) { c = o; } } else i && await i({ ...r.errors }, l), xe(), setTimeout(xe); if (v.state.next({ isSubmitted: !0, isSubmitting: !1, isSubmitSuccessful: R(r.errors) && !c, submitCount: r.submitCount + 1, errors: r.errors }), c) throw c; }, ct = (t, i = {}) => { h(a, t) && (D(i.defaultValue) ? M(t, C(h(u, t))) : (M(t, i.defaultValue), m(u, t, C(i.defaultValue))), i.keepTouched || E(r.touchedFields, t), i.keepDirty || (E(r.dirtyFields, t), r.isDirty = i.defaultValue ? K(t, C(h(u, t))) : K()), i.keepError || (E(r.errors, t), x.isValid && O()), v.state.next({ ...r })); }, Ne = (t, i = {}) => { const l = t ? C(t) : u, c = C(l), f = R(t), o = f ? u : c; if (i.keepDefaultValues || (u = l), !i.keepValues) { if (i.keepDirtyValues) { const g = /* @__PURE__ */ new Set([ ...y.mount, ...Object.keys(ae(u, n)) ]); for (const V of Array.from(g)) h(r.dirtyFields, V) ? m(o, V, h(n, V)) : M(V, h(o, V)); } else { if (Se && D(t)) for (const g of y.mount) { const V = h(a, g); if (V && V._f) { const A = Array.isArray(V._f.refs) ? V._f.refs[0] : V._f.ref; if (ge(A)) { const P = A.closest("form"); if (P) { P.reset(); break; } } } } if (i.keepFieldsRef) for (const g of y.mount) M(g, h(o, g)); else a = {}; } n = s.shouldUnregister ? i.keepDefaultValues ? C(u) : {} : C(o), v.array.next({ values: { ...o } }), v.state.next({ values: { ...o } }); } y = { mount: i.keepDirtyValues ? y.mount : /* @__PURE__ */ new Set(), unMount: /* @__PURE__ */ new Set(), array: /* @__PURE__ */ new Set(), disabled: /* @__PURE__ */ new Set(), watch: /* @__PURE__ */ new Set(), watchAll: !1, focus: "" }, d.mount = !x.isValid || !!i.keepIsValid || !!i.keepDirtyValues || !s.shouldUnregister && !R(o), d.watch = !!s.shouldUnregister, d.action = !1, i.keepErrors || (r.errors = {}), v.state.next({ submitCount: i.keepSubmitCount ? r.submitCount : 0, isDirty: f ? !1 : i.keepDirty ? r.isDirty : !!(i.keepDefaultValues && !ee(t, u)), isSubmitted: i.keepIsSubmitted ? r.isSubmitted : !1, dirtyFields: f ? {} : i.keepDirtyValues ? i.keepDefaultValues && n ? ae(u, n) : r.dirtyFields : i.keepDefaultValues && t ? ae(u, t) : i.keepDirty ? r.dirtyFields : {}, touchedFields: i.keepTouched ? r.touchedFields : {}, errors: i.keepErrors ? r.errors : {}, isSubmitSuccessful: i.keepIsSubmitSuccessful ? r.isSubmitSuccessful : !1, isSubmitting: !1, defaultValues: u }); }, Pe = (t, i) => Ne(H(t) ? t(n) : t, i), yt = (t, i = {}) => { const l = h(a, t), c = l && l._f; if (c) { const f = c.refs ? c.refs[0] : c.ref; f.focus && (f.focus(), i.shouldSelect && H(f.select) && f.select()); } }, ht = (t) => { r = { ...r, ...t }; }, qe = { control: { register: Ae, unregister: Fe, getFieldState: Re, handleSubmit: Be, setError: Ue, _subscribe: Me, _runSchema: Z, _focusError: xe, _getWatch: b, _getDirty: K, _setValid: O, _setFieldArray: _e, _setDisabledField: Ie, _setErrors: j, _getFieldArray: F, _reset: Ne, _resetDefaultValues: () => H(s.defaultValues) && s.defaultValues().then((t) => { Pe(t, s.resetOptions), v.state.next({ isLoading: !1 }); }), _removeUnmounted: te, _disableForm: dt, _subjects: v, _proxyFormState: x, get _fields() { return a; }, get _formValues() { return n; }, get _state() { return d; }, set _state(t) { d = t; }, get _defaultValues() { return u; }, get _names() { return y; }, set _names(t) { y = t; }, get _formState() { return r; }, get _options() { return s; }, set _options(t) { s = { ...s, ...t }; } }, subscribe: ft, trigger: se, register: Ae, handleSubmit: Be, watch: ot, setValue: M, getValues: ye, reset: Pe, resetField: ct, clearErrors: ut, unregister: Fe, setError: Ue, setFocus: yt, getFieldState: Re }; return { ...qe, formControl: qe }; } function pt(e = {}) { const s = B.useRef(void 0), r = B.useRef(void 0), [a, u] = B.useState({ isDirty: !1, isValidating: !1, isLoading: H(e.defaultValues), isSubmitted: !1, isSubmitting: !1, isSubmitSuccessful: !1, isValid: !1, submitCount: 0, dirtyFields: {}, touchedFields: {}, validatingFields: {}, errors: e.errors || {}, disabled: e.disabled || !1, isReady: !1, defaultValues: H(e.defaultValues) ? void 0 : e.defaultValues }); if (!s.current) if (e.formControl) s.current = { ...e.formControl, formState: a }, e.defaultValues && !H(e.defaultValues) && e.formControl.reset(e.defaultValues, e.resetOptions); else { const { formControl: d, ...y } = qt(e); s.current = { ...y, formState: a }; } const n = s.current.control; return n._options = e, wt(() => { const d = n._subscribe({ formState: n._proxyFormState, callback: () => u({ ...n._formState }), reRenderRoot: !0 }); return u((y) => ({ ...y, isReady: !0 })), n._formState.isReady = !0, d; }, [n]), B.useEffect(() => n._disableForm(e.disabled), [n, e.disabled]), B.useEffect(() => { e.mode && (n._options.mode = e.mode), e.reValidateMode && (n._options.reValidateMode = e.reValidateMode); }, [n, e.mode, e.reValidateMode]), B.useEffect(() => { e.errors && (n._setErrors(e.errors), n._focusError()); }, [n, e.errors]), B.useEffect(() => { e.shouldUnregister && n._subjects.state.next({ values: n._getWatch() }); }, [n, e.shouldUnregister]), B.useEffect(() => { if (n._proxyFormState.isDirty) { const d = n._getDirty(); d !== a.isDirty && n._subjects.state.next({ isDirty: d }); } }, [n, a.isDirty]), B.useEffect(() => { var d; e.values && !ee(e.values, r.current) ? (n._reset(e.values, { keepFieldsRef: !0, ...n._options.resetOptions }), !((d = n._options.resetOptions) === null || d === void 0) && d.keepIsValid || n._setValid(), r.current = e.values, u((y) => ({ ...y }))) : n._resetDefaultValues(); }, [n, e.values]), B.useEffect(() => { n._state.mount || (n._setValid(), n._state.mount = !0), n._state.watch && (n._state.watch = !1, n._subjects.state.next({ ...n._formState })), n._removeUnmounted(); }), s.current.formState = mt(a, n), s.current; } export { kt as a, h as g, m as s, pt as u };