@mitcsutt/kiln-forms
Version:
Kiln forms: typed React forms on TanStack Form, in component or JSON schema mode, rendered through kiln-ui
64 lines (63 loc) • 2.39 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 { defineOptionsField as o } from "../../../kit/contracts.js";
import { useRef as s, useState as c } from "react";
import { getBy as l, useSelector as u } from "@tanstack/react-form";
import { jsx as d } from "react/jsx-runtime";
import { ComboboxField as f } from "@mitcsutt/kiln-ui";
//#region src/components/fields/FormMultiSelectField/FormMultiSelectField.tsx
var p = o()(function({ warn: o, excluded: p, onBlur: m, onInputValueChange: h, options: g, loadOptions: _, reloadOn: v, minQueryLength: y, emptyMessage: b, ...x }) {
let S = a({
...x,
warn: o,
excluded: p,
accepts: i.arrayOfPrimitive,
empty: []
}), C = S.api.form, w = u(C.store, (e) => v && v.length > 0 ? JSON.stringify(v.map((t) => l(e.values, t))) : ""), T = v && v.length > 0 ? JSON.parse(w) : void 0, E = S.mode === "view", [D, O] = c(""), k = n(E ? g ?? [] : _ ?? g ?? [], {
query: D,
deps: T,
values: C.state.values,
...y === void 0 ? {} : { minQueryLength: y }
}), A = s(/* @__PURE__ */ new Map());
for (let e of k.options) A.current.set(String(e.value), e);
for (let e of g ?? []) A.current.set(String(e.value), e);
let j = r(k.options, {
retain: S.value,
known: A.current
});
if (E) {
let e = S.value.map((e) => A.current.get(String(e))?.label ?? String(e));
return /* @__PURE__ */ d(t, {
label: x.label,
children: e.length > 0 ? e.join(", ") : void 0
});
}
let M = e(C).options.messages;
return /* @__PURE__ */ d(f, {
...x,
...S.fieldProps,
ref: S.ref,
multiple: !0,
options: j.uiOptions,
filter: _ ? "none" : void 0,
loading: _ ? k.status === "loading" : void 0,
emptyMessage: k.status === "error" ? M.optionsFailed : b,
value: S.value.map(j.toUi),
onValueChange: (e) => {
S.setValue(e.map(j.fromUi).filter((e) => e !== null));
},
onInputValueChange: (e) => {
h?.(e), O(e);
},
onBlur: (e) => {
m?.(e), S.onBlur();
}
});
});
//#endregion
export { p as FormMultiSelectField };
//# sourceMappingURL=FormMultiSelectField.js.map