UNPKG

@mitcsutt/kiln-forms

Version:

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

72 lines (71 loc) • 2.46 kB
import { FieldPresentation as e } from "../../fields/FieldPresentation/FieldPresentation.js"; import { formatErrorText as t, getFormRuntime as n, isInactive as r, useResolvedForm as i } from "../../../runtime/formRuntime.js"; import { pickErrors as a } from "../../../runtime/errors.js"; import { FieldViewListBoundary as o } from "../../fields/FieldView/FieldView.js"; import { FieldScope as s } from "../FieldScope/FieldScope.js"; import { areErrorsVisible as c, isQuietMeta as l } from "../../../runtime/reveal.js"; import { useCallback as u, useId as d, useState as f } from "react"; import { useSelector as p } from "@tanstack/react-form"; import { jsx as m, jsxs as h } from "react/jsx-runtime"; import { ErrorIcon as g, Fieldset as _, Inline as v, Stack as y, Text as b } from "@mitcsutt/kiln-ui"; //#region src/components/layouts/FormSentence/FormSentence.tsx function x({ name: e, id: o }) { let s = i(), u = n(s), d = p(s.store, (t) => t.submissionAttempts > 0 || l(t.fieldMeta[e])), f = p(s.store, (n) => { let i = n.fieldMeta[e]; if (!i || r(u, e)) return ""; let o = a(i.errorMap)[0]; return !o || !c(u, i, n.submissionAttempts > 0) ? "" : t(u, o); }); if (f === "") return null; let _ = u.fields.get(e)?.getLabel(); return /* @__PURE__ */ m(b, { as: "p", id: o, size: "sm", tone: "critical", role: d ? void 0 : "alert", children: /* @__PURE__ */ h(v, { as: "span", gap: 1, align: "center", wrap: !1, children: [/* @__PURE__ */ m(g, { size: "sm" }), /* @__PURE__ */ m("span", { children: _ ? `${_}: ${f}` : f })] }) }); } function S({ label: t, scopeNames: n, children: r }) { let i = d(), [a, o] = f([]), c = u((e) => `${i}${e}-error`, [i]); return /* @__PURE__ */ m(s, { names: n, onNamesChange: o, children: /* @__PURE__ */ m(e, { layout: "inline", labelHidden: !0, errorPlacement: "external", describedBy: c, children: /* @__PURE__ */ m(_, { legend: t, legendHidden: !0, children: /* @__PURE__ */ h(y, { gap: 3, children: [/* @__PURE__ */ m(b, { as: "div", children: r }), a.length > 0 ? /* @__PURE__ */ m(y, { gap: 1, children: a.map((e) => /* @__PURE__ */ m(x, { name: e, id: c(e) }, e)) }) : null] }) }) }) }); } function C(e) { return /* @__PURE__ */ m(o, { children: /* @__PURE__ */ m(S, { ...e }) }); } //#endregion export { C as FormSentence }; //# sourceMappingURL=FormSentence.js.map