| 1 | "use client"; |
| 2 | |
| 3 | import { useState, useMemo |
| 4 | import { Select as |
| 5 | import { cn } |
| 6 | import { CheckIcon, ChevronDownIcon |
| 7 | |
| 8 | export interface SearchSelectItem |
| 9 | value: string; |
| 10 | label: string; |
| 11 | } |
| 12 | |
| 13 | export function SearchSelect({ |
| 14 | value, |
| 15 | onValueChange, |
| 16 | items, |
| 17 | placeholder = "Select…" |
| 18 | searchPlaceholder = "Search…" |
| 19 | noResultsText = "", |
| 20 | className, |
| 21 | }: { |
| 22 | value: string |
| 23 | onValueChange: (v |
| 24 | items: SearchSelectItem |
| 25 | placeholder?: string |
| 26 | searchPlaceholder?: string |
| 27 | noResultsText?: string |
| 28 | className?: string |
| 29 | }) { |
| 30 | const [query, |
| 31 | |
| 32 | const filtered = |
| 33 | () => |
| 34 | query.trim() |
| 35 | ? items.filter( |
| 36 | (item) => |
| 37 | item.value.toLowerCase |
| 38 | item.label.toLowerCase |
| 39 | ) |
| 40 | : items, |
| 41 | [items, query] |
| 42 | ); |
| 43 | |
| 44 | return ( |
| 45 | <SelectPrimitive.Root |
| 46 | value={value} |
| 47 | onValueChange={onValueChange} |
| 48 | onOpenChange={(open) |
| 49 | if (!open) setQuery |
| 50 | }} |
| 51 | > |
| 52 | <SelectPrimitive.Trigger |
| 53 | className={cn( |
| 54 | "flex h-8 w-full items-center justify-between gap-2 rounded-lg border border-border bg-background px-3 py-1.5 text-xs text-foreground outline-none transition-colors hover:bg-muted/40 focus-visible:ring-2 focus-visible:ring-ring data-[popup-open]:border-ring disabled:cursor-not-allowed disabled:opacity-50", |
| 55 | className, |
| 56 | )} |
| 57 | > |
| 58 | <SelectPrimitive.Value placeholder={ |
| 59 | <SelectPrimitive.Icon> |
| 60 | <ChevronDownIcon className=" |
| 61 | </SelectPrimitive.Icon> |
| 62 | </SelectPrimitive.Trigger> |
| 63 | <SelectPrimitive.Portal> |
| 64 | <SelectPrimitive.Positioner |
| 65 | className="isolate z-50 outline-none" |
| 66 | align="start" |
| 67 | sideOffset={4} |
| 68 | > |
| 69 | <SelectPrimitive.Popup className=" |
| 70 | <div className="border-b border-border px-2 py-2 |
| 71 | <div className="flex items-center gap-2 rounded-lg border border-border bg-background px-2 py-1.5 |
| 72 | <SearchIcon className=" |
| 73 | <input |
| 74 | value={query} |
| 75 | onChange={(e) |
| 76 | placeholder={searchPlaceholder} |
| 77 | className="min-w-0 flex-1 bg-transparent text-xs text-foreground outline-none placeholder:text-muted-foreground" |
| 78 | onKeyDown={(e) |
| 79 | /> |
| 80 | </div> |
| 81 | </div> |
| 82 | <div className="max-h-52 overflow-y-auto p-1 |
| 83 | {filtered.length === |
| 84 | <p className="px-2 py-4 text-center text-xs text-muted-foreground |
| 85 | {noResultsText} |
| 86 | </p> |
| 87 | ) : ( |
| 88 | filtered.map((item |
| 89 | <SelectPrimitive.Item |
| 90 | key={item.value |
| 91 | value={item.value |
| 92 | className="relative flex cursor-default items-center gap-2 rounded-lg py-1.5 pr-8 pl-2 text-xs outline-none select-none focus:bg-muted data-disabled:pointer-events-none data-disabled:opacity-50" |
| 93 | > |
| 94 | <span className="pointer-events-none absolute right-2 flex items-center justify-center |
| 95 | <SelectPrimitive.ItemIndicator> |
| 96 | <CheckIcon className=" |
| 97 | </SelectPrimitive.ItemIndicator> |
| 98 | </span> |
| 99 | <SelectPrimitive.ItemText> |
| 100 | <span className="font-mono font-medium |
| 101 | </SelectPrimitive.ItemText> |
| 102 | <span className="truncate text-muted-foreground |
| 103 | </SelectPrimitive.Item> |
| 104 | )) |
| 105 | )} |
| 106 | </div> |
| 107 | </SelectPrimitive.Popup> |
| 108 | </SelectPrimitive.Positioner> |
| 109 | </SelectPrimitive.Portal> |
| 110 | </SelectPrimitive.Root> |
| 111 | ); |
| 112 | } |