-
-
Notifications
You must be signed in to change notification settings - Fork 404
Expand file tree
/
Copy pathcombobox.tsx
More file actions
59 lines (56 loc) · 1.98 KB
/
combobox.tsx
File metadata and controls
59 lines (56 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import * as React from 'react';
import { Check, ChevronsUpDown } from 'lucide-react';
import { cn } from '@/lib/utils';
import { Button } from '@/components/ui/button';
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from '@/components/ui/command';
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
export function Combobox({
items,
value,
searchText = 'Search items...',
noneFoundText = 'No items found.',
onValueChange,
}: {
items: { value: string; label: string }[];
value: string;
searchText?: string;
noneFoundText?: string;
onValueChange: (value: string) => void;
}) {
const [open, setOpen] = React.useState(false);
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button variant="outline" role="combobox" aria-expanded={open} className="flex-1 justify-between">
<span>{value ? items.find((item) => item.value === value)?.label : ''}</span>
<ChevronsUpDown className="opacity-50" />
</Button>
</PopoverTrigger>
<PopoverContent className="p-0">
<Command>
<CommandInput placeholder={searchText} />
<CommandList className="p-0">
<CommandEmpty>{noneFoundText}</CommandEmpty>
<CommandGroup>
{open &&
items.map((item) => (
<CommandItem
key={item.value}
value={item.value}
onSelect={(currentValue) => {
value = currentValue;
onValueChange(value);
setOpen(false);
}}
>
{item.label}
<Check className={cn('ml-auto', value === item.value ? 'opacity-100' : 'opacity-0')} />
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
);
}