UNPKG

@mitcsutt/kiln-forms

Version:

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

134 lines (133 loc) • 4.92 kB
import { useFieldPresentation as e } from "../../components/fields/FieldPresentation/FieldPresentation.js"; import { SubmitButton as t } from "../../components/form/SubmitButton/SubmitButton.js"; import { ResetButton as n } from "../../components/form/ResetButton/ResetButton.js"; import { ErrorSummary as r } from "../../components/form/ErrorSummary/ErrorSummary.js"; import { FormStatus as i } from "../../components/form/FormStatus/FormStatus.js"; import { warnOnce as a } from "./warn.js"; import { Repeater as o } from "../../components/layouts/Repeater/Repeater.js"; import { joinPath as s } from "../core/values.js"; import { When as c } from "../../components/layouts/When/When.js"; import { SCOPED_LAYOUTS as l, isDefaultLayout as u } from "./layouts.js"; import { contentNodeProps as d, isContentNode as f, isCustomNode as p, isFieldNode as m, isLayoutNode as h, isRepeaterNode as g, layoutNodeProps as _ } from "../core/nodes.js"; import { useSchemaRender as v } from "./context.js"; import { withRequired as y } from "../core/rules.js"; import { ruleValidators as b } from "./rules.js"; import { FieldNodeView as x } from "./FieldNodeView.js"; import { createElement as S, useCallback as C, useMemo as w } from "react"; import { Fragment as T, jsx as E } from "react/jsx-runtime"; import { Alert as D, Divider as O, Heading as k, Text as A } from "@mitcsutt/kiln-ui"; //#region src/schema/render/SchemaNodeView.tsx var j = {}; function M(e, t) { return e.id === void 0 ? `#${String(t)}` : `id:${e.id}`; } function N(e, t) { return e.map((e, n) => /* @__PURE__ */ E(I, { node: e, prefix: t }, M(e, n))); } function P(e, t) { let { registries: n } = t; if (m(e)) return Object.prototype.hasOwnProperty.call(n.fields, e.kind) ? void 0 : `Unknown field kind "${e.kind}"`; if (!g(e)) { if (h(e)) return Object.prototype.hasOwnProperty.call(n.layouts, e.layout) ? void 0 : `Unknown layout "${e.layout}"`; if (!f(e)) return p(e) ? Object.prototype.hasOwnProperty.call(n.nodes, e.custom) ? void 0 : `Unknown custom node "${e.custom}"` : "Unknown node shape"; } } function F(e) { return m(e) || g(e) ? e.whenHidden : void 0; } function I({ node: e, prefix: t }) { let n = v(), r = P(e, n); if (r !== void 0) return a(`${r} — the node renders nothing. Validate untrusted schemas with parseFormSchema.`), null; let i = /* @__PURE__ */ E(L, { node: e, prefix: t }); return e.when ? /* @__PURE__ */ E(c, { form: n.form, condition: e.when, context: n.context, whenHidden: F(e), animate: e.whenAnimate === !0, scopeNames: n.analysis.names(e, t), children: i }) : i; } function L({ node: e, prefix: t }) { return m(e) ? /* @__PURE__ */ E(x, { node: e, prefix: t }) : g(e) ? /* @__PURE__ */ E(R, { node: e, prefix: t }) : h(e) ? /* @__PURE__ */ E(z, { node: e, prefix: t }) : f(e) ? /* @__PURE__ */ E(B, { node: e }) : p(e) ? /* @__PURE__ */ E(V, { node: e }) : null; } function R({ node: e, prefix: t }) { let { form: n, registries: r } = v(), i = w(() => _(e), [e]), a = w(() => y(e.rules, !1), [e.rules]), c = w(() => b(a, void 0, n, r.validators), [ a, n, r.validators ]), l = e.newItem, u = C(() => structuredClone(l), [l]), d = e.item, f = C((e) => N(d, e.name), [d]), p = { ...i, form: n, name: s(t, e.name), newItem: u }; return c && (p.validators = c), S(o, { ...p, children: f }); } function z({ node: e, prefix: t }) { let { form: n, analysis: r, registries: i } = v(), a = i.layouts[e.layout], o = a, s = w(() => _(e), [e]), c = {}; return l.has(e.layout) && (c.scopeNames = r.names(e, t)), a && !u(a) && (c.node = e, c.form = n), S(o, { ...s, ...c }, ...N(e.children, t)); } function B({ node: o }) { let { form: s } = v(), c = e().layout === "inline", l = d(o), u = typeof l.text == "string" ? l.text : void 0, f = typeof l.label == "string" ? l.label : void 0; switch (o.content) { case "heading": return /* @__PURE__ */ E(k, { level: l.level, children: u }); case "text": return E(c ? T : A, { children: u }); case "alert": return /* @__PURE__ */ E(D, { tone: l.tone, title: typeof l.title == "string" ? l.title : void 0, children: u }); case "divider": return /* @__PURE__ */ E(O, {}); case "submit": return /* @__PURE__ */ E(t, { form: s, children: f }); case "reset": return /* @__PURE__ */ E(n, { form: s, children: f }); case "errorSummary": return /* @__PURE__ */ E(r, { form: s, title: typeof l.title == "string" ? l.title : void 0 }); case "status": return /* @__PURE__ */ E(i, { form: s }); default: return a(`Unknown content "${String(o.content)}" — the node renders nothing.`), null; } } function V({ node: e }) { let { form: t, registries: n } = v(), r = n.nodes[e.custom]; return /* @__PURE__ */ E(r, { form: t, props: e.props ?? j, node: e }); } //#endregion export { I as SchemaNodeView }; //# sourceMappingURL=SchemaNodeView.js.map