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