UNPKG

chakra-combobox

Version:
785 lines (784 loc) 21.8 kB
import * as ee from "react"; import ve, { memo as q, useRef as le, useEffect as ie, useCallback as Ee, useMemo as he, createElement as Re, useState as ce } from "react"; import { createIcon as F, Icon as ne, useLiveRef as ge, Group as Te, InputElement as ue, Input as je, createListCollection as we, Listbox as W, Box as de, Show as re, Flex as J, Text as te, useListboxItemContext as _e, Checkmark as Ce, Popover as G, Spinner as ke, Button as xe, Portal as Ie } from "@chakra-ui/react"; import { useVirtualizer as Se } from "@tanstack/react-virtual"; var U = { exports: {} }, $ = {}; /** * @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 fe; function ye() { if (fe) return $; fe = 1; var n = Symbol.for("react.transitional.element"), s = Symbol.for("react.fragment"); function a(l, i, c) { var d = null; if (c !== void 0 && (d = "" + c), i.key !== void 0 && (d = "" + i.key), "key" in i) { c = {}; for (var f in i) f !== "key" && (c[f] = i[f]); } else c = i; return i = c.ref, { $$typeof: n, type: l, key: d, ref: i !== void 0 ? i : null, props: c }; } return $.Fragment = s, $.jsx = a, $.jsxs = a, $; } var D = {}; /** * @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 me; function Ae() { return me || (me = 1, process.env.NODE_ENV !== "production" && function() { function n(e) { if (e == null) return null; if (typeof e == "function") return e.$$typeof === X ? null : e.displayName || e.name || null; if (typeof e == "string") return e; switch (e) { case g: return "Fragment"; case y: return "Profiler"; case S: return "StrictMode"; case z: return "Suspense"; case M: return "SuspenseList"; case I: 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 R: return "Portal"; case N: return (e.displayName || "Context") + ".Provider"; case C: return (e._context.displayName || "Context") + ".Consumer"; case w: var r = e.render; return e = e.displayName, e || (e = r.displayName || r.name || "", e = e !== "" ? "ForwardRef(" + e + ")" : "ForwardRef"), e; case L: return r = e.displayName || null, r !== null ? r : n(e.type) || "Memo"; case v: r = e._payload, e = e._init; try { return n(e(r)); } catch { } } return null; } function s(e) { return "" + e; } function a(e) { try { s(e); var r = !1; } catch { r = !0; } if (r) { r = console; var u = r.error, p = typeof Symbol == "function" && Symbol.toStringTag && e[Symbol.toStringTag] || e.constructor.name || "Object"; return u.call( r, "The provided key is an unsupported type %s. This value must be coerced to a string before using it here.", p ), s(e); } } function l(e) { if (e === g) return "<>"; if (typeof e == "object" && e !== null && e.$$typeof === v) return "<...>"; try { var r = n(e); return r ? "<" + r + ">" : "<...>"; } catch { return "<...>"; } } function i() { var e = Y.A; return e === null ? null : e.getOwner(); } function c() { return Error("react-stack-top-frame"); } function d(e) { if (V.call(e, "key")) { var r = Object.getOwnPropertyDescriptor(e, "key").get; if (r && r.isReactWarning) return !1; } return e.key !== void 0; } function f(e, r) { function u() { x || (x = !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)", r )); } u.isReactWarning = !0, Object.defineProperty(e, "key", { get: u, configurable: !0 }); } function m() { var e = n(this.type); return B[e] || (B[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 E(e, r, u, p, k, _, Z, Q) { return u = _.ref, e = { $$typeof: o, type: e, key: r, props: _, _owner: k }, (u !== void 0 ? u : null) !== null ? Object.defineProperty(e, "ref", { enumerable: !1, get: m }) : 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: Z }), Object.defineProperty(e, "_debugTask", { configurable: !1, enumerable: !1, writable: !0, value: Q }), Object.freeze && (Object.freeze(e.props), Object.freeze(e)), e; } function T(e, r, u, p, k, _, Z, Q) { var h = r.children; if (h !== void 0) if (p) if (H(h)) { for (p = 0; p < h.length; p++) j(h[p]); Object.freeze && Object.freeze(h); } 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 j(h); if (V.call(r, "key")) { h = n(e); var O = Object.keys(r).filter(function(be) { return be !== "key"; }); p = 0 < O.length ? "{key: someKey, " + O.join(": ..., ") + ": ...}" : "{key: someKey}", ae[h + p] || (O = 0 < O.length ? "{" + O.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} />`, p, h, O, h ), ae[h + p] = !0); } if (h = null, u !== void 0 && (a(u), h = "" + u), d(r) && (a(r.key), h = "" + r.key), "key" in r) { u = {}; for (var K in r) K !== "key" && (u[K] = r[K]); } else u = r; return h && f( u, typeof e == "function" ? e.displayName || e.name || "Unknown" : e ), E( e, h, _, k, i(), u, Z, Q ); } function j(e) { typeof e == "object" && e !== null && e.$$typeof === o && e._store && (e._store.validated = 1); } var b = ve, o = Symbol.for("react.transitional.element"), R = Symbol.for("react.portal"), g = Symbol.for("react.fragment"), S = Symbol.for("react.strict_mode"), y = Symbol.for("react.profiler"), C = Symbol.for("react.consumer"), N = Symbol.for("react.context"), w = Symbol.for("react.forward_ref"), z = Symbol.for("react.suspense"), M = Symbol.for("react.suspense_list"), L = Symbol.for("react.memo"), v = Symbol.for("react.lazy"), I = Symbol.for("react.activity"), X = Symbol.for("react.client.reference"), Y = b.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, V = Object.prototype.hasOwnProperty, H = Array.isArray, A = console.createTask ? console.createTask : function() { return null; }; b = { react_stack_bottom_frame: function(e) { return e(); } }; var x, B = {}, oe = b.react_stack_bottom_frame.bind( b, c )(), se = A(l(c)), ae = {}; D.Fragment = g, D.jsx = function(e, r, u, p, k) { var _ = 1e4 > Y.recentlyCreatedOwnerStacks++; return T( e, r, u, !1, p, k, _ ? Error("react-stack-top-frame") : oe, _ ? A(l(e)) : se ); }, D.jsxs = function(e, r, u, p, k) { var _ = 1e4 > Y.recentlyCreatedOwnerStacks++; return T( e, r, u, !0, p, k, _ ? Error("react-stack-top-frame") : oe, _ ? A(l(e)) : se ); }; }()), D; } var pe; function Oe() { return pe || (pe = 1, process.env.NODE_ENV === "production" ? U.exports = ye() : U.exports = Ae()), U.exports; } var t = Oe(); const Pe = F({ 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" } }), Ne = F({ 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" } }), ze = F({ 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" } }); F({ displayName: "LuCheck", viewBox: "0 0 24 24", d: "M20 6 L9 17 L4 12", defaultProps: { width: "24", height: "24", stroke: "currentColor", strokeWidth: "2" } }); F({ displayName: "LuChevronRight", viewBox: "0 0 24 24", d: "M9 18 L15 12 L9 6", defaultProps: { width: "24", height: "24", stroke: "currentColor", strokeWidth: "2" } }); const Me = q( ({ customIcon: n, dropdownIndicatorCss: s, ...a }) => { const l = s ? s({}) : {}; return /* @__PURE__ */ t.jsx( ne, { as: n || Ne, boxSize: 4, className: "dropdown-indicator", ...a, css: l } ); } ); function Le(n, s) { let a; return function(...l) { clearTimeout(a), a = setTimeout(() => n(...l), s); }; } const P = (n, s) => s ? s(n) : n; function Ye(n, s) { let a = 0; return function(...l) { const i = Date.now(); i - a >= s && (a = i, n(...l)); }; } function We({ count: n, onScrollEnd: s, isLoading: a, isFetchingNextPage: l, hasNextPage: i }) { const c = le(null), d = le(null), f = () => { d.current && (clearTimeout(d.current), d.current = null); }, m = Se({ count: n, getScrollElement: () => c.current, estimateSize: () => 32, overscan: 10, gap: 4 }), E = ge(m), T = (o) => { f(); const R = () => { const g = E.current; if (g?.getVirtualItems()?.find( (C) => C.index === o.index )) { o.getElement()?.scrollIntoView({ block: "end" }), f(); return; } g?.scrollToIndex(o.index), o.immediate || (d.current = setTimeout(R, 16)); }; R(); }; ie(() => f, []); const j = m.getTotalSize(), b = Ee( Ye(() => { const o = c.current; o && o.scrollTop + o.clientHeight >= o.scrollHeight - 10 && i && !a && !l && s(); }, 0), [a, l, i, s] ); return ie(() => { const o = c.current; if (o) return o.addEventListener("scroll", b), () => { o.removeEventListener("scroll", b); }; }, [b]), { scrollRef: c, scrollToIndexFn: T, totalSize: j, virtualItems: m.getVirtualItems(), getViewportProps(o = {}) { return { style: { height: "200px", position: "relative", width: "100%", ...o.style }, ...o }; }, getItemProps(o) { const { virtualItem: R, ...g } = o; return { ...g, "aria-posinset": R.index + 1, "aria-setsize": j, style: { position: "absolute", top: 0, left: 0, width: "100%", whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis", ...g.style, height: `${R.size}px`, transform: `translateY(${R.start}px)` } }; } }; } const $e = ee.forwardRef( function(s, a) { const { startElement: l, startElementProps: i, endElement: c, endElementProps: d, children: f, startOffset: m = "6px", endOffset: E = "6px", ...T } = s, j = ee.Children.only(f); return /* @__PURE__ */ t.jsxs(Te, { ref: a, ...T, children: [ l && /* @__PURE__ */ t.jsx(ue, { pointerEvents: "none", ...i, children: l }), ee.cloneElement(j, { ...l && { ps: `calc(var(--input-height) - ${m})` }, ...c && { pe: `calc(var(--input-height) - ${E})` }, ...f.props }), c && /* @__PURE__ */ t.jsx(ue, { placement: "end", ...d, children: c }) ] }); } ), De = q(({ inputCss: n, ...s }) => { const a = P( { w: "full", borderRadius: "0", borderWidth: "0", _active: { borderColor: "transparent", outline: "none" }, _focus: { borderColor: "transparent", outline: "none", boxShadow: "none" }, h: "45px", fontSize: "md" }, n ); return /* @__PURE__ */ t.jsx( $e, { w: "full", startElement: /* @__PURE__ */ t.jsx( ne, { as: Pe, boxSize: 5, css: { h: a.h } } ), children: /* @__PURE__ */ t.jsx(je, { ps: "40px", ...s, css: a }) } ); }); function Fe({ options: n, searchInputPlaceholder: s, value: a, isLoading: l, isFetchingNextPage: i, hasNextPage: c, loadingElement: d, emptyElement: f, listboxProps: m, withCheckmark: E = !1, withIndicator: T = !0, searchValue: j, getOptionLabel: b, getOptionValue: o, fetchNextPage: R, onSelect: g, onInputChange: S, inputCss: y, emptyMessageCss: C, loadingMessageCss: N }) { const w = We({ count: n.length, onScrollEnd: R, isLoading: l, isFetchingNextPage: i, hasNextPage: c }), z = he( () => we({ items: n, itemToString: (v) => b(v), itemToValue: (v) => o(v) }), [n, o, b] ), M = P( { py: 2, px: 2, fontSize: "md" }, C ), L = P( { py: 2, px: 2, fontSize: "md" }, N ); return /* @__PURE__ */ t.jsxs( W.Root, { ...m?.Root, maxW: "full", collection: z, scrollToIndexFn: w.scrollToIndexFn, gap: "0", value: a, onValueChange: (v) => { g(v.value); }, children: [ /* @__PURE__ */ t.jsx(de, { px: "10px", borderBottomWidth: "1px", borderColor: "inherit", w: "full", children: /* @__PURE__ */ t.jsx( De, { placeholder: s, onChange: S, inputCss: y, value: j } ) }), /* @__PURE__ */ t.jsx(re, { when: w.totalSize > 0, children: /* @__PURE__ */ t.jsx( W.Content, { ref: w.scrollRef, border: "none", ...m?.Content, children: /* @__PURE__ */ t.jsx(de, { ...w.getViewportProps(), children: w.virtualItems.map((v) => { const I = n[v.index]; return /* @__PURE__ */ Re( W.Item, { ...m?.Item, key: o(I), item: I, ...w.getItemProps({ virtualItem: v }) }, E && /* @__PURE__ */ t.jsx(Ve, { ...m?.Checkmark }), /* @__PURE__ */ t.jsx(W.ItemText, { ...m?.ItemText, children: b(I) }), T && !E && /* @__PURE__ */ t.jsx(W.ItemIndicator, { ...m?.ItemIndicator }) ); }) }) } ) }), /* @__PURE__ */ t.jsx(re, { when: w.totalSize === 0 && !l && !i, children: typeof f == "string" ? /* @__PURE__ */ t.jsx(J, { css: M, children: /* @__PURE__ */ t.jsx(te, { children: f }) }) : f }), /* @__PURE__ */ t.jsx(re, { when: l, children: typeof d == "string" ? /* @__PURE__ */ t.jsx(J, { css: L, children: /* @__PURE__ */ t.jsx(te, { children: d }) }) : d }) ] } ); } const Ve = (n) => { const s = _e(); return /* @__PURE__ */ t.jsx( Ce, { filled: !0, size: "sm", checked: s.selected, disabled: s.disabled, ...n } ); }; function Be({ getOptionLabel: n, getOptionValue: s, isLoading: a, isFetchingNextPage: l, hasNextPage: i, onBlur: c, onSearchChange: d, fetchNextPage: f, options: m, value: E, onSelect: T, placeholder: j, dropdownIndicator: b, chakraStyles: o, isClearable: R = !1, loadingElement: g = "Loading...", emptyElement: S = "No results found", searchInputPlaceholder: y = "Search...", closeOnSelect: C = !1, insideDialog: N = !1, listboxProps: w, withIndicator: z, withCheckmark: M }) { const [L, v] = ce(!1), [I, X] = ce(""), Y = he( () => Le((x) => { d(x.target.value); }, 500), // eslint-disable-next-line react-hooks/exhaustive-deps [] ), V = P( { p: 0, overflow: "hidden" }, o?.popoverContentCss ), H = P( { p: 0, minW: 6, h: 6 }, o?.clearButton ), A = E?.map( (x) => n( m.find((B) => s(B) === x) || {} ) ) || []; return /* @__PURE__ */ t.jsxs( G.Root, { open: L, onOpenChange: (x) => v(x.open), positioning: { sameWidth: !0 }, lazyMount: !0, unmountOnExit: !0, onExitComplete: c, children: [ /* @__PURE__ */ t.jsx(G.Trigger, { asChild: !0, children: /* @__PURE__ */ t.jsxs(Ge, { controlCss: o?.control, children: [ /* @__PURE__ */ t.jsx(te, { lineClamp: 1, children: A.length > 0 && A.join(", ") || j }), /* @__PURE__ */ t.jsxs(J, { gap: 2, align: "center", children: [ (a || l) && /* @__PURE__ */ t.jsx(ke, { size: "sm" }), /* @__PURE__ */ t.jsxs(J, { gap: 1, align: "center", children: [ R && E && E.length > 0 && /* @__PURE__ */ t.jsx( xe, { as: "span", size: "xs", variant: "ghost", css: H, onClick: (x) => { x.stopPropagation(), T([]); }, children: /* @__PURE__ */ t.jsx(ne, { boxSize: 4, as: ze }) } ), /* @__PURE__ */ t.jsx( Me, { customIcon: b, dropdownIndicatorCss: o?.dropdownIndicator } ) ] }) ] }) ] }) }), /* @__PURE__ */ t.jsx(Ie, { disabled: N, children: /* @__PURE__ */ t.jsx(G.Positioner, { children: /* @__PURE__ */ t.jsx( G.Content, { width: "auto", minH: m.length > 7 ? "250px" : "auto", css: V, children: /* @__PURE__ */ t.jsx( Fe, { options: m, getOptionLabel: n, getOptionValue: s, searchInputPlaceholder: y, onInputChange: (x) => { X(x.target.value), Y(x); }, value: E || [], onSelect: (x) => { T(x), C && v(!1); }, isLoading: a, isFetchingNextPage: l, fetchNextPage: f, hasNextPage: i, loadingElement: g, emptyElement: S, loadingMessageCss: o?.loadingMessage, emptyMessageCss: o?.emptyMessage, inputCss: o?.input, listboxProps: w, withIndicator: z, withCheckmark: M, searchValue: I } ) } ) }) }) ] } ); } const Xe = q(Be), Ge = q( ({ controlCss: n, ...s }) => { const a = P( { "&[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" } }, n ); return /* @__PURE__ */ t.jsx(xe, { ...s, css: a, children: s.children }); } ); export { Xe as AsyncCombobox };