|
| 1 | +import { useMemo, useState } from 'react'; |
| 2 | +import { ChevronDown, Check, Plus } from 'lucide-react'; |
| 3 | +import { cn } from '../../lib/cn'; |
| 4 | +import { |
| 5 | + DropdownMenu, |
| 6 | + DropdownMenuContent, |
| 7 | + DropdownMenuSeparator, |
| 8 | + DropdownMenuTrigger, |
| 9 | +} from './DropdownMenu'; |
| 10 | +import { SearchField } from './SearchField'; |
| 11 | + |
| 12 | +export interface SearchableSelectOption<T extends string> { |
| 13 | + value: T; |
| 14 | + label: string; |
| 15 | + icon?: string; |
| 16 | +} |
| 17 | + |
| 18 | +export interface SearchableSelectProps<T extends string> { |
| 19 | + value: T; |
| 20 | + onChange: (value: T) => void; |
| 21 | + options: SearchableSelectOption<T>[]; |
| 22 | + placeholder: string; |
| 23 | + onCreateNew?: () => void; |
| 24 | + disabled?: boolean; |
| 25 | + testId?: string; |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Typeahead-filterable combobox built from DropdownMenu and SearchField. |
| 30 | + */ |
| 31 | +export function SearchableSelect<T extends string>({ |
| 32 | + value, |
| 33 | + onChange, |
| 34 | + options, |
| 35 | + placeholder, |
| 36 | + onCreateNew, |
| 37 | + disabled = false, |
| 38 | + testId, |
| 39 | +}: SearchableSelectProps<T>) { |
| 40 | + const [open, setOpen] = useState(false); |
| 41 | + const [filter, setFilter] = useState(''); |
| 42 | + |
| 43 | + const selectedOption = options.find((option) => option.value === value); |
| 44 | + |
| 45 | + const filteredOptions = useMemo(() => { |
| 46 | + const query = filter.trim().toLowerCase(); |
| 47 | + if (!query) { |
| 48 | + return options; |
| 49 | + } |
| 50 | + return options.filter((option) => option.label.toLowerCase().includes(query)); |
| 51 | + }, [filter, options]); |
| 52 | + |
| 53 | + /** |
| 54 | + * Sync open state and reset the filter when the panel closes. |
| 55 | + */ |
| 56 | + const handleOpenChange = (nextOpen: boolean) => { |
| 57 | + if (disabled && nextOpen) { |
| 58 | + return; |
| 59 | + } |
| 60 | + setOpen(nextOpen); |
| 61 | + if (!nextOpen) { |
| 62 | + setFilter(''); |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + /** |
| 67 | + * Select an option, notify the parent, and close the panel. |
| 68 | + */ |
| 69 | + const handleSelect = (optionValue: T) => { |
| 70 | + onChange(optionValue); |
| 71 | + setOpen(false); |
| 72 | + setFilter(''); |
| 73 | + }; |
| 74 | + |
| 75 | + /** |
| 76 | + * Invoke the optional create-new callback and close the panel. |
| 77 | + */ |
| 78 | + const handleCreateNew = () => { |
| 79 | + onCreateNew?.(); |
| 80 | + setOpen(false); |
| 81 | + setFilter(''); |
| 82 | + }; |
| 83 | + |
| 84 | + return ( |
| 85 | + <DropdownMenu open={open} onOpenChange={handleOpenChange} className="w-full"> |
| 86 | + <DropdownMenuTrigger |
| 87 | + data-testid={testId} |
| 88 | + className={cn( |
| 89 | + 'w-full flex items-center justify-between gap-2 px-3 py-2.5 rounded-xl border border-[rgb(var(--border))] bg-[rgb(var(--surface))] transition-all duration-150 group', |
| 90 | + disabled |
| 91 | + ? 'opacity-50 cursor-not-allowed' |
| 92 | + : 'hover:bg-[rgb(var(--surface-hover))] hover:border-[rgb(var(--primary))/30] cursor-pointer' |
| 93 | + )} |
| 94 | + aria-disabled={disabled} |
| 95 | + > |
| 96 | + <span className="flex items-center gap-3 min-w-0"> |
| 97 | + {selectedOption?.icon && ( |
| 98 | + <span className="text-xl flex-shrink-0">{selectedOption.icon}</span> |
| 99 | + )} |
| 100 | + <span |
| 101 | + className={cn( |
| 102 | + 'font-medium text-sm truncate', |
| 103 | + selectedOption ? 'text-[rgb(var(--foreground))]' : 'text-[rgb(var(--muted))]' |
| 104 | + )} |
| 105 | + > |
| 106 | + {selectedOption?.label ?? placeholder} |
| 107 | + </span> |
| 108 | + </span> |
| 109 | + <ChevronDown |
| 110 | + className={cn( |
| 111 | + 'h-4 w-4 flex-shrink-0 text-[rgb(var(--muted))] group-hover:text-[rgb(var(--foreground))] transition-all duration-200', |
| 112 | + open && 'rotate-180' |
| 113 | + )} |
| 114 | + /> |
| 115 | + </DropdownMenuTrigger> |
| 116 | + |
| 117 | + <DropdownMenuContent align="start" className="w-full min-w-[16rem]"> |
| 118 | + <div className="p-1.5 border-b border-[rgb(var(--border-subtle))]"> |
| 119 | + <SearchField |
| 120 | + value={filter} |
| 121 | + onChange={(event) => setFilter(event.target.value)} |
| 122 | + onClear={() => setFilter('')} |
| 123 | + placeholder="Search…" |
| 124 | + autoFocus |
| 125 | + data-testid={testId ? `${testId}-search` : undefined} |
| 126 | + onClick={(event) => event.stopPropagation()} |
| 127 | + onKeyDown={(event) => event.stopPropagation()} |
| 128 | + /> |
| 129 | + </div> |
| 130 | + |
| 131 | + <div className="p-1.5 max-h-64 overflow-y-auto"> |
| 132 | + {filteredOptions.length === 0 ? ( |
| 133 | + <div className="text-center py-4 text-sm text-[rgb(var(--muted))]">No matches</div> |
| 134 | + ) : ( |
| 135 | + filteredOptions.map((option) => { |
| 136 | + const isSelected = option.value === value; |
| 137 | + return ( |
| 138 | + <button |
| 139 | + key={option.value} |
| 140 | + type="button" |
| 141 | + role="menuitem" |
| 142 | + onClick={() => handleSelect(option.value)} |
| 143 | + className={cn( |
| 144 | + 'w-full flex items-center justify-between px-3 py-2.5 rounded-lg text-left transition-all duration-150', |
| 145 | + isSelected |
| 146 | + ? 'bg-[rgb(var(--primary))/12] text-[rgb(var(--primary))]' |
| 147 | + : 'hover:bg-[rgb(var(--surface-hover))]' |
| 148 | + )} |
| 149 | + data-testid={testId ? `${testId}-option-${option.value}` : undefined} |
| 150 | + > |
| 151 | + <span className="flex items-center gap-3 min-w-0"> |
| 152 | + {option.icon && <span className="text-xl flex-shrink-0">{option.icon}</span>} |
| 153 | + <span className="font-medium text-sm truncate">{option.label}</span> |
| 154 | + </span> |
| 155 | + {isSelected && <Check className="h-4 w-4 flex-shrink-0" />} |
| 156 | + </button> |
| 157 | + ); |
| 158 | + }) |
| 159 | + )} |
| 160 | + </div> |
| 161 | + |
| 162 | + {onCreateNew && ( |
| 163 | + <> |
| 164 | + <DropdownMenuSeparator /> |
| 165 | + <div className="p-1.5"> |
| 166 | + <button |
| 167 | + type="button" |
| 168 | + role="menuitem" |
| 169 | + onClick={handleCreateNew} |
| 170 | + className="w-full flex items-center gap-3 px-3 py-2.5 rounded-lg text-sm text-[rgb(var(--muted))] hover:bg-[rgb(var(--surface-hover))] hover:text-[rgb(var(--foreground))] transition-all duration-150" |
| 171 | + data-testid={testId ? `${testId}-create-new` : undefined} |
| 172 | + > |
| 173 | + <Plus className="h-4 w-4" /> |
| 174 | + New… |
| 175 | + </button> |
| 176 | + </div> |
| 177 | + </> |
| 178 | + )} |
| 179 | + </DropdownMenuContent> |
| 180 | + </DropdownMenu> |
| 181 | + ); |
| 182 | +} |
0 commit comments