UNPKG

@mitcsutt/kiln-forms

Version:

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

169 lines (168 loc) • 5.69 kB
import { mergeMessages as e } from "../runtime/messages.js"; import { isAtOrUnder as t, normalisePath as n } from "../utils/paths.js"; import { getFormRuntime as r, isInactive as i, runtimeDeriveRules as a } from "../runtime/formRuntime.js"; import { focusOnInvalidSubmit as o } from "../runtime/focus.js"; import { isStandardSchema as s, schemaValidator as c, schemaValidatorAsync as l } from "../runtime/standardSchema.js"; import { createSubmitHandler as u, guardSubmit as d } from "../runtime/submit.js"; import { kitValidationLogic as f } from "../runtime/validationLogic.js"; import { evaluate as p } from "@tanstack/react-form"; //#region src/kit/toTanStackOptions.ts function m(e) { return typeof e == "object" && !!e && !Array.isArray(e); } function h(e) { return e == null || e === !1 || e === ""; } function g(e, t) { if (!h(e)) { if (m(e) && !("message" in e) && ("fields" in e || "form" in e)) { let r = {}, a = m(e.fields) ? e.fields : {}; for (let [e, o] of Object.entries(a)) { if (h(o)) continue; let a = n(e); i(t, a) || (r[a] = o); } let o = h(e.form) ? void 0 : e.form; return o === void 0 && Object.keys(r).length === 0 ? void 0 : o === void 0 ? { fields: r } : { form: o, fields: r }; } return e; } } function _(e) { if (!e) return; if (s(e)) return c(e); let t = e; return (e) => g(t(e), r(e.formApi)); } function v(e) { if (!e) return; if (s(e)) return l(e); let t = e; return async (e) => g(await t(e), r(e.formApi)); } function y(e) { return h(e) ? [] : Array.isArray(e) ? e : [e]; } function b(e, t) { if (e === void 0) return t; if (t === void 0) return e; let n = m(e) && "fields" in e ? e : { form: e, fields: {} }, r = m(t) && "fields" in t ? t : { form: t, fields: {} }, i = { ...n.fields }; for (let [e, t] of Object.entries(r.fields)) i[e] = e in i ? [...y(i[e]), ...y(t)] : t; let a = [...y(n.form), ...y(r.form)]; return a.length === 0 ? { fields: i } : { form: a.length === 1 ? a[0] : a, fields: i }; } function x(...e) { let t = e.filter((e) => e !== void 0); return t.length <= 1 ? t[0] : (e) => t.reduce((t, n) => b(t, n(e)), void 0); } function S(...e) { let t = e.filter((e) => e !== void 0); return t.length <= 1 ? t[0] : async (e) => (await Promise.all(t.map((t) => t(e)))).reduce((e, t) => b(e, t), void 0); } var C = /* @__PURE__ */ new WeakMap(); function w(t, n, r) { t.options = { errorVisibility: n.errorVisibility ?? "blur", validateOn: n.validateOn ?? "blur", focusOnInvalid: n.focusOnInvalid ?? "auto", afterSubmit: n.afterSubmit ?? "rebaseline", formatError: n.formatError ?? r.formatError ?? ((e) => e.message), messages: e(r.messages, n.messages) }, t.userDefaults = n.defaultValues, t.baseline && !p(t.baseline.source, n.defaultValues) && (t.baseline = null); } function T(e, r) { let i = e.validators ?? {}, s = e.schema, m = e.listeners ?? {}, h = { defaultValues: r.baseline && p(r.baseline.source, e.defaultValues) ? r.baseline.values : e.defaultValues, validationLogic: f({ validateOn: e.validateOn ?? "blur", base: e.validationLogic }), validators: { onMount: _(i.onMount), onChange: _(i.onChange), onChangeAsync: v(i.onChangeAsync), onChangeAsyncDebounceMs: i.onChangeAsyncDebounceMs, onBlur: _(i.onBlur), onBlurAsync: v(i.onBlurAsync), onBlurAsyncDebounceMs: i.onBlurAsyncDebounceMs, onSubmit: _(i.onSubmit), onSubmitAsync: v(i.onSubmitAsync), onDynamic: x(s && !e.schemaAsync ? c(s) : void 0, _(i.onDynamic)), onDynamicAsync: S(s && e.schemaAsync ? l(s) : void 0, v(i.onDynamicAsync)), onDynamicAsyncDebounceMs: i.onDynamicAsyncDebounceMs }, listeners: { onBlur: m.onBlur, onBlurDebounceMs: m.onBlurDebounceMs, onSubmit: m.onSubmit, onChange: ({ formApi: i, fieldApi: o }) => { let s = o.name; r.changing = s, queueMicrotask(() => { r.changing === s && (r.changing = null); }); let c = [...e.derive ?? [], ...a(r)], l = r.changing; for (let e of c) { if (e.field === s || !e.from.some((e) => t(s, n(e)))) continue; let r = e.compute(i.state.values); p(r, i.getFieldValue(e.field)) || i.setFieldValue(e.field, r, { dontUpdateMeta: !0 }); } r.changing = l; let u = m.onChange; if (!u) return; let d = m.onChangeDebounceMs ?? 0; if (d <= 0) { u({ formApi: i, fieldApi: o }); return; } let f = C.get(r); f || (f = /* @__PURE__ */ new Map(), C.set(r, f)); let h = f.get(s); h && clearTimeout(h), f.set(s, setTimeout(() => { f.delete(s), u({ formApi: i, fieldApi: o }); }, d)); }, onMount: (e) => { r.core = e.formApi, d(e.formApi), m.onMount?.(e); } }, onSubmit: u(() => ({ schema: s, afterSubmit: r.options.afterSubmit, onSubmit: e.onSubmit, onSubmitError: e.onSubmitError })), onSubmitInvalid: ({ value: t, formApi: n }) => { if (!n.state.isFieldsValid) { let e = n.validate("submit"); e instanceof Promise && e.catch(() => void 0); } o(n), e.onSubmitInvalid?.({ value: t, formApi: n }); }, canSubmitWhenInvalid: e.canSubmitWhenInvalid ?? !0 }; return e.onSubmitMeta !== void 0 && (h.onSubmitMeta = e.onSubmitMeta), e.formId !== void 0 && (h.formId = e.formId), e.asyncAlways !== void 0 && (h.asyncAlways = e.asyncAlways), e.asyncDebounceMs !== void 0 && (h.asyncDebounceMs = e.asyncDebounceMs), e.transform !== void 0 && (h.transform = e.transform), e.defaultState !== void 0 && (h.defaultState = e.defaultState), h; } //#endregion export { w as configureRuntime, g as normaliseFormResult, T as toTanStackOptions }; //# sourceMappingURL=toTanStackOptions.js.map