UNPKG

@mitcsutt/kiln-forms

Version:

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

49 lines (48 loc) • 2 kB
import { useFieldPresentation as e } from "../../fields/FieldPresentation/FieldPresentation.js"; import { coreApi as t, getFormRuntime as n, markInactive as r, toFormApi as i } from "../../../runtime/formRuntime.js"; import { useIsomorphicLayoutEffect as a } from "../../../utils/env.js"; import { FieldScope as o } from "../FieldScope/FieldScope.js"; import { evaluateCondition as s } from "../../../schema/core/conditions.js"; import { useMemo as c, useState as l } from "react"; import { getBy as u, useSelector as d } from "@tanstack/react-form"; import { Fragment as f, jsx as p, jsxs as m } from "react/jsx-runtime"; import { Collapse as h } from "@mitcsutt/kiln-ui"; //#region src/components/layouts/When/When.tsx function g(e, r) { let a = i(e), o = n(e), s = t(e).options.defaultValues; for (let e of r) { let t = o.fieldDefaults.has(e) ? o.fieldDefaults.get(e) : u(s, e); a.setFieldValue(e, t, { dontUpdateMeta: !0 }); } } var _ = []; function v({ form: t, is: n, condition: u, context: v, whenHidden: y = "prune", names: b, scopeNames: x, fallback: S, animate: C = !1, children: w }) { let T = i(t), E = d(T.store, (e) => n ? n(e.values) : !u || s(u, e.values, v)), D = [...b ?? [], ...x ?? []].join("\0"), O = c(() => D === "" ? _ : D.split("\0"), [D]), [k, A] = l(O), j = e(), M = C && j.mode !== "view" && j.layout !== "inline"; return a(() => { if (!(E || k.length === 0)) return y === "reset" && g(t, k), r(t, k, "hidden", y === "keep" ? "keep" : "prune"); }, [ E, k, y, t ]), M ? /* @__PURE__ */ m(f, { children: [/* @__PURE__ */ p(o, { names: O, onNamesChange: A, children: /* @__PURE__ */ p(h, { open: E, keepMounted: !1, children: w }) }), S == null ? null : /* @__PURE__ */ p(h, { open: !E, keepMounted: !1, children: S })] }) : /* @__PURE__ */ m(f, { children: [/* @__PURE__ */ p(o, { names: O, onNamesChange: A, children: E ? w : null }), E ? null : S] }); } //#endregion export { v as When }; //# sourceMappingURL=When.js.map