Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

add virtual list for combobox

Carl-Gerhard Lindesvärd01ed4acce23c
diff --git a/apps/dashboard/package.json b/apps/dashboard/package.json
index 2781fbb3a..d002a0fb3 100644
--- a/apps/dashboard/package.json
+++ b/apps/dashboard/package.json
@@ -81,6 +81,7 @@
"pushmodal": "^1.0.3",
"ramda": "^0.29.1",
"random-animal-name": "^0.1.1",
+ "rc-virtual-list": "^3.14.5",
"react": "18.2.0",
"react-animate-height": "^3.2.3",
"react-animated-numbers": "^0.18.0",
diff --git a/apps/dashboard/src/components/ui/combobox-advanced.tsx b/apps/dashboard/src/components/ui/combobox-advanced.tsx
index 11905048e..0c7914211 100644
--- a/apps/dashboard/src/components/ui/combobox-advanced.tsx
+++ b/apps/dashboard/src/components/ui/combobox-advanced.tsx
@@ -1,13 +1,9 @@
import * as React from 'react';
import { Badge } from '@/components/ui/badge';
-import {
- Command,
- CommandGroup,
- CommandInput,
- CommandItem,
- CommandList,
-} from '@/components/ui/command';
+import { Command, CommandInput, CommandItem } from '@/components/ui/command';
+import { cn } from '@/utils/cn';
import { ChevronsUpDownIcon } from 'lucide-react';
+import VirtualList from 'rc-virtual-list';
import { useOnClickOutside } from 'usehooks-ts';
import { Button } from './button';
@@ -71,10 +67,31 @@ export function ComboboxAdvanced({
);
};
- const renderUnknownItem = (value: IValue) => {
- const item = items.find((item) => item.value === value);
- return item ? renderItem(item) : renderItem({ value, label: value });
- };
+ const data = React.useMemo(() => {
+ return [
+ ...(inputValue === ''
+ ? []
+ : [
+ {
+ value: inputValue,
+ label: `Pick '${inputValue}'`,
+ },
+ ]),
+ ...value.map((val) => {
+ const item = items.find((item) => item.value === val);
+ return item
+ ? {
+ value: val,
+ label: item.label,
+ }
+ : {
+ value: val,
+ label: val,
+ };
+ }),
+ ...selectables,
+ ].filter((item) => item.value);
+ }, [inputValue, selectables, items]);
return (
<Popover open={open} onOpenChange={setOpen}>
@@ -82,7 +99,7 @@ export function ComboboxAdvanced({
<Button
variant={'outline'}
onClick={() => setOpen((prev) => !prev)}
- className={className}
+ className={cn('h-min-10 h-auto', className)}
>
<div className="flex w-full flex-wrap gap-1">
{value.length === 0 && placeholder}
@@ -104,15 +121,9 @@ export function ComboboxAdvanced({
value={inputValue}
onValueChange={setInputValue}
/>
- <CommandList>
- {inputValue !== '' &&
- renderItem({
- value: inputValue,
- label: `Pick '${inputValue}'`,
- })}
- {value.map(renderUnknownItem)}
- {selectables.map(renderItem)}
- </CommandList>
+ <VirtualList height={300} data={data} itemHeight={32} itemKey="value">
+ {renderItem}
+ </VirtualList>
</Command>
</PopoverContent>
</Popover>
diff --git a/apps/dashboard/src/components/ui/combobox.tsx b/apps/dashboard/src/components/ui/combobox.tsx
index 0cb72ae6c..46a2a6775 100644
--- a/apps/dashboard/src/components/ui/combobox.tsx
+++ b/apps/dashboard/src/components/ui/combobox.tsx
@@ -6,7 +6,6 @@ import { Button } from '@/components/ui/button';
import {
Command,
CommandEmpty,
- CommandGroup,
CommandInput,
CommandItem,
} from '@/components/ui/command';
@@ -18,6 +17,7 @@ import {
import { cn } from '@/utils/cn';
import type { LucideIcon } from 'lucide-react';
import { Check, ChevronsUpDown } from 'lucide-react';
+import VirtualList from 'rc-virtual-list';
export interface ComboboxProps<T> {
placeholder: string;
@@ -123,30 +123,33 @@ export function Combobox<T extends string>({
) : (
<CommandEmpty>Nothing selected</CommandEmpty>
)}
- <div className="over-x-hidden max-h-[300px] overflow-y-auto">
- <CommandGroup>
- {items.map((item) => (
- <CommandItem
- key={item.value}
- value={item.value}
- onSelect={(currentValue) => {
- const value = find(currentValue)?.value ?? currentValue;
- onChange(value as T);
- setOpen(false);
- }}
- {...(item.disabled && { disabled: true })}
- >
- <Check
- className={cn(
- 'mr-2 h-4 w-4 flex-shrink-0',
- value === item.value ? 'opacity-100' : 'opacity-0'
- )}
- />
- {item.label}
- </CommandItem>
- ))}
- </CommandGroup>
- </div>
+ <VirtualList
+ height={300}
+ data={items}
+ itemHeight={32}
+ itemKey="value"
+ >
+ {(item) => (
+ <CommandItem
+ key={item.value}
+ value={item.value}
+ onSelect={(currentValue) => {
+ const value = find(currentValue)?.value ?? currentValue;
+ onChange(value as T);
+ setOpen(false);
+ }}
+ {...(item.disabled && { disabled: true })}
+ >
+ <Check
+ className={cn(
+ 'mr-2 h-4 w-4 flex-shrink-0',
+ value === item.value ? 'opacity-100' : 'opacity-0'
+ )}
+ />
+ {item.label}
+ </CommandItem>
+ )}
+ </VirtualList>
</Command>
</PopoverContent>
</Popover>
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 28589c6f2..215c57fdd 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -360,6 +360,9 @@ importers:
random-animal-name:
specifier: ^0.1.1
version: 0.1.1
+ rc-virtual-list:
+ specifier: ^3.14.5
react:
specifier: 18.2.0
version: 18.2.0
@@ -8949,6 +8952,10 @@ packages:
clsx: 2.0.0
dev: false
+ resolution: {integrity: sha512-saHYOzhIQs6wy2sVxTM6bUDsQO4F50V9RQ22qBpEdCW+I+/Wmke2HOl6lS6dTpdxVhb88/I6+Hs+438c3lfUow==}
+ dev: false
+
resolution: {integrity: sha512-4diC9HaTE+KRAMWhDhrGOECgWZxoevMc5TlkObMqNSsVU62PYzXZ/SMTjzyGAFF1YusgxGcSWTEXBhp0CPwQ1A==}
engines: {node: '>=6'}
@@ -15758,6 +15765,47 @@ packages:
unpipe: 1.0.0
dev: false
+ resolution: {integrity: sha512-PnMVyRid9JLxFavTjeDXEXo65HCRqbmLBw9xX9gfC4BZiSzbLXKzW3jPz+J0P71pLbD5tBMTT+mkstV5gD0c9Q==}
+ peerDependencies:
+ react: '>=16.9.0'
+ react-dom: '>=16.9.0'
+ dependencies:
+ '@babel/runtime': 7.23.9
+ classnames: 2.5.1
+ react: 18.2.0
+ react-dom: 18.2.0([email protected])
+ resize-observer-polyfill: 1.5.1
+ dev: false
+
+ resolution: {integrity: sha512-AzC7KKOXFqAdIBqdGWepL9Xn7cm3vnAmjlHqUnoQaTMZYhM4VlXGLkkHHxj/BZ7Td0+SOPKB4RGPboBVKT9htw==}
+ peerDependencies:
+ react: '>=16.9.0'
+ react-dom: '>=16.9.0'
+ dependencies:
+ '@babel/runtime': 7.23.9
+ react: 18.2.0
+ react-dom: 18.2.0([email protected])
+ react-is: 18.2.0
+ dev: false
+
+ resolution: {integrity: sha512-ZMOnkCLv2wUN8Jz7yI4XiSLa9THlYvf00LuMhb1JlsQCewuU7ydPuHw1rGVPhe9VZYl/5UqODtNd7QKJ2DMGfg==}
+ engines: {node: '>=8.x'}
+ peerDependencies:
+ react: '>=16.9.0'
+ react-dom: '>=16.9.0'
+ dependencies:
+ '@babel/runtime': 7.23.9
+ classnames: 2.5.1
+ rc-resize-observer: 1.4.0([email protected])([email protected])
+ react: 18.2.0
+ react-dom: 18.2.0([email protected])
+ dev: false
+
resolution: {integrity: sha512-y3bGgqKj3QBdxLbLkomlohkvsA8gdAiUQlSBJnBhfn+BPxg4bc62d8TcBW15wavDfgexCgccckhcZvywyQYPOw==}
hasBin: true
@@ -16509,6 +16557,10 @@ packages:
'@canvas/image-data': 1.0.0
dev: false
+ resolution: {integrity: sha512-LwZrotdHOo12nQuZlHEmtuXdqGoOD0OhaxopaNFxWzInpEgaLWoVuAMbTzixuosCx2nEG58ngzW3vxdWoxIgdg==}
+ dev: false
+
resolution: {integrity: sha512-GnlH6vxLymXJNMBo7XP1fJIzBFbdYt49CuTwmB/6N53t+kMPRMFKz783LlQ4tv28XoQfMWinAJX6WCGf2IlaIw==}
engines: {node: '>=4'}