@mitcsutt/kiln-forms
Version:
Kiln forms: typed React forms on TanStack Form, in component or JSON schema mode, rendered through kiln-ui
82 lines (81 loc) • 2.67 kB
JavaScript
import { formatErrorText as e, getFormRuntime as t, isInactive as n, useResolvedForm as r } from "../../../runtime/formRuntime.js";
import { normaliseErrors as i, pickErrors as a } from "../../../runtime/errors.js";
import { focusField as o, sortByDomOrder as s } from "../../../runtime/focus.js";
import { shallowEqual as c } from "../../../utils/shallow.js";
import { useEffect as l, useMemo as u, useRef as d } from "react";
import { useSelector as f } from "@tanstack/react-form";
import { jsx as p, jsxs as m } from "react/jsx-runtime";
import { Alert as h, Link as g, List as _ } from "@mitcsutt/kiln-ui";
//#region src/components/form/ErrorSummary/ErrorSummary.tsx
var v = "\0", y = [];
function b({ form: b, title: x, headingLevel: S = 2 }) {
let C = r(b), w = t(C), T = d(null);
l(() => {
let e = {
mounted: !0,
focus: () => T.current?.focus()
};
return w.summary = e, () => {
w.summary === e && (w.summary = null);
};
}, [w]);
let E = f(C.store, (e) => e.submissionAttempts), D = f(C.store, (t) => {
if (t.submissionAttempts === 0) return y;
let r = [];
for (let n of i(Object.values(t.errorMap))) r.push(`${v}${e(w, n)}`);
let o = t.fieldMeta;
for (let [t, i] of Object.entries(o)) {
if (!i || n(w, t)) continue;
let o = a(i.errorMap)[0];
o && r.push(`${t}${v}${e(w, o)}`);
}
return r;
}, { compare: c }), { formErrors: O, fieldErrors: k } = u(() => {
let e = [], t = [];
for (let n of D) {
let r = n.indexOf(v), i = n.slice(0, r), a = n.slice(r + 1);
i === "" ? e.push(a) : t.push({
name: i,
message: a
});
}
let n = s(t.map((e) => ({
...e,
element: () => w.fields.get(e.name)?.element() ?? null
})));
return {
formErrors: [...new Set(e)],
fieldErrors: n
};
}, [D, w]);
if (E === 0 || O.length === 0 && k.length === 0) return null;
let A = w.options.messages, j = (e) => (t) => {
t.preventDefault(), o(C, e);
};
return /* @__PURE__ */ p(h, {
ref: T,
tone: "critical",
tabIndex: -1,
title: /* @__PURE__ */ p("span", {
role: "heading",
"aria-level": S,
children: x ?? A.errorSummaryTitle
}),
children: /* @__PURE__ */ m(_, {
as: "ol",
divided: !1,
density: "compact",
children: [O.map((e) => /* @__PURE__ */ p(_.Item, { children: e }, `form:${e}`)), k.map(({ name: e, message: t }) => {
let n = w.fields.get(e), r = n?.getLabel() ?? e, i = t === "" ? r : `${r}: ${t}`;
return /* @__PURE__ */ p(_.Item, { children: n ? /* @__PURE__ */ p(g, {
href: `#${n.id}`,
onClick: j(e),
children: i
}) : i }, e);
})]
})
}, E);
}
//#endregion
export { b as ErrorSummary };
//# sourceMappingURL=ErrorSummary.js.map