UNPKG

@stanfordspezi/spezi-web-design-system

Version:

Stanford Biodesign Digital Health Spezi Web Design System

1,281 lines (1,280 loc) 41.3 kB
import * as ze from "react"; import S, { useCallback as Tt } from "react"; import { p as Mt } from "./query-B8v8XXJk.mjs"; import { jsx as ie, jsxs as Ce } from "react/jsx-runtime"; import { E as Ut } from "./Error-QDZNK6HE.mjs"; import { a as pt, L as Nt } from "./Label-ChMjb3jl.mjs"; import { I as Pt } from "./InfoButton-Dmi254lq.mjs"; import { d as Bt } from "./Tooltip-GHHPt6Q7.mjs"; import { e as It, i as $t, b as jt } from "./misc-Ci6Z8ZmP.mjs"; import { E as qt } from "./ErrorState-D5VyTRAB.mjs"; import { S as Wt } from "./StateContainer-D7lYvK4y.mjs"; import { c as Ht } from "./index-2NvaPZWc.mjs"; var ye = (e) => e.type === "checkbox", ae = (e) => e instanceof Date, B = (e) => e == null; const ut = (e) => typeof e == "object"; var R = (e) => !B(e) && !Array.isArray(e) && ut(e) && !ae(e), ct = (e) => R(e) && e.target ? ye(e.target) ? e.target.checked : e.target.value : e, Kt = (e) => e.substring(0, e.search(/\.\d+(\.|$)/)) || e, dt = (e, s) => e.has(Kt(s)), Yt = (e) => { const s = e.constructor && e.constructor.prototype; return R(s) && s.hasOwnProperty("isPrototypeOf"); }, Re = typeof window < "u" && typeof window.HTMLElement < "u" && typeof document < "u"; function p(e) { let s; const t = Array.isArray(e), i = typeof FileList < "u" ? e instanceof FileList : !1; if (e instanceof Date) s = new Date(e); else if (e instanceof Set) s = new Set(e); else if (!(Re && (e instanceof Blob || i)) && (t || R(e))) if (s = t ? [] : {}, !t && !Yt(e)) s = e; else for (const n in e) e.hasOwnProperty(n) && (s[n] = p(e[n])); else return e; return s; } var Ae = (e) => Array.isArray(e) ? e.filter(Boolean) : [], O = (e) => e === void 0, y = (e, s, t) => { if (!s || !R(e)) return t; const i = Ae(s.split(/[,[\].]+?/)).reduce((n, o) => B(n) ? n : n[o], e); return O(i) || i === e ? O(e[s]) ? t : e[s] : i; }, q = (e) => typeof e == "boolean", Le = (e) => /^\w*$/.test(e), ft = (e) => Ae(e.replace(/["|']|\]/g, "").split(/\.|\[/)), x = (e, s, t) => { let i = -1; const n = Le(s) ? [s] : ft(s), o = n.length, u = o - 1; for (; ++i < o; ) { const d = n[i]; let v = t; if (i !== u) { const A = e[d]; v = R(A) || Array.isArray(A) ? A : isNaN(+n[i + 1]) ? {} : []; } if (d === "__proto__" || d === "constructor" || d === "prototype") return; e[d] = v, e = e[d]; } }; const be = { BLUR: "blur", FOCUS_OUT: "focusout", CHANGE: "change" }, Y = { onBlur: "onBlur", onChange: "onChange", onSubmit: "onSubmit", onTouched: "onTouched", all: "all" }, Z = { max: "max", min: "min", maxLength: "maxLength", minLength: "minLength", pattern: "pattern", required: "required", validate: "validate" }, zt = S.createContext(null), Te = () => S.useContext(zt); var yt = (e, s, t, i = !0) => { const n = { defaultValues: s._defaultValues }; for (const o in e) Object.defineProperty(n, o, { get: () => { const u = o; return s._proxyFormState[u] !== Y.all && (s._proxyFormState[u] = !i || Y.all), t && (t[u] = !0), e[u]; } }); return n; }; const Me = typeof window < "u" ? ze.useLayoutEffect : ze.useEffect; function Gt(e) { const s = Te(), { control: t = s.control, disabled: i, name: n, exact: o } = e || {}, [u, d] = S.useState(t._formState), v = S.useRef({ isDirty: !1, isLoading: !1, dirtyFields: !1, touchedFields: !1, validatingFields: !1, isValidating: !1, isValid: !1, errors: !1 }); return Me(() => t._subscribe({ name: n, formState: v.current, exact: o, callback: (A) => { !i && d({ ...t._formState, ...A }); } }), [n, i, o]), S.useEffect(() => { v.current.isValid && t._setValid(!0); }, [t]), S.useMemo(() => yt(u, t, v.current, !1), [u, t]); } var Q = (e) => typeof e == "string", ht = (e, s, t, i, n) => Q(e) ? (i && s.watch.add(e), y(t, e, n)) : Array.isArray(e) ? e.map((o) => (i && s.watch.add(o), y(t, o))) : (i && (s.watchAll = !0), t); function Jt(e) { const s = Te(), { control: t = s.control, name: i, defaultValue: n, disabled: o, exact: u } = e || {}, d = S.useRef(n), [v, A] = S.useState(t._getWatch(i, d.current)); return Me(() => t._subscribe({ name: i, formState: { values: !0 }, exact: u, callback: (_) => !o && A(ht(i, t._names, _.values || t._formValues, !1, d.current)) }), [i, t, o, u]), S.useEffect(() => t._removeUnmounted()), v; } function Qt(e) { const s = Te(), { name: t, disabled: i, control: n = s.control, shouldUnregister: o } = e, u = dt(n._names.array, t), d = Jt({ control: n, name: t, defaultValue: y(n._formValues, t, y(n._defaultValues, t, e.defaultValue)), exact: !0 }), v = Gt({ control: n, name: t, exact: !0 }), A = S.useRef(e), _ = S.useRef(n.register(t, { ...e.rules, value: d, ...q(e.disabled) ? { disabled: e.disabled } : {} })), V = S.useMemo(() => Object.defineProperties({}, { invalid: { enumerable: !0, get: () => !!y(v.errors, t) }, isDirty: { enumerable: !0, get: () => !!y(v.dirtyFields, t) }, isTouched: { enumerable: !0, get: () => !!y(v.touchedFields, t) }, isValidating: { enumerable: !0, get: () => !!y(v.validatingFields, t) }, error: { enumerable: !0, get: () => y(v.errors, t) } }), [v, t]), m = S.useCallback((L) => _.current.onChange({ target: { value: ct(L), name: t }, type: be.CHANGE }), [t]), I = S.useCallback(() => _.current.onBlur({ target: { value: y(n._formValues, t), name: t }, type: be.BLUR }), [t, n._formValues]), T = S.useCallback((L) => { const X = y(n._fields, t); X && L && (X._f.ref = { focus: () => L.focus(), select: () => L.select(), setCustomValidity: (b) => L.setCustomValidity(b), reportValidity: () => L.reportValidity() }); }, [n._fields, t]), D = S.useMemo(() => ({ name: t, value: d, ...q(i) || v.disabled ? { disabled: v.disabled || i } : {}, onChange: m, onBlur: I, ref: T }), [t, i, v.disabled, m, I, T, d]); return S.useEffect(() => { const L = n._options.shouldUnregister || o; n.register(t, { ...A.current.rules, ...q(A.current.disabled) ? { disabled: A.current.disabled } : {} }); const X = (b, H) => { const $ = y(n._fields, b); $ && $._f && ($._f.mount = H); }; if (X(t, !0), L) { const b = p(y(n._options.defaultValues, t)); x(n._defaultValues, t, b), O(y(n._formValues, t)) && x(n._formValues, t, b); } return !u && n.register(t), () => { (u ? L && !n._state.action : L) ? n.unregister(t) : X(t, !1); }; }, [t, n, u, o]), S.useEffect(() => { n._setDisabledField({ disabled: i, name: t }); }, [i, t, n]), S.useMemo(() => ({ field: D, formState: v, fieldState: V }), [D, v, V]); } const Xt = (e) => e.render(Qt(e)); var gt = (e, s, t, i, n) => s ? { ...t[e], types: { ...t[e] && t[e].types ? t[e].types : {}, [i]: n || !0 } } : {}, de = (e) => Array.isArray(e) ? e : [e], Ge = () => { let e = []; return { get observers() { return e; }, next: (n) => { for (const o of e) o.next && o.next(n); }, subscribe: (n) => (e.push(n), { unsubscribe: () => { e = e.filter((o) => o !== n); } }), unsubscribe: () => { e = []; } }; }, Oe = (e) => B(e) || !ut(e); function re(e, s) { if (Oe(e) || Oe(s)) return e === s; if (ae(e) && ae(s)) return e.getTime() === s.getTime(); const t = Object.keys(e), i = Object.keys(s); if (t.length !== i.length) return !1; for (const n of t) { const o = e[n]; if (!i.includes(n)) return !1; if (n !== "ref") { const u = s[n]; if (ae(o) && ae(u) || R(o) && R(u) || Array.isArray(o) && Array.isArray(u) ? !re(o, u) : o !== u) return !1; } } return !0; } var P = (e) => R(e) && !Object.keys(e).length, Ue = (e) => e.type === "file", z = (e) => typeof e == "function", _e = (e) => { if (!Re) return !1; const s = e ? e.ownerDocument : 0; return e instanceof (s && s.defaultView ? s.defaultView.HTMLElement : HTMLElement); }, vt = (e) => e.type === "select-multiple", pe = (e) => e.type === "radio", Zt = (e) => pe(e) || ye(e), ke = (e) => _e(e) && e.isConnected; function er(e, s) { const t = s.slice(0, -1).length; let i = 0; for (; i < t; ) e = O(e) ? i++ : e[s[i++]]; return e; } function tr(e) { for (const s in e) if (e.hasOwnProperty(s) && !O(e[s])) return !1; return !0; } function U(e, s) { const t = Array.isArray(s) ? s : Le(s) ? [s] : ft(s), i = t.length === 1 ? e : er(e, t), n = t.length - 1, o = t[n]; return i && delete i[o], n !== 0 && (R(i) && P(i) || Array.isArray(i) && tr(i)) && U(e, t.slice(0, -1)), e; } var mt = (e) => { for (const s in e) if (z(e[s])) return !0; return !1; }; function Ve(e, s = {}) { const t = Array.isArray(e); if (R(e) || t) for (const i in e) Array.isArray(e[i]) || R(e[i]) && !mt(e[i]) ? (s[i] = Array.isArray(e[i]) ? [] : {}, Ve(e[i], s[i])) : B(e[i]) || (s[i] = !0); return s; } function bt(e, s, t) { const i = Array.isArray(e); if (R(e) || i) for (const n in e) Array.isArray(e[n]) || R(e[n]) && !mt(e[n]) ? O(s) || Oe(t[n]) ? t[n] = Array.isArray(e[n]) ? Ve(e[n], []) : { ...Ve(e[n]) } : bt(e[n], B(s) ? {} : s[n], t[n]) : t[n] = !re(e[n], s[n]); return t; } var ue = (e, s) => bt(e, s, Ve(s)); const Je = { value: !1, isValid: !1 }, Qe = { value: !0, isValid: !0 }; var _t = (e) => { if (Array.isArray(e)) { if (e.length > 1) { const s = e.filter((t) => t && t.checked && !t.disabled).map((t) => t.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 && !O(e[0].attributes.value) ? O(e[0].value) || e[0].value === "" ? Qe : { value: e[0].value, isValid: !0 } : Qe ) : Je; } return Je; }, Vt = (e, { valueAsNumber: s, valueAsDate: t, setValueAs: i }) => O(e) ? e : s ? e === "" ? NaN : e && +e : t && Q(e) ? new Date(e) : i ? i(e) : e; const Xe = { isValid: !1, value: null }; var Ft = (e) => Array.isArray(e) ? e.reduce((s, t) => t && t.checked && !t.disabled ? { isValid: !0, value: t.value } : s, Xe) : Xe; function Ze(e) { const s = e.ref; return Ue(s) ? s.files : pe(s) ? Ft(e.refs).value : vt(s) ? [...s.selectedOptions].map(({ value: t }) => t) : ye(s) ? _t(e.refs).value : Vt(O(s.value) ? e.ref.value : s.value, e); } var rr = (e, s, t, i) => { const n = {}; for (const o of e) { const u = y(s, o); u && x(n, o, u._f); } return { criteriaMode: t, names: [...e], fields: n, shouldUseNativeValidation: i }; }, Fe = (e) => e instanceof RegExp, ce = (e) => O(e) ? e : Fe(e) ? e.source : R(e) ? Fe(e.value) ? e.value.source : e.value : e, et = (e) => ({ isOnSubmit: !e || e === Y.onSubmit, isOnBlur: e === Y.onBlur, isOnChange: e === Y.onChange, isOnAll: e === Y.all, isOnTouch: e === Y.onTouched }); const tt = "AsyncFunction"; var sr = (e) => !!e && !!e.validate && !!(z(e.validate) && e.validate.constructor.name === tt || R(e.validate) && Object.values(e.validate).find((s) => s.constructor.name === tt)), ir = (e) => e.mount && (e.required || e.min || e.max || e.maxLength || e.minLength || e.pattern || e.validate), rt = (e, s, t) => !t && (s.watchAll || s.watch.has(e) || [...s.watch].some((i) => e.startsWith(i) && /^\.\w+/.test(e.slice(i.length)))); const fe = (e, s, t, i) => { for (const n of t || Object.keys(e)) { const o = y(e, n); if (o) { const { _f: u, ...d } = o; if (u) { if (u.refs && u.refs[0] && s(u.refs[0], n) && !i) return !0; if (u.ref && s(u.ref, u.name) && !i) return !0; if (fe(d, s)) break; } else if (R(d) && fe(d, s)) break; } } }; function st(e, s, t) { const i = y(e, t); if (i || Le(t)) return { error: i, name: t }; const n = t.split("."); for (; n.length; ) { const o = n.join("."), u = y(s, o), d = y(e, o); if (u && !Array.isArray(u) && t !== o) return { name: t }; if (d && d.type) return { name: o, error: d }; n.pop(); } return { name: t }; } var ar = (e, s, t, i) => { t(e); const { name: n, ...o } = e; return P(o) || Object.keys(o).length >= Object.keys(s).length || Object.keys(o).find((u) => s[u] === (!i || Y.all)); }, nr = (e, s, t) => !e || !s || e === s || de(e).some((i) => i && (t ? i === s : i.startsWith(s) || s.startsWith(i))), or = (e, s, t, i, n) => n.isOnAll ? !1 : !t && n.isOnTouch ? !(s || e) : (t ? i.isOnBlur : n.isOnBlur) ? !e : (t ? i.isOnChange : n.isOnChange) ? e : !0, lr = (e, s) => !Ae(y(e, s)).length && U(e, s), ur = (e, s, t) => { const i = de(y(e, t)); return x(i, "root", s[t]), x(e, t, i), e; }, me = (e) => Q(e); function it(e, s, t = "validate") { if (me(e) || Array.isArray(e) && e.every(me) || q(e) && !e) return { type: t, message: me(e) ? e : "", ref: s }; } var oe = (e) => R(e) && !Fe(e) ? e : { value: e, message: "" }, at = async (e, s, t, i, n, o) => { const { ref: u, refs: d, required: v, maxLength: A, minLength: _, min: V, max: m, pattern: I, validate: T, name: D, valueAsNumber: L, mount: X } = e._f, b = y(t, D); if (!X || s.has(D)) return {}; const H = d ? d[0] : u, $ = (F) => { n && H.reportValidity && (H.setCustomValidity(q(F) ? "" : F || ""), H.reportValidity()); }, M = {}, he = pe(u), ee = ye(u), xe = he || ee, K = (L || Ue(u)) && O(u.value) && O(b) || _e(u) && u.value === "" || b === "" || Array.isArray(b) && !b.length, se = gt.bind(null, D, i, M), G = (F, E, C, N = Z.maxLength, j = Z.minLength) => { const J = F ? E : C; M[D] = { type: F ? N : j, message: J, ref: u, ...se(F ? N : j, J) }; }; if (o ? !Array.isArray(b) || !b.length : v && (!xe && (K || B(b)) || q(b) && !b || ee && !_t(d).isValid || he && !Ft(d).isValid)) { const { value: F, message: E } = me(v) ? { value: !!v, message: v } : oe(v); if (F && (M[D] = { type: Z.required, message: E, ref: H, ...se(Z.required, E) }, !i)) return $(E), M; } if (!K && (!B(V) || !B(m))) { let F, E; const C = oe(m), N = oe(V); if (!B(b) && !isNaN(b)) { const j = u.valueAsNumber || b && +b; B(C.value) || (F = j > C.value), B(N.value) || (E = j < N.value); } else { const j = u.valueAsDate || new Date(b), J = (ge) => /* @__PURE__ */ new Date((/* @__PURE__ */ new Date()).toDateString() + " " + ge), le = u.type == "time", ne = u.type == "week"; Q(C.value) && b && (F = le ? J(b) > J(C.value) : ne ? b > C.value : j > new Date(C.value)), Q(N.value) && b && (E = le ? J(b) < J(N.value) : ne ? b < N.value : j < new Date(N.value)); } if ((F || E) && (G(!!F, C.message, N.message, Z.max, Z.min), !i)) return $(M[D].message), M; } if ((A || _) && !K && (Q(b) || o && Array.isArray(b))) { const F = oe(A), E = oe(_), C = !B(F.value) && b.length > +F.value, N = !B(E.value) && b.length < +E.value; if ((C || N) && (G(C, F.message, E.message), !i)) return $(M[D].message), M; } if (I && !K && Q(b)) { const { value: F, message: E } = oe(I); if (Fe(F) && !b.match(F) && (M[D] = { type: Z.pattern, message: E, ref: u, ...se(Z.pattern, E) }, !i)) return $(E), M; } if (T) { if (z(T)) { const F = await T(b, t), E = it(F, H); if (E && (M[D] = { ...E, ...se(Z.validate, E.message) }, !i)) return $(E.message), M; } else if (R(T)) { let F = {}; for (const E in T) { if (!P(F) && !i) break; const C = it(await T[E](b, t), H, E); C && (F = { ...C, ...se(E, C.message) }, $(C.message), i && (M[D] = F)); } if (!P(F) && (M[D] = { ref: H, ...F }, !i)) return M; } } return $(!0), M; }; const cr = { mode: Y.onSubmit, reValidateMode: Y.onChange, shouldFocusError: !0 }; function dr(e = {}) { let s = { ...cr, ...e }, t = { submitCount: 0, isDirty: !1, isReady: !1, isLoading: z(s.defaultValues), isValidating: !1, isSubmitted: !1, isSubmitting: !1, isSubmitSuccessful: !1, isValid: !1, touchedFields: {}, dirtyFields: {}, validatingFields: {}, errors: s.errors || {}, disabled: s.disabled || !1 }; const i = {}; let n = R(s.defaultValues) || R(s.values) ? p(s.defaultValues || s.values) || {} : {}, o = s.shouldUnregister ? {} : p(n), u = { action: !1, mount: !1, watch: !1 }, d = { mount: /* @__PURE__ */ new Set(), disabled: /* @__PURE__ */ new Set(), unMount: /* @__PURE__ */ new Set(), array: /* @__PURE__ */ new Set(), watch: /* @__PURE__ */ new Set() }, v, A = 0; const _ = { isDirty: !1, dirtyFields: !1, validatingFields: !1, touchedFields: !1, isValidating: !1, isValid: !1, errors: !1 }; let V = { ..._ }; const m = { array: Ge(), state: Ge() }, I = s.criteriaMode === Y.all, T = (r) => (a) => { clearTimeout(A), A = setTimeout(r, a); }, D = async (r) => { if (!s.disabled && (_.isValid || V.isValid || r)) { const a = s.resolver ? P((await ee()).errors) : await K(i, !0); a !== t.isValid && m.state.next({ isValid: a }); } }, L = (r, a) => { !s.disabled && (_.isValidating || _.validatingFields || V.isValidating || V.validatingFields) && ((r || Array.from(d.mount)).forEach((l) => { l && (a ? x(t.validatingFields, l, a) : U(t.validatingFields, l)); }), m.state.next({ validatingFields: t.validatingFields, isValidating: !P(t.validatingFields) })); }, X = (r, a = [], l, h, f = !0, c = !0) => { if (h && l && !s.disabled) { if (u.action = !0, c && Array.isArray(y(i, r))) { const g = l(y(i, r), h.argA, h.argB); f && x(i, r, g); } if (c && Array.isArray(y(t.errors, r))) { const g = l(y(t.errors, r), h.argA, h.argB); f && x(t.errors, r, g), lr(t.errors, r); } if ((_.touchedFields || V.touchedFields) && c && Array.isArray(y(t.touchedFields, r))) { const g = l(y(t.touchedFields, r), h.argA, h.argB); f && x(t.touchedFields, r, g); } (_.dirtyFields || V.dirtyFields) && (t.dirtyFields = ue(n, o)), m.state.next({ name: r, isDirty: G(r, a), dirtyFields: t.dirtyFields, errors: t.errors, isValid: t.isValid }); } else x(o, r, a); }, b = (r, a) => { x(t.errors, r, a), m.state.next({ errors: t.errors }); }, H = (r) => { t.errors = r, m.state.next({ errors: t.errors, isValid: !1 }); }, $ = (r, a, l, h) => { const f = y(i, r); if (f) { const c = y(o, r, O(l) ? y(n, r) : l); O(c) || h && h.defaultChecked || a ? x(o, r, a ? c : Ze(f._f)) : C(r, c), u.mount && D(); } }, M = (r, a, l, h, f) => { let c = !1, g = !1; const w = { name: r }; if (!s.disabled) { if (!l || h) { (_.isDirty || V.isDirty) && (g = t.isDirty, t.isDirty = w.isDirty = G(), c = g !== w.isDirty); const k = re(y(n, r), a); g = !!y(t.dirtyFields, r), k ? U(t.dirtyFields, r) : x(t.dirtyFields, r, !0), w.dirtyFields = t.dirtyFields, c = c || (_.dirtyFields || V.dirtyFields) && g !== !k; } if (l) { const k = y(t.touchedFields, r); k || (x(t.touchedFields, r, l), w.touchedFields = t.touchedFields, c = c || (_.touchedFields || V.touchedFields) && k !== l); } c && f && m.state.next(w); } return c ? w : {}; }, he = (r, a, l, h) => { const f = y(t.errors, r), c = (_.isValid || V.isValid) && q(a) && t.isValid !== a; if (s.delayError && l ? (v = T(() => b(r, l)), v(s.delayError)) : (clearTimeout(A), v = null, l ? x(t.errors, r, l) : U(t.errors, r)), (l ? !re(f, l) : f) || !P(h) || c) { const g = { ...h, ...c && q(a) ? { isValid: a } : {}, errors: t.errors, name: r }; t = { ...t, ...g }, m.state.next(g); } }, ee = async (r) => { L(r, !0); const a = await s.resolver(o, s.context, rr(r || d.mount, i, s.criteriaMode, s.shouldUseNativeValidation)); return L(r), a; }, xe = async (r) => { const { errors: a } = await ee(r); if (r) for (const l of r) { const h = y(a, l); h ? x(t.errors, l, h) : U(t.errors, l); } else t.errors = a; return a; }, K = async (r, a, l = { valid: !0 }) => { for (const h in r) { const f = r[h]; if (f) { const { _f: c, ...g } = f; if (c) { const w = d.array.has(c.name), k = f._f && sr(f._f); k && _.validatingFields && L([h], !0); const W = await at(f, d.disabled, o, I, s.shouldUseNativeValidation && !a, w); if (k && _.validatingFields && L([h]), W[c.name] && (l.valid = !1, a)) break; !a && (y(W, c.name) ? w ? ur(t.errors, W, c.name) : x(t.errors, c.name, W[c.name]) : U(t.errors, c.name)); } !P(g) && await K(g, a, l); } } return l.valid; }, se = () => { for (const r of d.unMount) { const a = y(i, r); a && (a._f.refs ? a._f.refs.every((l) => !ke(l)) : !ke(a._f.ref)) && we(r); } d.unMount = /* @__PURE__ */ new Set(); }, G = (r, a) => !s.disabled && (r && a && x(o, r, a), !re(ge(), n)), F = (r, a, l) => ht(r, d, { ...u.mount ? o : O(a) ? n : Q(r) ? { [r]: a } : a }, l, a), E = (r) => Ae(y(u.mount ? o : n, r, s.shouldUnregister ? y(n, r, []) : [])), C = (r, a, l = {}) => { const h = y(i, r); let f = a; if (h) { const c = h._f; c && (!c.disabled && x(o, r, Vt(a, c)), f = _e(c.ref) && B(a) ? "" : a, vt(c.ref) ? [...c.ref.options].forEach((g) => g.selected = f.includes(g.value)) : c.refs ? ye(c.ref) ? c.refs.forEach((g) => { (!g.defaultChecked || !g.disabled) && (Array.isArray(f) ? g.checked = !!f.find((w) => w === g.value) : g.checked = f === g.value || !!f); }) : c.refs.forEach((g) => g.checked = g.value === f) : Ue(c.ref) ? c.ref.value = "" : (c.ref.value = f, c.ref.type || m.state.next({ name: r, values: p(o) }))); } (l.shouldDirty || l.shouldTouch) && M(r, f, l.shouldTouch, l.shouldDirty, !0), l.shouldValidate && ne(r); }, N = (r, a, l) => { for (const h in a) { if (!a.hasOwnProperty(h)) return; const f = a[h], c = `${r}.${h}`, g = y(i, c); (d.array.has(r) || R(f) || g && !g._f) && !ae(f) ? N(c, f, l) : C(c, f, l); } }, j = (r, a, l = {}) => { const h = y(i, r), f = d.array.has(r), c = p(a); x(o, r, c), f ? (m.array.next({ name: r, values: p(o) }), (_.isDirty || _.dirtyFields || V.isDirty || V.dirtyFields) && l.shouldDirty && m.state.next({ name: r, dirtyFields: ue(n, o), isDirty: G(r, c) })) : h && !h._f && !B(c) ? N(r, c, l) : C(r, c, l), rt(r, d) && m.state.next({ ...t }), m.state.next({ name: u.mount ? r : void 0, values: p(o) }); }, J = async (r) => { u.mount = !0; const a = r.target; let l = a.name, h = !0; const f = y(i, l), c = (k) => { h = Number.isNaN(k) || ae(k) && isNaN(k.getTime()) || re(k, y(o, l, k)); }, g = et(s.mode), w = et(s.reValidateMode); if (f) { let k, W; const ve = a.type ? Ze(f._f) : ct(r), te = r.type === be.BLUR || r.type === be.FOCUS_OUT, Ot = !ir(f._f) && !s.resolver && !y(t.errors, l) && !f._f.deps || or(te, y(t.touchedFields, l), t.isSubmitted, w, g), Se = rt(l, d, te); x(o, l, ve), te ? (f._f.onBlur && f._f.onBlur(r), v && v(0)) : f._f.onChange && f._f.onChange(r); const De = M(l, ve, te), Rt = !P(De) || Se; if (!te && m.state.next({ name: l, type: r.type, values: p(o) }), Ot) return (_.isValid || V.isValid) && (s.mode === "onBlur" ? te && D() : te || D()), Rt && m.state.next({ name: l, ...Se ? {} : De }); if (!te && Se && m.state.next({ ...t }), s.resolver) { const { errors: Ke } = await ee([l]); if (c(ve), h) { const Lt = st(t.errors, i, l), Ye = st(Ke, i, Lt.name || l); k = Ye.error, l = Ye.name, W = P(Ke); } } else L([l], !0), k = (await at(f, d.disabled, o, I, s.shouldUseNativeValidation))[l], L([l]), c(ve), h && (k ? W = !1 : (_.isValid || V.isValid) && (W = await K(i, !0))); h && (f._f.deps && ne(f._f.deps), he(l, W, k, De)); } }, le = (r, a) => { if (y(t.errors, a) && r.focus) return r.focus(), 1; }, ne = async (r, a = {}) => { let l, h; const f = de(r); if (s.resolver) { const c = await xe(O(r) ? r : f); l = P(c), h = r ? !f.some((g) => y(c, g)) : l; } else r ? (h = (await Promise.all(f.map(async (c) => { const g = y(i, c); return await K(g && g._f ? { [c]: g } : g); }))).every(Boolean), !(!h && !t.isValid) && D()) : h = l = await K(i); return m.state.next({ ...!Q(r) || (_.isValid || V.isValid) && l !== t.isValid ? {} : { name: r }, ...s.resolver || !r ? { isValid: l } : {}, errors: t.errors }), a.shouldFocus && !h && fe(i, le, r ? f : d.mount), h; }, ge = (r) => { const a = { ...u.mount ? o : n }; return O(r) ? a : Q(r) ? y(a, r) : r.map((l) => y(a, l)); }, Ne = (r, a) => ({ invalid: !!y((a || t).errors, r), isDirty: !!y((a || t).dirtyFields, r), error: y((a || t).errors, r), isValidating: !!y(t.validatingFields, r), isTouched: !!y((a || t).touchedFields, r) }), xt = (r) => { r && de(r).forEach((a) => U(t.errors, a)), m.state.next({ errors: r ? t.errors : {} }); }, Pe = (r, a, l) => { const h = (y(i, r, { _f: {} })._f || {}).ref, f = y(t.errors, r) || {}, { ref: c, message: g, type: w, ...k } = f; x(t.errors, r, { ...k, ...a, ref: h }), m.state.next({ name: r, errors: t.errors, isValid: !1 }), l && l.shouldFocus && h && h.focus && h.focus(); }, wt = (r, a) => z(r) ? m.state.subscribe({ next: (l) => r(F(void 0, a), l) }) : F(r, a, !0), Be = (r) => m.state.subscribe({ next: (a) => { nr(r.name, a.name, r.exact) && ar(a, r.formState || _, Ct, r.reRenderRoot) && r.callback({ values: { ...o }, ...t, ...a }); } }).unsubscribe, Et = (r) => (u.mount = !0, V = { ...V, ...r.formState }, Be({ ...r, formState: V })), we = (r, a = {}) => { for (const l of r ? de(r) : d.mount) d.mount.delete(l), d.array.delete(l), a.keepValue || (U(i, l), U(o, l)), !a.keepError && U(t.errors, l), !a.keepDirty && U(t.dirtyFields, l), !a.keepTouched && U(t.touchedFields, l), !a.keepIsValidating && U(t.validatingFields, l), !s.shouldUnregister && !a.keepDefaultValue && U(n, l); m.state.next({ values: p(o) }), m.state.next({ ...t, ...a.keepDirty ? { isDirty: G() } : {} }), !a.keepIsValid && D(); }, Ie = ({ disabled: r, name: a }) => { (q(r) && u.mount || r || d.disabled.has(a)) && (r ? d.disabled.add(a) : d.disabled.delete(a)); }, Ee = (r, a = {}) => { let l = y(i, r); const h = q(a.disabled) || q(s.disabled); return x(i, r, { ...l || {}, _f: { ...l && l._f ? l._f : { ref: { name: r } }, name: r, mount: !0, ...a } }), d.mount.add(r), l ? Ie({ disabled: q(a.disabled) ? a.disabled : s.disabled, name: r }) : $(r, !0, a.value), { ...h ? { disabled: a.disabled || s.disabled } : {}, ...s.progressive ? { required: !!a.required, min: ce(a.min), max: ce(a.max), minLength: ce(a.minLength), maxLength: ce(a.maxLength), pattern: ce(a.pattern) } : {}, name: r, onChange: J, onBlur: J, ref: (f) => { if (f) { Ee(r, a), l = y(i, r); const c = O(f.value) && f.querySelectorAll && f.querySelectorAll("input,select,textarea")[0] || f, g = Zt(c), w = l._f.refs || []; if (g ? w.find((k) => k === c) : c === l._f.ref) return; x(i, r, { _f: { ...l._f, ...g ? { refs: [ ...w.filter(ke), c, ...Array.isArray(y(n, r)) ? [{}] : [] ], ref: { type: c.type, name: r } } : { ref: c } } }), $(r, !1, void 0, c); } else l = y(i, r, {}), l._f && (l._f.mount = !1), (s.shouldUnregister || a.shouldUnregister) && !(dt(d.array, r) && u.action) && d.unMount.add(r); } }; }, $e = () => s.shouldFocusError && fe(i, le, d.mount), St = (r) => { q(r) && (m.state.next({ disabled: r }), fe(i, (a, l) => { const h = y(i, l); h && (a.disabled = h._f.disabled || r, Array.isArray(h._f.refs) && h._f.refs.forEach((f) => { f.disabled = h._f.disabled || r; })); }, 0, !1)); }, je = (r, a) => async (l) => { let h; l && (l.preventDefault && l.preventDefault(), l.persist && l.persist()); let f = p(o); if (m.state.next({ isSubmitting: !0 }), s.resolver) { const { errors: c, values: g } = await ee(); t.errors = c, f = g; } else await K(i); if (d.disabled.size) for (const c of d.disabled) x(f, c, void 0); if (U(t.errors, "root"), P(t.errors)) { m.state.next({ errors: {} }); try { await r(f, l); } catch (c) { h = c; } } else a && await a({ ...t.errors }, l), $e(), setTimeout($e); if (m.state.next({ isSubmitted: !0, isSubmitting: !1, isSubmitSuccessful: P(t.errors) && !h, submitCount: t.submitCount + 1, errors: t.errors }), h) throw h; }, Dt = (r, a = {}) => { y(i, r) && (O(a.defaultValue) ? j(r, p(y(n, r))) : (j(r, a.defaultValue), x(n, r, p(a.defaultValue))), a.keepTouched || U(t.touchedFields, r), a.keepDirty || (U(t.dirtyFields, r), t.isDirty = a.defaultValue ? G(r, p(y(n, r))) : G()), a.keepError || (U(t.errors, r), _.isValid && D()), m.state.next({ ...t })); }, qe = (r, a = {}) => { const l = r ? p(r) : n, h = p(l), f = P(r), c = f ? n : h; if (a.keepDefaultValues || (n = l), !a.keepValues) { if (a.keepDirtyValues) { const g = /* @__PURE__ */ new Set([ ...d.mount, ...Object.keys(ue(n, o)) ]); for (const w of Array.from(g)) y(t.dirtyFields, w) ? x(c, w, y(o, w)) : j(w, y(c, w)); } else { if (Re && O(r)) for (const g of d.mount) { const w = y(i, g); if (w && w._f) { const k = Array.isArray(w._f.refs) ? w._f.refs[0] : w._f.ref; if (_e(k)) { const W = k.closest("form"); if (W) { W.reset(); break; } } } } for (const g of d.mount) j(g, y(c, g)); } o = p(c), m.array.next({ values: { ...c } }), m.state.next({ values: { ...c } }); } d = { mount: a.keepDirtyValues ? d.mount : /* @__PURE__ */ new Set(), unMount: /* @__PURE__ */ new Set(), array: /* @__PURE__ */ new Set(), disabled: /* @__PURE__ */ new Set(), watch: /* @__PURE__ */ new Set(), watchAll: !1, focus: "" }, u.mount = !_.isValid || !!a.keepIsValid || !!a.keepDirtyValues, u.watch = !!s.shouldUnregister, m.state.next({ submitCount: a.keepSubmitCount ? t.submitCount : 0, isDirty: f ? !1 : a.keepDirty ? t.isDirty : !!(a.keepDefaultValues && !re(r, n)), isSubmitted: a.keepIsSubmitted ? t.isSubmitted : !1, dirtyFields: f ? {} : a.keepDirtyValues ? a.keepDefaultValues && o ? ue(n, o) : t.dirtyFields : a.keepDefaultValues && r ? ue(n, r) : a.keepDirty ? t.dirtyFields : {}, touchedFields: a.keepTouched ? t.touchedFields : {}, errors: a.keepErrors ? t.errors : {}, isSubmitSuccessful: a.keepIsSubmitSuccessful ? t.isSubmitSuccessful : !1, isSubmitting: !1 }); }, We = (r, a) => qe(z(r) ? r(o) : r, a), kt = (r, a = {}) => { const l = y(i, r), h = l && l._f; if (h) { const f = h.refs ? h.refs[0] : h.ref; f.focus && (f.focus(), a.shouldSelect && z(f.select) && f.select()); } }, Ct = (r) => { t = { ...t, ...r }; }, He = { control: { register: Ee, unregister: we, getFieldState: Ne, handleSubmit: je, setError: Pe, _subscribe: Be, _runSchema: ee, _getWatch: F, _getDirty: G, _setValid: D, _setFieldArray: X, _setDisabledField: Ie, _setErrors: H, _getFieldArray: E, _reset: qe, _resetDefaultValues: () => z(s.defaultValues) && s.defaultValues().then((r) => { We(r, s.resetOptions), m.state.next({ isLoading: !1 }); }), _removeUnmounted: se, _disableForm: St, _subjects: m, _proxyFormState: _, get _fields() { return i; }, get _formValues() { return o; }, get _state() { return u; }, set _state(r) { u = r; }, get _defaultValues() { return n; }, get _names() { return d; }, set _names(r) { d = r; }, get _formState() { return t; }, get _options() { return s; }, set _options(r) { s = { ...s, ...r }; } }, subscribe: Et, trigger: ne, register: Ee, handleSubmit: je, watch: wt, setValue: j, getValues: ge, reset: We, resetField: Dt, clearErrors: xt, unregister: we, setError: Pe, setFocus: kt, getFieldState: Ne }; return { ...He, formControl: He }; } function fr(e = {}) { const s = S.useRef(void 0), t = S.useRef(void 0), [i, n] = S.useState({ isDirty: !1, isValidating: !1, isLoading: z(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: z(e.defaultValues) ? void 0 : e.defaultValues }); s.current || (s.current = { ...e.formControl ? e.formControl : dr(e), formState: i }, e.formControl && e.defaultValues && !z(e.defaultValues) && e.formControl.reset(e.defaultValues, e.resetOptions)); const o = s.current.control; return o._options = e, Me(() => { const u = o._subscribe({ formState: o._proxyFormState, callback: () => n({ ...o._formState }), reRenderRoot: !0 }); return n((d) => ({ ...d, isReady: !0 })), o._formState.isReady = !0, u; }, [o]), S.useEffect(() => o._disableForm(e.disabled), [o, e.disabled]), S.useEffect(() => { e.mode && (o._options.mode = e.mode), e.reValidateMode && (o._options.reValidateMode = e.reValidateMode), e.errors && !P(e.errors) && o._setErrors(e.errors); }, [o, e.errors, e.mode, e.reValidateMode]), S.useEffect(() => { e.shouldUnregister && o._subjects.state.next({ values: o._getWatch() }); }, [o, e.shouldUnregister]), S.useEffect(() => { if (o._proxyFormState.isDirty) { const u = o._getDirty(); u !== i.isDirty && o._subjects.state.next({ isDirty: u }); } }, [o, i.isDirty]), S.useEffect(() => { e.values && !re(e.values, t.current) ? (o._reset(e.values, o._options.resetOptions), t.current = e.values, n((u) => ({ ...u }))) : o._resetDefaultValues(); }, [o, e.values]), S.useEffect(() => { o._state.mount || (o._setValid(), o._state.mount = !0), o._state.watch && (o._state.watch = !1, o._subjects.state.next({ ...o._formState })), o._removeUnmounted(); }), s.current.formState = yt(i, o), s.current; } const nt = (e, s, t) => { if (e && "reportValidity" in e) { const i = y(t, s); e.setCustomValidity(i && i.message || ""), e.reportValidity(); } }, At = (e, s) => { for (const t in s.fields) { const i = s.fields[t]; i && i.ref && "reportValidity" in i.ref ? nt(i.ref, t, e) : i && i.refs && i.refs.forEach((n) => nt(n, t, e)); } }, yr = (e, s) => { s.shouldUseNativeValidation && At(e, s); const t = {}; for (const i in e) { const n = y(s.fields, i), o = Object.assign(e[i] || {}, { ref: n && n.ref }); if (hr(s.names || Object.keys(e), i)) { const u = Object.assign({}, y(t, i)); x(u, "root", o), x(t, i, u); } else x(t, i, o); } return t; }, hr = (e, s) => { const t = ot(s); return e.some((i) => ot(i).match(`^${t}\\.\\d+`)); }; function ot(e) { return e.replace(/\]|\[/g, ""); } function gr(e, s) { for (var t = {}; e.length; ) { var i = e[0], n = i.code, o = i.message, u = i.path.join("."); if (!t[u]) if ("unionErrors" in i) { var d = i.unionErrors[0].errors[0]; t[u] = { message: d.message, type: d.code }; } else t[u] = { message: o, type: n }; if ("unionErrors" in i && i.unionErrors.forEach(function(_) { return _.errors.forEach(function(V) { return e.push(V); }); }), s) { var v = t[u].types, A = v && v[i.code]; t[u] = gt(u, s, t, n, A ? [].concat(A, i.message) : i.message); } e.shift(); } return t; } function vr(e, s, t) { return t === void 0 && (t = {}), function(i, n, o) { try { return Promise.resolve(function(u, d) { try { var v = Promise.resolve(e[t.mode === "sync" ? "parse" : "parseAsync"](i, s)).then(function(A) { return o.shouldUseNativeValidation && At({}, o), { errors: {}, values: t.raw ? Object.assign({}, i) : A }; }); } catch (A) { return d(A); } return v && v.then ? v.then(void 0, d) : v; }(0, function(u) { if (function(d) { return Array.isArray(d == null ? void 0 : d.errors); }(u)) return { values: {}, errors: yr(gr(u.errors, !o.shouldUseNativeValidation && o.criteriaMode === "all"), o) }; throw u; })); } catch (u) { return Promise.reject(u); } }; } const lt = "FORM_ERROR"; class mr extends Error { constructor(s) { super("Form validation field when submitting"), this.fieldErrors = s; } } const Rr = ({ formSchema: e, ...s }) => { const t = fr({ resolver: vr(e), ...s }), { formState: { isValid: i, isDirty: n, errors: o }, setError: u } = t, d = () => new Promise((m, I) => { t.handleSubmit( (T) => { m(T); }, (T) => { const D = new mr(T); I(D); } )(); }), v = Tt( (m, I) => { const T = { message: Mt(m) }; u( // @ts-expect-error Form error is a special key, so type error here is expected lt, T, I ); }, [u] ), A = (m, I) => t.handleSubmit(async (...T) => { try { await m(...T); } catch (D) { v(D); } }, I), _ = o[lt]; return { ...t, submitAsync: d, formError: _, setFormError: v, isSubmitDisabled: !i || !n, handleSubmit: A }; }, br = ({ label: e, id: s, ...t }) => /* @__PURE__ */ ie(Bt, { ...t, children: /* @__PURE__ */ ie( Pt, { "aria-label": [ "More information about the", It(e) ?? s, "field" ].filter(Boolean).join(" ") } ) }), Lr = ({ label: e, name: s, className: t, checkEmptyError: i, render: n, error: o, tooltip: u, ...d }) => { const v = s; return /* @__PURE__ */ ie( Xt, { ...d, name: s, render: (A) => { const _ = `${v}-error`, V = o ?? A.fieldState.error, m = { ...A.field, id: v, "aria-errormessage": V ? _ : "", "aria-invalid": !!V }; return /* @__PURE__ */ Ce("div", { className: t, children: [ u || e ? /* @__PURE__ */ Ce(pt, { children: [ e && /* @__PURE__ */ ie(Nt, { htmlFor: v, children: e }), u && /* @__PURE__ */ ie(br, { tooltip: u, label: e, id: v }) ] }) : null, n({ ...A, field: m }), /* @__PURE__ */ ie(Ut, { id: _, checkEmpty: i, children: V == null ? void 0 : V.message }) ] }); } } ); }, Tr = ({ formError: e, className: s, prefix: t, ...i }) => e ? /* @__PURE__ */ ie(Wt, { padding: !1, className: Ht("pb-5", s), children: /* @__PURE__ */ Ce(qt, { ...i, children: [ t, $t(e) && "message" in e && jt(e.message) ? e.message : e ] }) }) : null; export { lt as F, Lr as a, Tr as b, Rr as u };