@konstructio/ui
Version:
A set of reusable and customizable React components built for konstruct.io
59 lines (58 loc) • 1.5 kB
JavaScript
import { jsx as G } from "react/jsx-runtime";
import { useState as e, useRef as f, useCallback as p, useEffect as T } from "react";
import { DropdownContext as L } from "./dropdown.context.js";
import { useToggle as N } from "../../../hooks/useToggle.js";
const c = 1, O = 300, U = ({
children: d,
value: C,
name: i,
highlightSearch: E = !1,
options: n,
onChange: a,
onBlur: u
}) => {
const [v, l] = e(n), F = f(E), [P, g] = N(!1), [D, I] = e(""), [S, b] = e(!0), [x, o] = e(!0), [A, s] = e(c), [_, h] = e(!1), t = f(void 0), w = p(
(r, m) => {
m?.current && (m.current.value = r), t.current && clearTimeout(t.current), h(!0), o(!0), s(c), a?.({ target: { value: r, name: i ?? "" } }), u?.(), t.current = setTimeout(() => {
h(!1);
}, O);
},
[a, i, u]
), y = p(
(r) => {
g(r), o(!0), s(c);
},
[g]
);
return T(() => {
l(n);
}, [n.length]), T(() => () => {
t.current && clearTimeout(t.current);
}, []), /* @__PURE__ */ G(
L.Provider,
{
value: {
highlightSearchEnabled: F.current,
isOpen: P,
searchTerm: D,
value: C,
canFilter: S,
canContinueFetching: x,
page: A,
options: v,
isTyping: _,
setOptions: l,
setPage: s,
setCanContinueFetching: o,
setCanFilter: b,
setSearchTerm: I,
setValue: w,
toggleOpen: y
},
children: d
}
);
};
export {
U as DropdownProvider
};