UNPKG

@stanfordspezi/spezi-web-design-system

Version:

Stanford Biodesign Digital Health Spezi Web Design System

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