UNPKG

@mitcsutt/kiln-forms

Version:

Kiln forms: typed React forms on TanStack Form, in component or JSON schema mode, rendered through kiln-ui

264 lines (263 loc) • 7.75 kB
import { issuePath as e } from "../../../utils/paths.js"; import { getFormRuntime as t, isInactive as n, toFormApi as r, useResolvedForm as i } from "../../../runtime/formRuntime.js"; import { SubmitButton as a } from "../../form/SubmitButton/SubmitButton.js"; import { focusFirstInvalid as o, invalidFields as s, nextFrame as c } from "../../../runtime/focus.js"; import { useIsomorphicLayoutEffect as l } from "../../../utils/env.js"; import { FieldViewListBoundary as u } from "../../fields/FieldView/FieldView.js"; import { FieldScope as d, useFieldScope as f } from "../FieldScope/FieldScope.js"; import { revealFieldErrors as p } from "../../../runtime/reveal.js"; import { useScopeErrors as m } from "../../../hooks/useScopeErrors.js"; import { useOrderedRegistry as h } from "../internal/registry.js"; import { useAnnouncer as ee } from "../internal/announcer.js"; import { FormStepsContext as g } from "./context.js"; import { useCallback as _, useContext as v, useId as y, useMemo as b, useRef as x, useState as te } from "react"; import { Fragment as ne, jsx as S, jsxs as C } from "react/jsx-runtime"; import { ActionBar as w, Button as T, Heading as E, Stack as D, Stepper as O, Text as k } from "@mitcsutt/kiln-ui"; import { flushSync as re } from "react-dom"; //#region src/components/layouts/FormSteps/FormSteps.tsx var ie = /* @__PURE__ */ new Set([ "button", "submit", "reset", "image", "checkbox", "radio", "file" ]); async function ae(t, n, r, i) { let a = await n["~standard"].validate(t.state.values), o = /* @__PURE__ */ new Map(); for (let t of a.issues ?? []) { let n = e(t); r.includes(n) && !o.has(n) && o.set(n, t.message); } for (let e of r) { let n = o.get(e), r = i.get(e); if (n === void 0 && r === void 0) continue; let a = { applied: !1 }; t.setFieldMeta(e, (e) => { let t = e.errorMap.onSubmit; return t !== void 0 && t !== r ? e : (a.applied = !0, { ...e, errorMap: { ...e.errorMap, onSubmit: n } }); }), n === void 0 || !a.applied ? i.delete(e) : i.set(e, n); } } function A({ label: e, value: u, defaultValue: d, onValueChange: f, linear: m = !0, nav: v = "auto", backLabel: y, nextLabel: E, submitLabel: k, headingLevel: A = 3, compactBelow: j, children: M }) { let N = i(), P = t(N), F = P.options.messages, [I, L] = h(), [R, z] = te(d), [oe, B] = ee(), se = x(/* @__PURE__ */ new Map()), V = u !== void 0, ce = R !== void 0 && I.some((e) => e.value === R), H = V ? u : ce ? R : I[0]?.value ?? R, U = I.findIndex((e) => e.value === H), W = I.length, G = x({ entries: I, current: H, index: U, linear: m, controlled: V, onValueChange: f }); l(() => { G.current = { entries: I, current: H, index: U, linear: m, controlled: V, onValueChange: f }; }); let K = _((e) => { e !== G.current.current && re(() => { G.current.controlled || z(e), G.current.onValueChange?.(e); }); }, []), q = _(async (e) => { (() => { let t = G.current.entries.find((t) => t.value === e)?.heading(); return !(!t || t.closest("[hidden]")); })() || await c(); let t = G.current.entries, n = t.findIndex((t) => t.value === e), r = t[n]; if (!r) return; let i = r.heading(); i?.focus(); let a = i?.textContent.replace(/\s+/g, " ").trim() ?? e; B(F.stepOf(n + 1, t.length, a)); }, [B, F]), J = _(async (e) => { let t = r(N), i = e.scope.names().filter((e) => P.fields.has(e) && !n(P, e)); return await Promise.all(i.map((e) => t.validateField(e, "submit"))), p(t, i, { quiet: !0 }), e.schema && await ae(t, e.schema, i, se.current), s(N, e.scope).length === 0 || (await o(N, e.scope), !1); }, [N, P]), Y = _(async () => { let { entries: e, index: t } = G.current, n = e[t]; if (!n || !await J(n)) return !1; let i = G.current.entries, a = i[i.findIndex((e) => e.value === n.value) + 1]; return a ? (K(a.value), await q(a.value), !0) : (await r(N).handleSubmit(), !0); }, [ q, K, N, J ]), X = _(() => { let { entries: e, index: t } = G.current, n = e[t - 1]; n && (K(n.value), q(n.value)); }, [q, K]), Z = _(async (e) => { let { entries: t, index: n, linear: r } = G.current, i = t.findIndex((t) => t.value === e); if (i < 0) return !1; if (i === n) return !0; if (r && i > n) for (let e = Math.max(n, 0); e < i; e += 1) { let n = t[e]; if (n && !await J(n)) return !1; } return K(e), await q(e), !0; }, [ q, K, J ]), Q = b(() => I.map((e, t) => { let n = e.errors > 0 ? "error" : t === U ? "current" : t < U ? "complete" : "upcoming"; return { value: e.value, title: e.title, status: n }; }), [I, U]), $ = b(() => ({ steps: Q, current: H ?? "", index: Math.max(U, 0), count: W, isFirst: U <= 0, isLast: U >= W - 1, next: Y, back: X, goTo: Z }), [ Q, H, U, W, Y, X, Z ]), le = b(() => ({ api: $, current: H, headingLevel: A, register: L, reveal: K }), [ $, H, A, L, K ]), ue = b(() => Q.map((e, t) => ({ value: e.value, label: e.title, status: t === U ? "current" : t < U ? "complete" : "upcoming", invalid: e.status === "error" })), [Q, U]), de = b(() => ({ complete: F.stepComplete, error: F.stepError }), [F]), fe = _((e, t, n) => /* @__PURE__ */ C(ne, { children: [ F.stepCompact(e + 1, t), " · ", n ] }), [F]); return /* @__PURE__ */ S(g.Provider, { value: le, children: /* @__PURE__ */ C(D, { gap: 6, children: [ W > 0 ? /* @__PURE__ */ S(O, { steps: ue, value: H ?? "", onStepSelect: (e) => void Z(e), statusLabels: de, compactBelow: j, formatCompact: fe, "aria-label": e }) : null, /* @__PURE__ */ S("div", { onKeyDown: (e) => { if (e.key !== "Enter" || e.defaultPrevented || $.isLast) return; let t = e.target; t instanceof HTMLInputElement && !ie.has(t.type) && (e.preventDefault(), Y()); }, children: M }), v === "auto" && W > 0 ? /* @__PURE__ */ C(w, { align: U <= 0 ? "end" : "between", children: [U <= 0 ? null : /* @__PURE__ */ S(T, { variant: "outline", tone: "neutral", onClick: X, children: y ?? F.back }), U >= W - 1 ? /* @__PURE__ */ S(a, { children: k ?? F.submit }) : /* @__PURE__ */ S(T, { onClick: () => void Y(), children: E ?? F.next })] }) : null, oe ] }) }); } function j() { let e = v(g); if (!e) throw Error("[@mitcsutt/kiln-forms] <FormSteps.Step> must be rendered inside <FormSteps>."); return e; } function M({ value: e, title: t, description: n, schema: r, children: i }) { let { current: a, register: o, headingLevel: s } = j(), c = f(); if (!c) throw Error("[@mitcsutt/kiln-forms] FormStep: missing scope."); let u = m(c), d = x(null), p = x(null), h = y(); return l(() => o({ key: e, value: e, title: t, scope: c, schema: r, errors: u, element: () => d.current, heading: () => p.current }), [ o, e, t, c, r, u ]), /* @__PURE__ */ C(D, { as: "section", ref: d, gap: 5, hidden: a !== e, "aria-labelledby": h, children: [/* @__PURE__ */ C(D, { gap: 2, children: [/* @__PURE__ */ S(E, { ref: p, level: s, id: h, tabIndex: -1, children: t }), n == null ? null : /* @__PURE__ */ S(k, { tone: "muted", children: n })] }), i] }); } function N({ scopeNames: e, ...t }) { let { reveal: n } = j(), r = t.value, i = _(() => { n(r); }, [n, r]); return /* @__PURE__ */ S(d, { reveal: i, names: e, children: /* @__PURE__ */ S(M, { ...t }) }); } function P(e) { return /* @__PURE__ */ S(u, { children: /* @__PURE__ */ S(A, { ...e }) }); } var F = Object.assign(P, { Step: N }); //#endregion export { N as FormStep, F as FormSteps }; //# sourceMappingURL=FormSteps.js.map