@uva-glass/component-library
Version:
React components UvA
45 lines (44 loc) • 1.32 kB
JavaScript
import { jsx as g } from "react/jsx-runtime";
import { createContext as y, useContext as C, useState as c, useRef as P, useId as p, useEffect as D } from "react";
const v = y({}), h = ({ options: e, defaultValue: s, children: d }) => {
const [l, x] = c({ value: "", label: "" }), [b, o] = c(!1), [f, i] = c(-1), [I, u] = c(!1), S = P(p()), m = () => {
o((t) => !t), u(!1);
}, n = (t) => e.find((a) => a.value === t) || {
value: -1,
label: "",
selectedLabel: "-"
}, r = (t, a = !1) => {
const w = e.findIndex((L) => L.value === t.value);
i(w), x(t), o(!1), u(!a);
};
return s !== -1 && !l.value && r(n(s), !0), D(() => {
n(l.value || s).label !== l.label && r(n(l.value || s));
}, [e, s]), /* @__PURE__ */ g(
v.Provider,
{
value: {
options: e,
selectedValue: l,
setSelectedValue: r,
isOpen: b,
setIsOpen: o,
toggleListbox: m,
getValue: n,
activeIndex: f,
setActiveIndex: i,
listboxId: S.current,
isDirty: I
},
children: d
}
);
}, j = () => {
const e = C(v);
if (e === void 0) throw new Error("useSelect can only be used in an SelectProvider");
return e;
};
export {
h as SelectProvider,
j as useSelect
};
//# sourceMappingURL=SelectProvider.js.map