@mitcsutt/kiln-forms
Version:
Kiln forms: typed React forms on TanStack Form, in component or JSON schema mode, rendered through kiln-ui
89 lines (88 loc) • 3.03 kB
JavaScript
import { getFormRuntime as e } from "../../../runtime/formRuntime.js";
import { FieldView as t } from "../FieldView/FieldView.js";
import { useOptions as n } from "../../../hooks/useOptions.js";
import { useOptionMapping as r } from "../../../hooks/useOptionMapping.js";
import { accepts as i } from "../../../kit/accepts.js";
import { useFieldBinding as a } from "../../../hooks/useFieldBinding.js";
import { defineOptionField as o } from "../../../kit/contracts.js";
import { useMemo as s, useRef as c, useState as l } from "react";
import { getBy as u, useSelector as d } from "@tanstack/react-form";
import { jsx as f } from "react/jsx-runtime";
import { ComboboxField as p } from "@mitcsutt/kiln-ui";
//#region src/components/fields/FormComboboxField/FormComboboxField.tsx
var m = o()(function({ warn: o, excluded: m, onBlur: h, onInputValueChange: g, options: _, loadOptions: v, reloadOn: y, minQueryLength: b, creatable: x, emptyMessage: S, ...C }) {
let w = a({
...C,
warn: o,
excluded: m,
accepts: i.primitiveOrNull,
empty: null
}), T = w.api.form, E = d(T.store, (e) => y && y.length > 0 ? JSON.stringify(y.map((t) => u(e.values, t))) : ""), D = y && y.length > 0 ? JSON.parse(E) : void 0, O = w.mode === "view", [k, A] = l(""), j = n(O ? _ ?? [] : v ?? _ ?? [], {
query: k,
deps: D,
values: T.state.values,
...b === void 0 ? {} : { minQueryLength: b }
}), M = c(/* @__PURE__ */ new Map());
for (let e of j.options) M.current.set(String(e.value), e.label);
for (let e of _ ?? []) M.current.set(String(e.value), e.label);
let N = s(() => [w.value], [w.value]), P = r(j.options, { retain: N }), F = c(null);
if (O) {
let e = w.value, n = e === null ? void 0 : M.current.get(String(e)) ?? String(e);
return /* @__PURE__ */ f(t, {
label: C.label,
children: n
});
}
let I = e(T).options.messages, L = (e) => {
if (e === null) return "";
let t = P.toUi(e);
return t === "" ? x ? String(e) : "" : t;
}, R = (e) => {
let t = P.fromUi(e);
return t !== null || e === "" ? t : x ? e : null;
};
return /* @__PURE__ */ f(p, {
...C,
...w.fieldProps,
ref: w.ref,
creatable: x,
options: P.uiOptions,
filter: v ? "none" : void 0,
loading: v ? j.status === "loading" : void 0,
emptyMessage: j.status === "error" ? I.optionsFailed : S,
value: L(w.value),
onValueChange: (e) => {
if (e === null) {
F.current = null, w.setValue(null);
return;
}
let t = R(e);
F.current = t === null ? null : P.labelOf(t) ?? (x ? e : null), w.setValue(t);
},
onInputValueChange: (e) => {
if (g?.(e), F.current !== null) {
let t = F.current;
if (F.current = null, e === t) return;
A(e);
return;
}
if (!x) {
A(e);
return;
}
Promise.resolve().then(() => {
if (F.current !== null) {
let t = F.current;
if (F.current = null, e === t) return;
}
A(e);
});
},
onBlur: (e) => {
h?.(e), w.onBlur();
}
});
});
//#endregion
export { m as FormComboboxField };
//# sourceMappingURL=FormComboboxField.js.map