| 1 | "use client"; |
| 2 | |
| 3 | import { useState, useMemo |
| 4 | import { createPortal } |
| 5 | import { ChevronDownIcon, SearchIcon |
| 6 | import { cn } |
| 7 | |
| 8 | export interface GroupedMultiSelectItem |
| 9 | id: string; |
| 10 | label: string; |
| 11 | } |
| 12 | |
| 13 | export interface GroupedMultiSelectGroup |
| 14 | id: string; |
| 15 | label: string; |
| 16 | items: GroupedMultiSelectItem |
| 17 | } |
| 18 | |
| 19 | export function GroupedMultiSelect({ |
| 20 | value, |
| 21 | onChange, |
| 22 | groups, |
| 23 | placeholder = "Select…" |
| 24 | searchPlaceholder = "Search…" |
| 25 | noResultsText = "", |
| 26 | clearAllText, |
| 27 | className, |
| 28 | }: { |
| 29 | value: string |
| 30 | onChange: (values |
| 31 | groups: GroupedMultiSelectGroup |
| 32 | placeholder?: string |
| 33 | searchPlaceholder?: string |
| 34 | noResultsText?: string |
| 35 | clearAllText?: ( |
| 36 | className?: string |
| 37 | }) { |
| 38 | const [open, |
| 39 | const [query, |
| 40 | const [rect, |
| 41 | const triggerRef = |
| 42 | const popupRef = |
| 43 | |
| 44 | const allItems = |
| 45 | |
| 46 | function openDropdown() { |
| 47 | if (triggerRef.current |
| 48 | setOpen(true); |
| 49 | } |
| 50 | |
| 51 | function close() { |
| 52 | setOpen(false); |
| 53 | setQuery(""); |
| 54 | } |
| 55 | |
| 56 | useEffect(() => |
| 57 | if (!open) |
| 58 | function handleScroll(e |
| 59 | if (popupRef.current |
| 60 | close(); |
| 61 | } |
| 62 | window.addEventListener("scroll |
| 63 | window.addEventListener("resize |
| 64 | return () => |
| 65 | window.removeEventListener("scroll |
| 66 | window.removeEventListener("resize |
| 67 | }; |
| 68 | }, [open]); |
| 69 | |
| 70 | const filtered = |
| 71 | const q = |
| 72 | if (!q) |
| 73 | return groups |
| 74 | .map((g) |
| 75 | ...g, |
| 76 | items: g.items |
| 77 | (item) => |
| 78 | ), |
| 79 | })) |
| 80 | .filter((g) |
| 81 | }, [groups, |
| 82 | |
| 83 | function toggle(id |
| 84 | onChange(value.includes |
| 85 | } |
| 86 | |
| 87 | function toggleGroup(group |
| 88 | const ids = |
| 89 | const allChecked = |
| 90 | onChange(allChecked ? |
| 91 | } |
| 92 | |
| 93 | const selectedLabels = |
| 94 | .map((id) |
| 95 | .filter(Boolean) |
| 96 | |
| 97 | const triggerText = |
| 98 | ? null |
| 99 | : selectedLabels.length <= |
| 100 | ? selectedLabels.join( |
| 101 | : `${selectedLabels.slice |
| 102 | |
| 103 | const dropdown = |
| 104 | ? createPortal( |
| 105 | <> |
| 106 | <div className="fixed inset-0 z-[200] |
| 107 | <div |
| 108 | ref={popupRef} |
| 109 | className="fixed z-[201] overflow-hidden rounded-xl border border-border bg-card shadow-lg" |
| 110 | style={{ top: |
| 111 | > |
| 112 | <div className="border-b border-border px-2 py-2 |
| 113 | <div className="flex items-center gap-2 rounded-lg border border-border bg-background px-2 py-1.5 |
| 114 | <SearchIcon className=" |
| 115 | <input |
| 116 | autoFocus |
| 117 | value={query} |
| 118 | onChange={(e) |
| 119 | placeholder={searchPlaceholder} |
| 120 | className="min-w-0 flex-1 bg-transparent text-xs text-foreground outline-none placeholder:text-muted-foreground" |
| 121 | /> |
| 122 | </div> |
| 123 | </div> |
| 124 | |
| 125 | <div className="max-h-56 overflow-y-auto p-1 |
| 126 | {filtered.length === |
| 127 | <p className="py-4 text-center text-xs text-muted-foreground |
| 128 | ) : ( |
| 129 | filtered.map((group |
| 130 | const groupChecked = |
| 131 | return ( |
| 132 | <div key={group |
| 133 | <button |
| 134 | type="button" |
| 135 | onClick={() => |
| 136 | className="flex w-full items-center gap-2 rounded-lg px-2 py-1.5 text-left hover:bg-muted/60" |
| 137 | > |
| 138 | <div className={cn |
| 139 | "flex h-3.5 w-3.5 shrink-0 items-center justify-center rounded border transition-colors", |
| 140 | groupChecked ? "border-primary bg-primary" |
| 141 | )}> |
| 142 | {groupChecked && <CheckIcon |
| 143 | </div> |
| 144 | <span className="text-[11px] font-semibold uppercase tracking-wide text-muted-foreground |
| 145 | {group.label} |
| 146 | </span> |
| 147 | </button> |
| 148 | {group.items.map |
| 149 | const checked = |
| 150 | return ( |
| 151 | <button |
| 152 | key={item.id |
| 153 | type="button" |
| 154 | onClick={() => |
| 155 | className="flex w-full items-center gap-2 rounded-lg py-1.5 pr-2 pl-6 text-left hover:bg-muted/60" |
| 156 | > |
| 157 | <div className={cn |
| 158 | "flex h-3.5 w-3.5 shrink-0 items-center justify-center rounded border transition-colors", |
| 159 | checked ? "border-primary bg-primary" |
| 160 | )}> |
| 161 | {checked && <CheckIcon |
| 162 | </div> |
| 163 | <span className="text-xs text-foreground |
| 164 | </button> |
| 165 | ); |
| 166 | })} |
| 167 | </div> |
| 168 | ); |
| 169 | }) |
| 170 | )} |
| 171 | </div> |
| 172 | |
| 173 | {value.length > |
| 174 | <div className="border-t border-border px-3 py-2 |
| 175 | <button |
| 176 | type="button" |
| 177 | onClick={() => |
| 178 | className="text-[11px] text-muted-foreground hover:text-destructive" |
| 179 | > |
| 180 | {clearAllText ? clearAllText |
| 181 | </button> |
| 182 | </div> |
| 183 | )} |
| 184 | </div> |
| 185 | </>, |
| 186 | document.body, |
| 187 | ) |
| 188 | : null; |
| 189 | |
| 190 | return ( |
| 191 | <> |
| 192 | <button |
| 193 | ref={triggerRef} |
| 194 | type="button" |
| 195 | onClick={openDropdown} |
| 196 | className={cn( |
| 197 | "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 transition-colors hover:bg-muted/40", |
| 198 | open && "border-ring ring-2 ring-ring" |
| 199 | className, |
| 200 | )} |
| 201 | > |
| 202 | <span className="truncate text-left |
| 203 | {triggerText ?? <span |
| 204 | </span> |
| 205 | <ChevronDownIcon className={ |
| 206 | </button> |
| 207 | {dropdown} |
| 208 | </> |
| 209 | ); |
| 210 | } |