@mitcsutt/kiln-forms
Version:
Kiln forms: typed React forms on TanStack Form, in component or JSON schema mode, rendered through kiln-ui
56 lines (55 loc) • 1.64 kB
JavaScript
import { registerDerive as e, toFormApi as t } from "../../runtime/formRuntime.js";
import { warnOnce as n } from "./warn.js";
import { analyseSchema as r } from "../core/collect.js";
import { SchemaRenderContext as i } from "./context.js";
import { schemaDeriveRules as a } from "./derive.js";
import { SchemaNodeView as o } from "./SchemaNodeView.js";
import { useEffect as s, useMemo as c } from "react";
import { jsx as l } from "react/jsx-runtime";
//#region src/schema/render/SchemaForm.tsx
var u = {};
function d({ form: e, schema: n, context: i, registries: a }) {
let o = i ? JSON.stringify(i) : "", s = c(() => o === "" ? u : JSON.parse(o), [o]);
return c(() => ({
form: e,
api: t(e),
schema: n,
analysis: r(n),
registries: a,
context: s
}), [
e,
n,
a,
s
]);
}
function f({ form: t, schema: n, registries: r }) {
let i = a(n, r.computers);
s(() => i.length === 0 ? void 0 : e(t, i, i), [t, i]);
}
function p(e) {
let t = d(e);
return f(t), /* @__PURE__ */ l(i.Provider, {
value: t,
children: /* @__PURE__ */ l(o, {
node: t.schema.root,
prefix: ""
})
});
}
function m({ id: e, prefix: t = "", ...r }) {
let a = d(r);
f(a);
let s = a.analysis.nodeById.get(e);
return s ? (t === "" && a.analysis.repeaterOf(s) && n(`SchemaNode: node "${e}" is inside a repeater item; pass \`prefix\` (e.g. "guests[0]").`), /* @__PURE__ */ l(i.Provider, {
value: a,
children: /* @__PURE__ */ l(o, {
node: s,
prefix: t
})
})) : (n(`SchemaNode: no node with id "${e}".`), null);
}
//#endregion
export { p as SchemaFormRenderer, m as SchemaNodeRenderer };
//# sourceMappingURL=SchemaForm.js.map