@mitcsutt/kiln-forms
Version:
Kiln forms: typed React forms on TanStack Form, in component or JSON schema mode, rendered through kiln-ui
125 lines (124 loc) • 3.34 kB
JavaScript
import { fieldContext as e, formContext as t, useFormContext as n } from "./contexts.js";
import { mergeMessages as r } from "../runtime/messages.js";
import { attachFormRuntime as i, createFormRuntime as a } from "../runtime/formRuntime.js";
import { SubmitButton as o } from "../components/form/SubmitButton/SubmitButton.js";
import { ResetButton as s } from "../components/form/ResetButton/ResetButton.js";
import { ErrorSummary as c } from "../components/form/ErrorSummary/ErrorSummary.js";
import { FormStatus as l } from "../components/form/FormStatus/FormStatus.js";
import { isDev as u } from "../utils/env.js";
import { bindFields as d, fieldComponentName as f } from "./bindFields.js";
import { createKitAppField as p } from "./kitAppField.js";
import { guardSubmit as m } from "../runtime/submit.js";
import { configureRuntime as h, toTanStackOptions as g } from "./toTanStackOptions.js";
import { defineSchemaFor as _ } from "../schema/core/define.js";
import { createSchemaComponents as v, schemaRegistries as y } from "../schema/render/kitSchema.js";
import { useMemo as b, useState as x } from "react";
import { createFormHook as S } from "@tanstack/react-form";
//#region src/kit/createFormKit.tsx
var C = /^[a-z][A-Za-z0-9]*$/, w = {
SubmitButton: o,
ResetButton: s,
ErrorSummary: c,
FormStatus: l
};
function T(e) {
return E(e);
}
function E(o) {
let { fields: s } = o;
if (u()) {
for (let e of Object.keys(s)) if (!C.test(e)) throw Error(`[@mitcsutt/kiln-forms] Field kind "${e}" must be camelCase ([a-z][A-Za-z0-9]*).`);
}
let c = Object.fromEntries(Object.entries(s).map(([e, t]) => [f(e), t])), l = o, T = {
...w,
...l.formComponents
}, D = S({
fieldContext: e,
formContext: t,
fieldComponents: c,
formComponents: T
}), O = {
messages: l.messages,
formatError: l.formatError
};
function k(e) {
let t = e, [n] = x(a);
h(n, t, O), n.registry = s, n.dragAndDrop = l.dragAndDrop ?? null;
let r = D.useAppForm(g(t, n));
return i(r, n), b(() => (m(r), r.AppField = p(r, r.AppField, c), Object.assign(r, d(r, s))), [r]);
}
function A(e) {
let t = e.render, n = e.props;
function r(e) {
return t({
...n,
...e
});
}
return r;
}
function j(e) {
return n();
}
function M(e) {
return d(e, s);
}
function N(e) {
let t = e.fields ?? {};
if (u()) {
for (let e of Object.keys(t)) if (e in s) throw Error(`[@mitcsutt/kiln-forms] kit.extend: field kind "${e}" already exists.`);
}
let n = o;
return E({
...n,
...e,
fields: {
...s,
...t
},
formComponents: {
...n.formComponents,
...e.formComponents
},
loaders: {
...n.loaders,
...e.loaders
},
validators: {
...n.validators,
...e.validators
},
computers: {
...n.computers,
...e.computers
},
nodes: {
...n.nodes,
...e.nodes
},
layouts: {
...n.layouts,
...e.layouts
},
messages: r(n.messages, e.messages)
});
}
let P = v(y(s, l));
return {
useAppForm: k,
withForm: A,
useTypedAppFormContext: j,
withFieldGroup: D.withFieldGroup,
useFields: M,
extend: N,
defineFormSchema: _(),
SchemaForm: P.SchemaForm,
SchemaNode: P.SchemaNode,
registries: o,
fieldContext: e,
formContext: t
};
}
//#endregion
export { T as createFormKit };
//# sourceMappingURL=createFormKit.js.map