@mitcsutt/kiln-forms
Version:
Kiln forms: typed React forms on TanStack Form, in component or JSON schema mode, rendered through kiln-ui
66 lines (65 loc) • 1.76 kB
JavaScript
import { FieldView as e } from "../FieldView/FieldView.js";
import { useOptionMapping as t } from "../../../hooks/useOptionMapping.js";
import { accepts as n } from "../../../kit/accepts.js";
import { useFieldBinding as r } from "../../../hooks/useFieldBinding.js";
import { defineOptionField as i } from "../../../kit/contracts.js";
import { useRef as a } from "react";
import { jsx as o } from "react/jsx-runtime";
import { SelectField as s } from "@mitcsutt/kiln-ui";
//#region src/components/fields/FormSelectField/FormSelectField.tsx
function c(e) {
let t = [], n = [];
for (let r of e) {
let e = {
value: r.value,
label: r.label
};
if (r.disabled !== void 0 && (e.disabled = r.disabled), r.group === void 0) {
t.push(e);
continue;
}
let i = n.find((e) => e.label === r.group);
i || (i = {
label: r.group,
options: []
}, n.push(i)), i.options.push(e);
}
return {
flat: t,
groups: n
};
}
var l = i()(function({ warn: i, excluded: l, onBlur: u, onOpenChange: d, options: f, emptyOption: p, ...m }) {
let h = r({
...m,
warn: i,
excluded: l,
accepts: n.primitiveOrNull,
empty: null
}), g = t(f, { emptyOption: p }), _ = a(!1);
if (h.mode === "view") return /* @__PURE__ */ o(e, {
label: m.label,
children: g.labelOf(h.value)
});
let { flat: v, groups: y } = c(g.uiOptions);
return /* @__PURE__ */ o(s, {
...m,
...h.fieldProps,
ref: h.ref,
options: v,
groups: y.length > 0 ? y : void 0,
value: g.toUi(h.value),
onValueChange: (e) => {
h.setValue(g.fromUi(e));
},
onOpenChange: (e) => {
_.current = e, d?.(e);
},
onBlur: (e) => {
u?.(e), _.current || h.onBlur();
}
});
});
//#endregion
export { l as FormSelectField };
//# sourceMappingURL=FormSelectField.js.map