-
Notifications
You must be signed in to change notification settings - Fork 562
Expand file tree
/
Copy pathsearchable-select.tsx
More file actions
144 lines (136 loc) · 3.93 KB
/
searchable-select.tsx
File metadata and controls
144 lines (136 loc) · 3.93 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { Check, ChevronDown } from "lucide-react";
import { useCallback, useMemo, useState } from "react";
import { Button } from "@hypr/ui/components/ui/button";
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
} from "@hypr/ui/components/ui/command";
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@hypr/ui/components/ui/popover";
import { cn } from "@hypr/utils";
export interface SearchableSelectOption {
value: string;
label: string;
detail?: string;
}
interface SearchableSelectProps {
value: string;
onChange: (value: string) => void;
options: SearchableSelectOption[];
placeholder?: string;
searchPlaceholder?: string;
emptyMessage?: string;
className?: string;
}
const filterFunction = (value: string, search: string) => {
const v = value.toLocaleLowerCase();
const s = search.toLocaleLowerCase();
if (v.includes(s)) {
return 1;
}
return 0;
};
export function SearchableSelect({
value,
onChange,
options,
placeholder = "Select...",
searchPlaceholder = "Search...",
emptyMessage = "No results found.",
className,
}: SearchableSelectProps) {
const [open, setOpen] = useState(false);
const [query, setQuery] = useState("");
const selectedOption = useMemo(
() => options.find((opt) => opt.value === value),
[options, value],
);
const handleSelect = useCallback(
(optionValue: string) => {
onChange(optionValue);
setOpen(false);
setQuery("");
},
[onChange],
);
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button
type="button"
variant="outline"
role="combobox"
aria-expanded={open}
className={cn([
"justify-between font-normal bg-white shadow-none focus-visible:ring-0",
"rounded-lg px-3",
className,
])}
>
<span className="truncate">
{selectedOption
? selectedOption.detail
? `${selectedOption.label} (${selectedOption.detail})`
: selectedOption.label
: placeholder}
</span>
<ChevronDown className="-mr-1 h-4 w-4 shrink-0 opacity-50" />
</Button>
</PopoverTrigger>
<PopoverContent
className="p-0"
align="start"
style={{ width: "var(--radix-popover-trigger-width)" }}
>
<Command filter={filterFunction}>
<CommandInput
placeholder={searchPlaceholder}
value={query}
onValueChange={setQuery}
/>
<CommandEmpty>
<div className="py-1.5 px-2 text-sm text-muted-foreground">
{emptyMessage}
</div>
</CommandEmpty>
<CommandList>
<CommandGroup className="max-h-[250px] overflow-y-auto">
{options.map((option) => (
<CommandItem
key={option.value}
value={
option.detail
? `${option.label} ${option.detail}`
: option.label
}
onSelect={() => handleSelect(option.value)}
className={cn([
"cursor-pointer",
"hover:bg-neutral-200! data-[selected=true]:bg-neutral-200!",
])}
>
<span className="truncate flex-1">{option.label}</span>
{option.detail && (
<span className="text-[10px] font-mono text-neutral-400 shrink-0">
{option.detail}
</span>
)}
{value === option.value && (
<Check className="h-4 w-4 shrink-0" />
)}
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
);
}