chakra-combobox
Version:
A combobox componente for Chakra UI.
861 lines (860 loc) • 23.1 kB
JavaScript
import * as A from "react";
import me, { memo as N, forwardRef as he, useRef as be, useCallback as xe, useEffect as ge, useState as Ce, useMemo as ve } from "react";
import { createIcon as G, Group as Re, InputElement as le, Input as Ee, Icon as re, Box as S, Flex as q, Text as X, IconButton as je, Popover as h, Portal as we, Spinner as Te, Button as de } from "@chakra-ui/react";
import { Corner as _e, Root as Ae, Scrollbar as Se, Thumb as ke, Viewport as Pe } from "@radix-ui/react-scroll-area";
import { useVirtualizer as Oe } from "@tanstack/react-virtual";
var J = { exports: {} }, B = {};
/**
* @license React
* react-jsx-runtime.production.js
*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
var ie;
function ye() {
if (ie) return B;
ie = 1;
var o = Symbol.for("react.transitional.element"), t = Symbol.for("react.fragment");
function n(i, a, l) {
var c = null;
if (l !== void 0 && (c = "" + l), a.key !== void 0 && (c = "" + a.key), "key" in a) {
l = {};
for (var m in a)
m !== "key" && (l[m] = a[m]);
} else l = a;
return a = l.ref, {
$$typeof: o,
type: i,
key: c,
ref: a !== void 0 ? a : null,
props: l
};
}
return B.Fragment = t, B.jsx = n, B.jsxs = n, B;
}
var V = {};
/**
* @license React
* react-jsx-runtime.development.js
*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
var ce;
function ze() {
return ce || (ce = 1, process.env.NODE_ENV !== "production" && function() {
function o(e) {
if (e == null) return null;
if (typeof e == "function")
return e.$$typeof === W ? null : e.displayName || e.name || null;
if (typeof e == "string") return e;
switch (e) {
case w:
return "Fragment";
case L:
return "Profiler";
case M:
return "StrictMode";
case F:
return "Suspense";
case Y:
return "SuspenseList";
case Z:
return "Activity";
}
if (typeof e == "object")
switch (typeof e.tag == "number" && console.error(
"Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."
), e.$$typeof) {
case I:
return "Portal";
case T:
return (e.displayName || "Context") + ".Provider";
case P:
return (e._context.displayName || "Context") + ".Consumer";
case _:
var s = e.render;
return e = e.displayName, e || (e = s.displayName || s.name || "", e = e !== "" ? "ForwardRef(" + e + ")" : "ForwardRef"), e;
case b:
return s = e.displayName || null, s !== null ? s : o(e.type) || "Memo";
case U:
s = e._payload, e = e._init;
try {
return o(e(s));
} catch {
}
}
return null;
}
function t(e) {
return "" + e;
}
function n(e) {
try {
t(e);
var s = !1;
} catch {
s = !0;
}
if (s) {
s = console;
var u = s.error, f = typeof Symbol == "function" && Symbol.toStringTag && e[Symbol.toStringTag] || e.constructor.name || "Object";
return u.call(
s,
"The provided key is an unsupported type %s. This value must be coerced to a string before using it here.",
f
), t(e);
}
}
function i(e) {
if (e === w) return "<>";
if (typeof e == "object" && e !== null && e.$$typeof === U)
return "<...>";
try {
var s = o(e);
return s ? "<" + s + ">" : "<...>";
} catch {
return "<...>";
}
}
function a() {
var e = O.A;
return e === null ? null : e.getOwner();
}
function l() {
return Error("react-stack-top-frame");
}
function c(e) {
if (D.call(e, "key")) {
var s = Object.getOwnPropertyDescriptor(e, "key").get;
if (s && s.isReactWarning) return !1;
}
return e.key !== void 0;
}
function m(e, s) {
function u() {
te || (te = !0, console.error(
"%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://react.dev/link/special-props)",
s
));
}
u.isReactWarning = !0, Object.defineProperty(e, "key", {
get: u,
configurable: !0
});
}
function g() {
var e = o(this.type);
return oe[e] || (oe[e] = !0, console.error(
"Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release."
)), e = this.props.ref, e !== void 0 ? e : null;
}
function C(e, s, u, f, j, R, Q, K) {
return u = R.ref, e = {
$$typeof: k,
type: e,
key: s,
props: R,
_owner: j
}, (u !== void 0 ? u : null) !== null ? Object.defineProperty(e, "ref", {
enumerable: !1,
get: g
}) : Object.defineProperty(e, "ref", { enumerable: !1, value: null }), e._store = {}, Object.defineProperty(e._store, "validated", {
configurable: !1,
enumerable: !1,
writable: !0,
value: 0
}), Object.defineProperty(e, "_debugInfo", {
configurable: !1,
enumerable: !1,
writable: !0,
value: null
}), Object.defineProperty(e, "_debugStack", {
configurable: !1,
enumerable: !1,
writable: !0,
value: Q
}), Object.defineProperty(e, "_debugTask", {
configurable: !1,
enumerable: !1,
writable: !0,
value: K
}), Object.freeze && (Object.freeze(e.props), Object.freeze(e)), e;
}
function v(e, s, u, f, j, R, Q, K) {
var p = s.children;
if (p !== void 0)
if (f)
if (y(p)) {
for (f = 0; f < p.length; f++)
E(p[f]);
Object.freeze && Object.freeze(p);
} else
console.error(
"React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead."
);
else E(p);
if (D.call(s, "key")) {
p = o(e);
var z = Object.keys(s).filter(function(pe) {
return pe !== "key";
});
f = 0 < z.length ? "{key: someKey, " + z.join(": ..., ") + ": ...}" : "{key: someKey}", ae[p + f] || (z = 0 < z.length ? "{" + z.join(": ..., ") + ": ...}" : "{}", console.error(
`A props object containing a "key" prop is being spread into JSX:
let props = %s;
<%s {...props} />
React keys must be passed directly to JSX without using spread:
let props = %s;
<%s key={someKey} {...props} />`,
f,
p,
z,
p
), ae[p + f] = !0);
}
if (p = null, u !== void 0 && (n(u), p = "" + u), c(s) && (n(s.key), p = "" + s.key), "key" in s) {
u = {};
for (var ee in s)
ee !== "key" && (u[ee] = s[ee]);
} else u = s;
return p && m(
u,
typeof e == "function" ? e.displayName || e.name || "Unknown" : e
), C(
e,
p,
R,
j,
a(),
u,
Q,
K
);
}
function E(e) {
typeof e == "object" && e !== null && e.$$typeof === k && e._store && (e._store.validated = 1);
}
var d = me, k = Symbol.for("react.transitional.element"), I = Symbol.for("react.portal"), w = Symbol.for("react.fragment"), M = Symbol.for("react.strict_mode"), L = Symbol.for("react.profiler"), P = Symbol.for("react.consumer"), T = Symbol.for("react.context"), _ = Symbol.for("react.forward_ref"), F = Symbol.for("react.suspense"), Y = Symbol.for("react.suspense_list"), b = Symbol.for("react.memo"), U = Symbol.for("react.lazy"), Z = Symbol.for("react.activity"), W = Symbol.for("react.client.reference"), O = d.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, D = Object.prototype.hasOwnProperty, y = Array.isArray, $ = console.createTask ? console.createTask : function() {
return null;
};
d = {
"react-stack-bottom-frame": function(e) {
return e();
}
};
var te, oe = {}, ne = d["react-stack-bottom-frame"].bind(
d,
l
)(), se = $(i(l)), ae = {};
V.Fragment = w, V.jsx = function(e, s, u, f, j) {
var R = 1e4 > O.recentlyCreatedOwnerStacks++;
return v(
e,
s,
u,
!1,
f,
j,
R ? Error("react-stack-top-frame") : ne,
R ? $(i(e)) : se
);
}, V.jsxs = function(e, s, u, f, j) {
var R = 1e4 > O.recentlyCreatedOwnerStacks++;
return v(
e,
s,
u,
!0,
f,
j,
R ? Error("react-stack-top-frame") : ne,
R ? $(i(e)) : se
);
};
}()), V;
}
var ue;
function Ne() {
return ue || (ue = 1, process.env.NODE_ENV === "production" ? J.exports = ye() : J.exports = ze()), J.exports;
}
var r = Ne();
const Ie = G({
displayName: "MagnifyingGlass",
viewBox: "0 0 256 256",
d: "M229.66,218.34l-50.07-50.06a88.11,88.11,0,1,0-11.31,11.31l50.06,50.07a8,8,0,0,0,11.32-11.32ZM40,112a72,72,0,1,1,72,72A72.08,72.08,0,0,1,40,112Z",
defaultProps: {
fill: "currentColor",
height: "32",
width: "32"
}
}), Me = G({
displayName: "CaretDown",
viewBox: "0 0 256 256",
d: "M213.66,101.66l-80,80a8,8,0,0,1-11.32,0l-80-80A8,8,0,0,1,53.66,90.34L128,164.69l74.34-74.35a8,8,0,0,1,11.32,11.32Z",
defaultProps: {
fill: "currentColor",
height: "32",
width: "32"
}
}), fe = G({
displayName: "LuX",
viewBox: "0 0 24 24",
d: "M18 6 L6 18 M6 6 L18 18",
defaultProps: {
width: "24",
height: "24",
stroke: "currentColor",
strokeWidth: "2"
}
});
G({
displayName: "LuCheck",
viewBox: "0 0 24 24",
d: "M20 6 L9 17 L4 12",
defaultProps: {
width: "24",
height: "24",
stroke: "currentColor",
strokeWidth: "2"
}
});
G({
displayName: "LuChevronRight",
viewBox: "0 0 24 24",
d: "M9 18 L15 12 L9 6",
defaultProps: {
width: "24",
height: "24",
stroke: "currentColor",
strokeWidth: "2"
}
});
const Le = A.forwardRef(
function(t, n) {
const {
startElement: i,
startElementProps: a,
endElement: l,
endElementProps: c,
children: m,
startOffset: g = "6px",
endOffset: C = "6px",
...v
} = t, E = A.Children.only(m);
return /* @__PURE__ */ r.jsxs(Re, { ref: n, ...v, children: [
i && /* @__PURE__ */ r.jsx(le, { pointerEvents: "none", ...a, children: i }),
A.cloneElement(E, {
...i && {
ps: `calc(var(--input-height) - ${g})`
},
...l && { pe: `calc(var(--input-height) - ${C})` },
...m.props
}),
l && /* @__PURE__ */ r.jsx(le, { placement: "end", ...c, children: l })
] });
}
), x = (o, t) => t ? t(o) : o, Fe = N(({ inputCss: o, ...t }) => {
const n = x(
{
w: "full",
borderRadius: "0",
borderWidth: "0",
_active: {
borderColor: "transparent",
outline: "none"
},
_focus: {
borderColor: "transparent",
outline: "none",
boxShadow: "none"
},
h: "45px",
fontSize: "md"
},
o
);
return /* @__PURE__ */ r.jsx(
Le,
{
startElement: /* @__PURE__ */ r.jsx(
re,
{
as: Ie,
boxSize: 5,
css: {
h: n.h
}
}
),
children: /* @__PURE__ */ r.jsx(Ee, { ps: "40px", ...t, css: n })
}
);
}), Ye = ({
...o
}) => /* @__PURE__ */ r.jsx(S, { as: _e, ...o }), We = ({
children: o,
css: t,
...n
}) => /* @__PURE__ */ r.jsx(
S,
{
as: Ae,
maxH: "225px",
h: "full",
overflow: "hidden",
...n,
css: {
"--scrollbar-size": "10px",
...t
},
children: o
}
), De = ({ children: o, ...t }) => /* @__PURE__ */ r.jsx(S, { as: Se, ...t, userSelect: "none", display: "flex", children: o }), $e = ({
css: o,
...t
}) => /* @__PURE__ */ r.jsx(
S,
{
as: ke,
...t,
flex: "1",
css: {
...o,
borderRadius: "var(--scrollbar-size)"
}
}
), Be = he((o, t) => /* @__PURE__ */ r.jsx(
S,
{
as: Pe,
ref: t,
w: "100%",
maxH: "225px",
h: "full",
borderRadius: "inherit",
px: "calc(var(--scrollbar-size) + 4px)",
...o,
children: o.children
}
)), H = {
Root: We,
Viewport: Be,
Scrollbar: De,
Thumb: $e,
Corner: Ye
};
function Ve(o, t) {
let n = 0;
return function(...i) {
const a = Date.now();
a - n >= t && (n = a, o(...i));
};
}
const He = ({
onScrollEnd: o,
isLoading: t,
isFetchingNextPage: n,
hasNextPage: i
}) => {
const a = be(null), l = xe(
Ve(() => {
const c = a.current;
c && c.scrollTop + c.clientHeight >= c.scrollHeight - 10 && i && !t && !n && o();
}, 0),
[t, n, i, o]
);
return ge(() => {
const c = a.current;
if (c)
return c.addEventListener("scroll", l), () => {
c.removeEventListener("scroll", l);
};
}, [l]), a;
};
function Ge({
options: o,
value: t,
onSelect: n,
getOptionLabel: i,
getOptionValue: a,
isLoading: l,
isFetchingNextPage: c,
fetchNextPage: m,
hasNextPage: g,
loadingElement: C = "Loading...",
emptyElement: v = "No results found",
scrollAreaCss: E,
optionCss: d,
scrollbarCss: k,
scrollThumbCss: I,
scrollCornerCss: w,
emptyMessageCss: M,
loadingMessageCss: L
}) {
const P = He({
onScrollEnd: m,
isLoading: l,
isFetchingNextPage: c,
hasNextPage: g
}), T = Oe({
count: o.length,
getScrollElement: () => P.current,
estimateSize: () => 40,
gap: 4,
paddingStart: 4,
paddingEnd: 4,
overscan: 5
}), _ = T.getVirtualItems(), F = x(
{
p: "2px",
bg: "whiteAlpha.300",
transition: "background 160ms ease-out",
_hover: {
bg: "whiteAlpha.400"
},
touchAction: "none",
'&[data-orientation="vertical"]': {
width: "var(--scrollbar-size)"
},
'&[data-orientation="horizontal"]': {
flexDirection: "column",
height: "var(--scrollbar-size)"
}
},
k
), Y = x(
{
bg: "gray.200",
position: "relative",
_before: {
content: '""',
position: "absolute",
top: "50%",
left: "50%",
transform: "translate(-50%, -50%)",
width: "100%",
height: "100%",
minWidth: "44px",
minHeight: "44px"
}
},
I
), b = x(
{
bg: "blackAlpha.500"
},
w
), U = x(
{
py: 2,
px: 2,
fontSize: "md"
},
L
), Z = x(
{
py: 2,
px: 2,
fontSize: "md"
},
M
), W = x({}, E), O = W.maxH || W.maxHeight;
return /* @__PURE__ */ r.jsxs(H.Root, { css: W, children: [
/* @__PURE__ */ r.jsxs(
H.Viewport,
{
ref: P,
...O && { maxH: O },
children: [
_.length > 0 && /* @__PURE__ */ r.jsx(
S,
{
h: `${T.getTotalSize()}px`,
w: "full",
position: "relative",
children: _.map((D) => {
const y = o[D.index], $ = t && a(t) === a(y) || !1;
return /* @__PURE__ */ r.jsx(
qe,
{
option: y,
isSelected: $,
onSelect: n,
label: i(y),
virtualRowStart: D.start,
css: d
},
a(y)
);
})
}
),
_.length === 0 && !l && !c && /* @__PURE__ */ r.jsx(r.Fragment, { children: typeof v == "string" ? /* @__PURE__ */ r.jsx(q, { css: Z, children: /* @__PURE__ */ r.jsx(X, { children: v }) }) : v }),
l && /* @__PURE__ */ r.jsx(r.Fragment, { children: typeof C == "string" ? /* @__PURE__ */ r.jsx(q, { css: U, children: /* @__PURE__ */ r.jsx(X, { children: C }) }) : C })
]
}
),
/* @__PURE__ */ r.jsx(
H.Scrollbar,
{
orientation: "vertical",
css: F,
children: /* @__PURE__ */ r.jsx(H.Thumb, { css: Y })
}
),
/* @__PURE__ */ r.jsx(H.Corner, { css: b })
] });
}
const Ue = N(Ge);
function Je({
option: o,
isSelected: t,
onSelect: n,
label: i,
virtualRowStart: a,
css: l
}) {
const c = x(
{
py: 2.5,
px: 2,
position: "absolute",
top: 0,
left: 0,
width: "100%",
transform: `translateY(${a}px)`,
_hover: {
bg: "gray.100",
color: "gray.900"
},
cursor: "pointer",
borderRadius: "4px",
transition: "all 0.2s ease-in-out",
bg: "transparent",
color: "inherit",
_selected: {
bg: "gray.100",
color: "gray.900"
},
backfaceVisibility: "hidden",
fontSize: "md"
},
l
);
return /* @__PURE__ */ r.jsx(
X,
{
as: "option",
...t && { "data-selected": !0 },
onClick: () => n(o),
css: c,
children: i
}
);
}
const qe = N(Je), Xe = N(
({ customIcon: o, dropdownIndicatorCss: t, ...n }) => {
const i = t ? t({}) : {};
return /* @__PURE__ */ r.jsx(
re,
{
as: o || Me,
boxSize: 4,
className: "dropdown-indicator",
...n,
css: i
}
);
}
);
function Ze(o, t) {
let n;
return function(...i) {
clearTimeout(n), n = setTimeout(() => o(...i), t);
};
}
const Qe = A.forwardRef(function(t, n) {
return /* @__PURE__ */ r.jsx(je, { variant: "ghost", "aria-label": "Close", ref: n, ...t, children: t.children ?? /* @__PURE__ */ r.jsx(fe, {}) });
}), Ke = A.forwardRef(function(t, n) {
const { portalled: i = !0, portalRef: a, ...l } = t;
return /* @__PURE__ */ r.jsx(we, { disabled: !i, container: a, children: /* @__PURE__ */ r.jsx(h.Positioner, { children: /* @__PURE__ */ r.jsx(h.Content, { ref: n, ...l }) }) });
});
A.forwardRef(function(t, n) {
return /* @__PURE__ */ r.jsx(h.Arrow, { ...t, ref: n, children: /* @__PURE__ */ r.jsx(h.ArrowTip, {}) });
});
A.forwardRef(function(t, n) {
return /* @__PURE__ */ r.jsx(
h.CloseTrigger,
{
position: "absolute",
top: "1",
insetEnd: "1",
...t,
asChild: !0,
ref: n,
children: /* @__PURE__ */ r.jsx(Qe, { size: "sm" })
}
);
});
h.Title;
h.Description;
h.Footer;
h.Header;
const er = h.Root;
h.Body;
const rr = h.Trigger;
function tr({
getOptionLabel: o,
getOptionValue: t,
isLoading: n,
isFetchingNextPage: i,
hasNextPage: a,
onSearchChange: l,
fetchNextPage: c,
options: m,
value: g,
onSelect: C,
placeholder: v,
dropdownIndicator: E,
chakraStyles: d,
loadingElement: k,
emptyElement: I,
isClearable: w = !1,
searchInputPlaceholder: M = "Search...",
closeOnSelect: L = !0
}) {
const [P, T] = Ce(!1), _ = ve(
() => Ze((b) => {
l(b.target.value);
}, 500),
// eslint-disable-next-line react-hooks/exhaustive-deps
[]
), F = x(
{
p: 0,
overflow: "hidden"
},
d?.menuList
), Y = x(
{
p: 0,
minW: 6,
h: 6
},
d?.clearButton
);
return /* @__PURE__ */ r.jsxs(
er,
{
open: P,
onOpenChange: (b) => T(b.open),
positioning: { sameWidth: !0 },
lazyMount: !0,
unmountOnExit: !0,
children: [
/* @__PURE__ */ r.jsxs(or, { controlCss: d?.control, children: [
/* @__PURE__ */ r.jsx(X, { lineClamp: 1, children: g && o(g) || v }),
/* @__PURE__ */ r.jsxs(q, { gap: 2, align: "center", children: [
(n || i) && /* @__PURE__ */ r.jsx(Te, { size: "sm" }),
/* @__PURE__ */ r.jsxs(q, { gap: 1, align: "center", children: [
w && g && /* @__PURE__ */ r.jsx(
de,
{
size: "xs",
variant: "ghost",
css: Y,
onClick: (b) => {
b.stopPropagation(), C(void 0);
},
children: /* @__PURE__ */ r.jsx(re, { boxSize: 4, as: fe })
}
),
/* @__PURE__ */ r.jsx(
Xe,
{
customIcon: E,
dropdownIndicatorCss: d?.dropdownIndicator
}
)
] })
] })
] }),
/* @__PURE__ */ r.jsxs(Ke, { width: "auto", css: F, children: [
/* @__PURE__ */ r.jsx(S, { px: "10px", borderBottomWidth: "1px", borderColor: "inherit", children: /* @__PURE__ */ r.jsx(
Fe,
{
placeholder: M,
onChange: _,
inputCss: d?.input
}
) }),
/* @__PURE__ */ r.jsx(
Ue,
{
options: m,
value: g,
onSelect: (b) => {
C(b), L && T(!1);
},
getOptionLabel: o,
getOptionValue: t,
isLoading: n,
isFetchingNextPage: i,
fetchNextPage: c,
hasNextPage: a,
loadingElement: k,
emptyElement: I,
optionCss: d?.option,
scrollAreaCss: d?.scrollArea,
scrollbarCss: d?.scrollbar,
scrollThumbCss: d?.scrollThumb,
scrollCornerCss: d?.scrollCorner,
loadingMessageCss: d?.loadingMessage,
emptyMessageCss: d?.emptyMessage
}
)
] })
]
}
);
}
const ir = N(tr), or = N(
({ controlCss: o, ...t }) => {
const n = x(
{
"&[aria-expanded='true']": {
"& .dropdown-indicator": {
transform: "rotate(180deg)"
}
},
"& .dropdown-indicator": {
transition: "transform 0.1s ease-in-out"
},
justifyContent: "space-between",
textAlign: "left",
borderWidth: "1px",
borderColor: "black",
borderRadius: "md",
fontSize: "md",
color: "black",
bg: "transparent",
_hover: {
bg: "transparent"
},
_active: {
bg: "transparent"
}
},
o
);
return /* @__PURE__ */ r.jsx(rr, { asChild: !0, children: /* @__PURE__ */ r.jsx(de, { ...t, css: n, children: t.children }) });
}
);
export {
ir as AsyncCombobox
};