@mitcsutt/kiln-forms
Version:
Kiln forms: typed React forms on TanStack Form, in component or JSON schema mode, rendered through kiln-ui
139 lines (138 loc) • 4.09 kB
JavaScript
import { useFieldContext as e } from "../kit/contexts.js";
import { useFieldPresentation as t } from "../components/fields/FieldPresentation/FieldPresentation.js";
import { getFormRuntime as n, markInactive as r } from "../runtime/formRuntime.js";
import { focusTarget as i } from "../runtime/focus.js";
import { isDev as a, useIsomorphicLayoutEffect as o } from "../utils/env.js";
import { scopeChain as s, useScopeNode as c } from "../components/layouts/FieldScope/FieldScope.js";
import { isQuietMeta as l, isRevealedMeta as u } from "../runtime/reveal.js";
import { readFieldLabel as d } from "../runtime/labels.js";
import { fieldDisplay as f } from "../runtime/fieldDisplay.js";
import { accepts as p } from "../kit/accepts.js";
import { useCallback as m, useEffect as h, useId as g, useRef as _ } from "react";
import { useSelector as v } from "@tanstack/react-form";
//#region src/hooks/useFieldBinding.ts
function y(...e) {
let t = e.filter((e) => typeof e == "string" && e !== "").join(" ");
return t === "" ? void 0 : t;
}
function b(e) {
return Object.fromEntries(Object.entries(e).filter(([, e]) => e !== void 0));
}
var x = 1, S = 2, C = 4;
function w(p) {
let w = e(), T = w.form, E = n(T), D = t(), O = c(), k = g(), A = w.name, j = w.state.value, M = w.state.meta, N = v(T.store, (e) => {
let t = e.fieldMeta[A];
return (e.submissionAttempts > 0 ? x : 0) | (u(t) ? S : 0) | (l(t) ? C : 0);
}), P = D.mode ?? "edit", F = D.disabled === !0 || p.disabled === !0, I = D.readOnly === !0 || p.readOnly === !0, L = p.excluded === !0, R = F || I || L, z = F ? "disabled" : I ? "readOnly" : "excluded";
o(() => {
if (R && P !== "view") return r(T, [A], z, L ? "prune" : "keep");
}, [
T,
A,
R,
z,
L,
P
]);
let B = w.options.defaultValue;
o(() => {
B !== void 0 && E.fieldDefaults.set(A, B);
}, [
E,
A,
B
]);
let V = _(null), H = m((e) => {
V.current = e;
}, []), U = _(p.check);
o(() => {
U.current = p.check;
});
let W = p.check !== void 0;
o(() => {
if (P === "view") return;
let e = s(O), t = e.map((e) => e.register(A)), n = () => V.current ?? (typeof document > "u" ? null : document.getElementById(k)), r = {
id: k,
element: n,
scopes: e,
focus: () => {
let e = n();
e && i(e)?.focus();
},
getLabel: () => d(typeof document > "u" ? null : document.getElementById(k) ?? V.current, A, A)
};
return W && (r.check = (e) => U.current?.(e)), E.fields.set(A, r), () => {
for (let e of t) e();
E.fields.get(A) === r && E.fields.delete(A);
};
}, [
E,
A,
k,
O,
P,
W
]);
let G = _(!1), K = p.accepts;
h(() => {
G.current || !a() || K(j) || (G.current = !0, console.error(`[@mitcsutt/kiln-forms] Field "${A}" holds ${JSON.stringify(j)}, which this field component can't edit. Bind a field component whose value type matches (see the value contracts in the forms docs).`));
}, [
K,
j,
A
]);
let { handleChange: q, handleBlur: J } = w, Y = m((e) => {
F || I || q(e);
}, [
q,
F,
I
]), X = m(() => {
J();
}, [J]), Z = p.warn, Q = f(E, {
mode: P,
inactive: R,
meta: M,
submitted: (N & x) !== 0,
revealed: (N & S) !== 0,
quiet: (N & C) !== 0,
warning: Z ? () => Z(j) : void 0
}), $ = D.errorPlacement === "external", ee = b({
id: k,
name: A,
"data-field": A,
error: Q.error === "" || Q.error,
errorLive: Q.errorLive,
errorHidden: $ || void 0,
warning: Q.warning,
required: p.required,
disabled: F || void 0,
readOnly: I || void 0,
validating: M.isValidating || void 0,
layout: p.layout ?? D.layout,
labelHidden: p.labelHidden ?? D.labelHidden,
"aria-describedby": y(p["aria-describedby"], $ ? D.describedBy?.(A) : void 0)
});
return {
api: w,
name: A,
id: k,
value: j,
setValue: Y,
onBlur: X,
ref: H,
mode: P,
state: b({
showError: Q.showError,
error: Q.error,
warning: Q.warning,
isValidating: M.isValidating,
isDirty: !M.isDefaultValue,
inactive: R
}),
fieldProps: ee
};
}
//#endregion
export { p as accepts, w as useFieldBinding };
//# sourceMappingURL=useFieldBinding.js.map