Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

feat: add virtualization to comboboxes to support thousand-plus item lists

Brad Barbinb6a8159bc874
diff --git a/packages/react/src/Combobox.tsx b/packages/react/src/Combobox.tsx
index 189860cdc9..54e5db0c48 100644
--- a/packages/react/src/Combobox.tsx
+++ b/packages/react/src/Combobox.tsx
@@ -1,10 +1,10 @@
+import { useVirtualizer } from "@tanstack/react-virtual";
import type { ComponentPropsWithoutRef } from "react";
-import { forwardRef, useState } from "react";
+import { forwardRef, useMemo, useRef, useState } from "react";
import { MdClose } from "react-icons/md";
import { RxCheck } from "react-icons/rx";
import {
Command,
- CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
@@ -13,7 +13,6 @@ import {
import { HStack } from "./HStack";
import { IconButton } from "./IconButton";
import { Popover, PopoverContent, PopoverTrigger } from "./Popover";
-import { ScrollArea } from "./ScrollArea";
import { cn } from "./utils/cn";
export type ComboboxProps = Omit<
@@ -31,6 +30,7 @@ export type ComboboxProps = Omit<
isReadOnly?: boolean;
placeholder?: string;
onChange?: (selected: string) => void;
+ itemHeight?: number;
};
const Combobox = forwardRef<HTMLButtonElement, ComboboxProps>(
@@ -43,11 +43,13 @@ const Combobox = forwardRef<HTMLButtonElement, ComboboxProps>(
isReadOnly,
placeholder,
onChange,
+ itemHeight = 40,
...props
},
ref
) => {
const [open, setOpen] = useState(false);
+
return (
<HStack spacing={1}>
<Popover open={open} onOpenChange={setOpen}>
@@ -57,8 +59,8 @@ const Combobox = forwardRef<HTMLButtonElement, ComboboxProps>(
role="combobox"
className={cn("min-w-[160px]", !value && "text-muted-foreground")}
ref={ref}
- onClick={() => setOpen(true)}
{...props}
+ onClick={() => setOpen(true)}
>
{value ? (
options.find((option) => option.value === value)?.label
@@ -70,45 +72,13 @@ const Combobox = forwardRef<HTMLButtonElement, ComboboxProps>(
</CommandTrigger>
</PopoverTrigger>
<PopoverContent className="min-w-[200px] w-[--radix-popover-trigger-width] p-0">
- <Command>
- <CommandInput placeholder="Search..." className="h-9" />
- <ScrollArea className="overflow-auto max-h-96">
- <CommandEmpty>No option found.</CommandEmpty>
- <CommandGroup>
- {options.map((option) => (
- <CommandItem
- value={
- typeof option.label === "string"
- ? option.label + option.helper
- : undefined
- }
- key={option.value}
- onSelect={() => {
- onChange?.(option.value);
- setOpen(false);
- }}
- >
- {option.helper ? (
- <div className="flex flex-col">
- <p>{option.label}</p>
- <p className="text-xs text-muted-foreground">
- {option.helper}
- </p>
- </div>
- ) : (
- option.label
- )}
- <RxCheck
- className={cn(
- "ml-auto h-4 w-4",
- option.value === value ? "opacity-100" : "opacity-0"
- )}
- />
- </CommandItem>
- ))}
- </CommandGroup>
- </ScrollArea>
- </Command>
+ <VirtualizedCommand
+ options={options}
+ value={value}
+ onChange={onChange}
+ itemHeight={itemHeight}
+ setOpen={setOpen}
+ />
</PopoverContent>
</Popover>
{isClearable && !isReadOnly && value && (
@@ -127,3 +97,113 @@ const Combobox = forwardRef<HTMLButtonElement, ComboboxProps>(
Combobox.displayName = "Combobox";
export { Combobox };
+
+type VirtualizedCommandProps = {
+ options: ComboboxProps["options"];
+ value?: string;
+ onChange?: (selected: string) => void;
+ itemHeight: number;
+ setOpen: (open: boolean) => void;
+};
+
+function VirtualizedCommand({
+ options,
+ value,
+ onChange,
+ itemHeight,
+ setOpen,
+}: VirtualizedCommandProps) {
+ const [search, setSearch] = useState("");
+ const parentRef = useRef<HTMLDivElement>(null);
+
+ const filteredOptions = useMemo(() => {
+ return search
+ ? options.filter((option) => {
+ return `${option.label} ${option.helper}`
+ .toLowerCase()
+ .includes(search.toLowerCase());
+ })
+ : options;
+ }, [options, search]);
+
+ const virtualizer = useVirtualizer({
+ count: filteredOptions.length,
+ getScrollElement: () => parentRef.current,
+ estimateSize: () => itemHeight,
+ overscan: 5,
+ });
+
+ const items = virtualizer.getVirtualItems();
+
+ return (
+ <Command shouldFilter={false}>
+ <CommandInput
+ value={search}
+ onValueChange={setSearch}
+ placeholder="Search..."
+ className="h-9"
+ />
+ <div
+ ref={parentRef}
+ className="overflow-auto"
+ style={{
+ height: `${Math.min(filteredOptions.length, 6) * itemHeight}px`,
+ }}
+ >
+ <CommandGroup
+ style={{
+ height: `${virtualizer.getTotalSize()}px`,
+ width: "100%",
+ position: "relative",
+ }}
+ >
+ {items.map((virtualRow) => {
+ const item = filteredOptions[virtualRow.index];
+
+ return (
+ <CommandItem
+ key={item.value}
+ value={
+ typeof item.label === "string"
+ ? item.label.replace(/"/g, '\\"') +
+ item.helper?.replace(/"/g, '\\"')
+ : undefined
+ }
+ onSelect={() => {
+ onChange?.(item.value);
+ setSearch("");
+ setOpen(false);
+ }}
+ style={{
+ position: "absolute",
+ top: 0,
+ left: 0,
+ width: "100%",
+ height: `${itemHeight}px`,
+ transform: `translateY(${virtualRow.start}px)`,
+ }}
+ >
+ {item.helper ? (
+ <div className="flex flex-col">
+ <p>{item.label}</p>
+ <p className="text-xs text-muted-foreground">
+ {item.helper}
+ </p>
+ </div>
+ ) : (
+ item.label
+ )}
+ <RxCheck
+ className={cn(
+ "ml-auto h-4 w-4",
+ item.value === value ? "opacity-100" : "opacity-0"
+ )}
+ />
+ </CommandItem>
+ );
+ })}
+ </CommandGroup>
+ </div>
+ </Command>
+ );
+}
diff --git a/packages/react/src/CreateableCombobox.tsx b/packages/react/src/CreateableCombobox.tsx
index ca82823850..576e86ac5f 100644
--- a/packages/react/src/CreateableCombobox.tsx
+++ b/packages/react/src/CreateableCombobox.tsx
@@ -1,5 +1,6 @@
+import { useVirtualizer } from "@tanstack/react-virtual";
import type { ComponentPropsWithoutRef } from "react";
-import { forwardRef, useState } from "react";
+import { forwardRef, useMemo, useRef, useState } from "react";
import { MdClose } from "react-icons/md";
import { RxCheck } from "react-icons/rx";
import {
@@ -12,7 +13,6 @@ import {
import { HStack } from "./HStack";
import { IconButton } from "./IconButton";
import { Popover, PopoverContent, PopoverTrigger } from "./Popover";
-import { ScrollArea } from "./ScrollArea";
import { cn } from "./utils/cn";
export type CreatableComboboxProps = Omit<
@@ -33,6 +33,7 @@ export type CreatableComboboxProps = Omit<
placeholder?: string;
onChange?: (selected: string) => void;
onCreateOption?: (inputValue: string) => void;
+ itemHeight?: number;
};
const CreatableCombobox = forwardRef<HTMLButtonElement, CreatableComboboxProps>(
@@ -47,17 +48,13 @@ const CreatableCombobox = forwardRef<HTMLButtonElement, CreatableComboboxProps>(
placeholder,
onChange,
label,
+ onCreateOption,
+ itemHeight = 40,
...props
},
ref
) => {
const [open, setOpen] = useState(false);
- const [search, setSearch] = useState("");
- const isExactMatch = options.some((option) =>
- [option.label.toLowerCase(), option.helper?.toLowerCase()].includes(
- search.toLowerCase()
- )
- );
return (
<HStack spacing={1}>
@@ -81,67 +78,15 @@ const CreatableCombobox = forwardRef<HTMLButtonElement, CreatableComboboxProps>(
</CommandTrigger>
</PopoverTrigger>
<PopoverContent className="min-w-[200px] w-[--radix-popover-trigger-width] p-0">
- <Command>
- <CommandInput
- value={search}
- onValueChange={setSearch}
- placeholder="Search..."
- className="h-9"
- />
- <ScrollArea className="overflow-auto max-h-96">
- <CommandGroup>
- {options.map((option) => {
- const isSelected = !!selected?.includes(option.value);
- return (
- <CommandItem
- value={
- typeof option.label === "string"
- ? option.label.replace(/"/g, '\\"') +
- option.helper?.replace(/"/g, '\\"')
- : undefined
- }
- key={option.value}
- onSelect={() => {
- if (!isSelected) onChange?.(option.value);
- setOpen(false);
- }}
- >
- {option.helper ? (
- <div className="flex flex-col">
- <p>{option.label}</p>
- <p className="text-xs text-muted-foreground">
- {option.helper}
- </p>
- </div>
- ) : (
- option.label
- )}
- <RxCheck
- className={cn(
- "ml-auto h-4 w-4",
- isSelected || option.value === value
- ? "opacity-100"
- : "opacity-0"
- )}
- />
- </CommandItem>
- );
- })}
- {!isExactMatch && (
- <CommandItem
- onSelect={() => {
- props.onCreateOption?.(search);
- }}
- value={search?.replace(/"/g, '\\"').trim() || ""}
- className="cursor-pointer"
- >
- <span>Create</span>
- <span className="ml-1 font-bold">{search || label}</span>
- </CommandItem>
- )}
- </CommandGroup>
- </ScrollArea>
- </Command>
+ <VirtualizedCommand
+ options={options}
+ selected={selected}
+ value={value}
+ onChange={onChange}
+ onCreateOption={onCreateOption}
+ itemHeight={itemHeight}
+ setOpen={setOpen}
+ />
</PopoverContent>
</Popover>
{isClearable && !isReadOnly && value && (
@@ -160,3 +105,149 @@ const CreatableCombobox = forwardRef<HTMLButtonElement, CreatableComboboxProps>(
CreatableCombobox.displayName = "CreatableCombobox";
export { CreatableCombobox };
+
+type VirtualizedCommandProps = {
+ options: CreatableComboboxProps["options"];
+ selected?: string[];
+ value?: string;
+ onChange?: (selected: string) => void;
+ onCreateOption?: (inputValue: string) => void;
+ itemHeight: number;
+ setOpen: (open: boolean) => void;
+};
+
+function VirtualizedCommand({
+ options,
+ selected,
+ value,
+ onChange,
+ onCreateOption,
+ itemHeight,
+ setOpen,
+}: VirtualizedCommandProps) {
+ const [search, setSearch] = useState("");
+ const parentRef = useRef<HTMLDivElement>(null);
+
+ const filteredOptions = useMemo(() => {
+ const filtered = search
+ ? options.filter((option) => {
+ return `${option.label} ${option.helper}`
+ .toLowerCase()
+ .includes(search.toLowerCase());
+ })
+ : options;
+
+ if (
+ !options.some((option) =>
+ [option.label.toLowerCase(), option.helper?.toLowerCase()].includes(
+ search.toLowerCase()
+ )
+ ) &&
+ search.trim() !== ""
+ ) {
+ filtered.push({
+ label: `Create "${search}"`,
+ value: "create",
+ });
+ }
+
+ return filtered;
+ }, [options, search]);
+
+ const virtualizer = useVirtualizer({
+ count: filteredOptions.length,
+ getScrollElement: () => parentRef.current,
+ estimateSize: () => itemHeight,
+ overscan: 5,
+ });
+
+ const items = virtualizer.getVirtualItems();
+
+ return (
+ <Command shouldFilter={false}>
+ <CommandInput
+ value={search}
+ onValueChange={setSearch}
+ placeholder="Search..."
+ className="h-9"
+ />
+ <div
+ ref={parentRef}
+ className="overflow-auto"
+ style={{
+ height: `${Math.min(filteredOptions.length, 6) * itemHeight}px`,
+ }}
+ >
+ <CommandGroup
+ style={{
+ height: `${virtualizer.getTotalSize()}px`,
+ width: "100%",
+ position: "relative",
+ }}
+ >
+ {items.map((virtualRow) => {
+ const item = filteredOptions[virtualRow.index];
+
+ const isSelected = !!selected?.includes(item.value);
+ const isCreateOption = item.value === "create";
+
+ return (
+ <CommandItem
+ key={item.value}
+ value={
+ typeof item.label === "string"
+ ? item.label.replace(/"/g, '\\"') +
+ item.helper?.replace(/"/g, '\\"')
+ : undefined
+ }
+ onSelect={() => {
+ if (isCreateOption) {
+ onCreateOption?.(search);
+ } else if (!isSelected) {
+ onChange?.(item.value);
+ setSearch("");
+ }
+ setOpen(false);
+ }}
+ style={{
+ position: "absolute",
+ top: 0,
+ left: 0,
+ width: "100%",
+ height: `${itemHeight}px`,
+ transform: `translateY(${virtualRow.start}px)`,
+ }}
+ >
+ {isCreateOption ? (
+ <>
+ <span>Create</span>
+ <span className="ml-1 font-bold">{search}</span>
+ </>
+ ) : item.helper ? (
+ <div className="flex flex-col">
+ <p>{item.label}</p>
+ <p className="text-xs text-muted-foreground">
+ {item.helper}
+ </p>
+ </div>
+ ) : (
+ item.label
+ )}
+ {!isCreateOption && (
+ <RxCheck
+ className={cn(
+ "ml-auto h-4 w-4",
+ isSelected || item.value === value
+ ? "opacity-100"
+ : "opacity-0"
+ )}
+ />
+ )}
+ </CommandItem>
+ );
+ })}
+ </CommandGroup>
+ </div>
+ </Command>
+ );
+}
diff --git a/packages/react/src/CreateableMultiSelect.tsx b/packages/react/src/CreateableMultiSelect.tsx
index ceb815df96..e6c4a76f5a 100644
--- a/packages/react/src/CreateableMultiSelect.tsx
+++ b/packages/react/src/CreateableMultiSelect.tsx
@@ -1,6 +1,7 @@
+import { useVirtualizer } from "@tanstack/react-virtual";
import { CommandEmpty } from "cmdk";
import type { ComponentPropsWithoutRef } from "react";
-import { forwardRef, useId, useState } from "react";
+import { forwardRef, useId, useMemo, useRef, useState } from "react";
import { MdClose } from "react-icons/md";
import { RxCheck, RxMagnifyingGlass } from "react-icons/rx";
import { Badge, BadgeCloseButton } from "./Badge";
@@ -15,7 +16,6 @@ import {
import { HStack } from "./HStack";
import { IconButton } from "./IconButton";
import { Popover, PopoverContent, PopoverTrigger } from "./Popover";
-import { ScrollArea } from "./ScrollArea";
import { cn } from "./utils/cn";
export type CreatableMultiSelectProps = Omit<
@@ -36,6 +36,7 @@ export type CreatableMultiSelectProps = Omit<
placeholder?: string;
onChange: (selected: string[]) => void;
onCreateOption?: (inputValue: string) => void;
+ itemHeight?: number;
};
const CreatableMultiSelect = forwardRef<
@@ -54,17 +55,13 @@ const CreatableMultiSelect = forwardRef<
onChange,
label,
className,
+ onCreateOption,
+ itemHeight = 40,
...props
},
ref
) => {
const [open, setOpen] = useState(false);
- const [search, setSearch] = useState("");
- const isExactMatch = options.some((option) =>
- [option.label.toLowerCase(), option.helper?.toLowerCase()].includes(
- search.toLowerCase()
- )
- );
const id = useId();
@@ -123,55 +120,14 @@ const CreatableMultiSelect = forwardRef<
</Button>
</PopoverTrigger>
<PopoverContent className="min-w-[200px] w-[--radix-popover-trigger-width] p-0">
- <Command>
- <CommandInput
- placeholder="Search..."
- value={search}
- onValueChange={setSearch}
- className="h-9"
- />
- <ScrollArea className="overflow-auto max-h-96">
- <CommandEmpty>No option found.</CommandEmpty>
- <CommandGroup className="max-h-64 overflow-auto">
- {options.map((option) => (
- <CommandItem
- key={option.value}
- onSelect={() => {
- onChange(
- value.includes(option.value)
- ? value.filter((item) => item !== option.value)
- : [...value, option.value]
- );
- setOpen(true);
- }}
- >
- <RxCheck
- className={cn(
- "mr-2 h-4 w-4",
- value.includes(option.value)
- ? "opacity-100"
- : "opacity-0"
- )}
- />
- {option.label}
- </CommandItem>
- ))}
- {!isExactMatch && (
- <CommandItem
- onSelect={() => {
- props.onCreateOption?.(search);
- setSearch("");
- }}
- value={search?.replace(/"/g, '\\"').trim() || ""}
- className="cursor-pointer"
- >
- <span>Create</span>
- <span className="ml-1 font-bold">{search || label}</span>
- </CommandItem>
- )}
- </CommandGroup>
- </ScrollArea>
- </Command>
+ <VirtualizedCommand
+ options={options}
+ selected={value}
+ onChange={onChange}
+ onCreateOption={onCreateOption}
+ itemHeight={itemHeight}
+ setOpen={setOpen}
+ />
</PopoverContent>
</Popover>
{isClearable && !isReadOnly && value && (
@@ -191,6 +147,152 @@ CreatableMultiSelect.displayName = "CreatableMultiSelect";
export { CreatableMultiSelect };
+type VirtualizedCommandProps = {
+ options: CreatableMultiSelectProps["options"];
+ selected: string[];
+ onChange: (selected: string[]) => void;
+ onCreateOption?: (inputValue: string) => void;
+ itemHeight: number;
+ setOpen: (open: boolean) => void;
+};
+
+function VirtualizedCommand({
+ options,
+ selected,
+ onChange,
+ onCreateOption,
+ itemHeight,
+ setOpen,
+}: VirtualizedCommandProps) {
+ const [search, setSearch] = useState("");
+ const parentRef = useRef<HTMLDivElement>(null);
+
+ const filteredOptions = useMemo(() => {
+ const filtered = search
+ ? options.filter((option) => {
+ return `${option.label} ${option.helper}`
+ .toLowerCase()
+ .includes(search.toLowerCase());
+ })
+ : options;
+
+ if (
+ !options.some((option) =>
+ [option.label.toLowerCase(), option.helper?.toLowerCase()].includes(
+ search.toLowerCase()
+ )
+ ) &&
+ search.trim() !== ""
+ ) {
+ filtered.push({
+ label: `Create "${search}"`,
+ value: "create",
+ });
+ }
+
+ return filtered;
+ }, [options, search]);
+
+ const virtualizer = useVirtualizer({
+ count: filteredOptions.length,
+ getScrollElement: () => parentRef.current,
+ estimateSize: () => itemHeight,
+ overscan: 5,
+ });
+
+ const items = virtualizer.getVirtualItems();
+
+ return (
+ <Command shouldFilter={false}>
+ <CommandInput
+ value={search}
+ onValueChange={setSearch}
+ placeholder="Search..."
+ className="h-9"
+ />
+ <div
+ ref={parentRef}
+ className="overflow-auto"
+ style={{
+ height: `${Math.min(filteredOptions.length, 6) * itemHeight}px`,
+ }}
+ >
+ <CommandEmpty>No option found.</CommandEmpty>
+ <CommandGroup
+ style={{
+ height: `${virtualizer.getTotalSize()}px`,
+ width: "100%",
+ position: "relative",
+ }}
+ >
+ {items.map((virtualRow) => {
+ const item = filteredOptions[virtualRow.index];
+ const isSelected = selected.includes(item.value);
+ const isCreateOption = item.value === "create";
+
+ return (
+ <CommandItem
+ key={item.value}
+ value={
+ typeof item.label === "string"
+ ? item.label.replace(/"/g, '\\"') +
+ item.helper?.replace(/"/g, '\\"')
+ : undefined
+ }
+ onSelect={() => {
+ if (isCreateOption) {
+ onCreateOption?.(search);
+ setSearch("");
+ } else {
+ onChange(
+ isSelected
+ ? selected.filter((value) => value !== item.value)
+ : [...selected, item.value]
+ );
+ }
+ setOpen(true);
+ }}
+ style={{
+ position: "absolute",
+ top: 0,
+ left: 0,
+ width: "100%",
+ height: `${itemHeight}px`,
+ transform: `translateY(${virtualRow.start}px)`,
+ }}
+ >
+ {isCreateOption ? (
+ <>
+ <span>Create</span>
+ <span className="ml-1 font-bold">{search}</span>
+ </>
+ ) : item.helper ? (
+ <div className="flex flex-col">
+ <p>{item.label}</p>
+ <p className="text-xs text-muted-foreground">
+ {item.helper}
+ </p>
+ </div>
+ ) : (
+ item.label
+ )}
+ {!isCreateOption && (
+ <RxCheck
+ className={cn(
+ "ml-auto h-4 w-4",
+ isSelected ? "opacity-100" : "opacity-0"
+ )}
+ />
+ )}
+ </CommandItem>
+ );
+ })}
+ </CommandGroup>
+ </div>
+ </Command>
+ );
+}
+
function SelectedOption({
isReadOnly,
item,
@@ -203,7 +305,12 @@ function SelectedOption({
onUnselect: (item: string) => void;
}) {
return (
- <Badge key={item} onClick={() => onUnselect(item)} variant="secondary">
+ <Badge
+ key={item}
+ onClick={() => onUnselect(item)}
+ variant="secondary"
+ className="border border-border shadow-sm"
+ >
{options.find((option) => option.value === item)?.label}
<BadgeCloseButton
tabIndex={-1}
diff --git a/packages/react/src/MultiSelect.tsx b/packages/react/src/MultiSelect.tsx
index 6606f9ce4f..759f268689 100644
--- a/packages/react/src/MultiSelect.tsx
+++ b/packages/react/src/MultiSelect.tsx
@@ -1,5 +1,6 @@
+import { useVirtualizer } from "@tanstack/react-virtual";
import type { ComponentPropsWithoutRef } from "react";
-import { forwardRef, useId, useState } from "react";
+import { forwardRef, useId, useMemo, useRef, useState } from "react";
import { RxCheck, RxMagnifyingGlass } from "react-icons/rx";
import { Badge, BadgeCloseButton } from "./Badge";
import { Button } from "./Button";
@@ -27,6 +28,7 @@ export type MultiSelectProps = Omit<
isReadOnly?: boolean;
placeholder?: string;
onChange: (selected: string[]) => void;
+ itemHeight?: number;
};
const MultiSelect = forwardRef<HTMLButtonElement, MultiSelectProps>(
@@ -39,11 +41,13 @@ const MultiSelect = forwardRef<HTMLButtonElement, MultiSelectProps>(
placeholder,
onChange,
className,
+ itemHeight = 40,
...props
},
ref
) => {
const [open, setOpen] = useState(false);
+
const id = useId();
const handleUnselect = (item: string) => {
@@ -53,7 +57,7 @@ const MultiSelect = forwardRef<HTMLButtonElement, MultiSelectProps>(
const hasSelections = value.length > 0;
return (
- <Popover>
+ <Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button
aria-controls={id}
@@ -110,33 +114,13 @@ const MultiSelect = forwardRef<HTMLButtonElement, MultiSelectProps>(
</Button>
</PopoverTrigger>
<PopoverContent className="min-w-[200px] w-[--radix-popover-trigger-width] p-0">
- <Command>
- <CommandInput placeholder="Search..." className="h-9" />
- <CommandEmpty>No option found.</CommandEmpty>
- <CommandGroup className="max-h-64 overflow-auto">
- {options.map((option) => (
- <CommandItem
- key={option.value}
- onSelect={() => {
- onChange(
- value.includes(option.value)
- ? value.filter((item) => item !== option.value)
- : [...value, option.value]
- );
- setOpen(true);
- }}
- >
- <RxCheck
- className={cn(
- "mr-2 h-4 w-4",
- value.includes(option.value) ? "opacity-100" : "opacity-0"
- )}
- />
- {option.label}
- </CommandItem>
- ))}
- </CommandGroup>
- </Command>
+ <VirtualizedCommand
+ options={options}
+ value={value}
+ onChange={onChange}
+ itemHeight={itemHeight}
+ setOpen={setOpen}
+ />
</PopoverContent>
</Popover>
);
@@ -145,3 +129,101 @@ const MultiSelect = forwardRef<HTMLButtonElement, MultiSelectProps>(
MultiSelect.displayName = "MultiSelect";
export { MultiSelect };
+
+type VirtualizedCommandProps = {
+ options: MultiSelectProps["options"];
+ value: string[];
+ onChange: (selected: string[]) => void;
+ itemHeight: number;
+ setOpen: (open: boolean) => void;
+};
+
+function VirtualizedCommand({
+ options,
+ value,
+ onChange,
+ itemHeight,
+ setOpen,
+}: VirtualizedCommandProps) {
+ const [search, setSearch] = useState("");
+ const parentRef = useRef<HTMLDivElement>(null);
+
+ const filteredOptions = useMemo(() => {
+ return search
+ ? options.filter((option) =>
+ option.label.toLowerCase().includes(search.toLowerCase())
+ )
+ : options;
+ }, [options, search]);
+
+ const virtualizer = useVirtualizer({
+ count: filteredOptions.length,
+ getScrollElement: () => parentRef.current,
+ estimateSize: () => itemHeight,
+ overscan: 5,
+ });
+
+ const items = virtualizer.getVirtualItems();
+
+ return (
+ <Command shouldFilter={false}>
+ <CommandInput
+ value={search}
+ onValueChange={setSearch}
+ placeholder="Search..."
+ className="h-9"
+ />
+ <CommandEmpty>No option found.</CommandEmpty>
+ <div
+ ref={parentRef}
+ className="overflow-auto"
+ style={{
+ height: `${Math.min(filteredOptions.length, 6) * itemHeight}px`,
+ }}
+ >
+ <CommandGroup
+ style={{
+ height: `${virtualizer.getTotalSize()}px`,
+ width: "100%",
+ position: "relative",
+ }}
+ >
+ {items.map((virtualRow) => {
+ const option = filteredOptions[virtualRow.index];
+ const isSelected = value.includes(option.value);
+
+ return (
+ <CommandItem
+ key={option.value}
+ onSelect={() => {
+ onChange(
+ isSelected
+ ? value.filter((item) => item !== option.value)
+ : [...value, option.value]
+ );
+ setOpen(true);
+ }}
+ style={{
+ position: "absolute",
+ top: 0,
+ left: 0,
+ width: "100%",
+ height: `${itemHeight}px`,
+ transform: `translateY(${virtualRow.start}px)`,
+ }}
+ >
+ <RxCheck
+ className={cn(
+ "mr-2 h-4 w-4",
+ isSelected ? "opacity-100" : "opacity-0"
+ )}
+ />
+ {option.label}
+ </CommandItem>
+ );
+ })}
+ </CommandGroup>
+ </div>
+ </Command>
+ );
+}