| 1 | "use client"; |
| 2 | |
| 3 | import React from "react" |
| 4 | import { Check, ChevronsUpDown |
| 5 | import { currencies as |
| 6 | import { useVirtualizer } |
| 7 | |
| 8 | import { cn } |
| 9 | import { allCurrencies } |
| 10 | import { Button } |
| 11 | import { Input } |
| 12 | import { |
| 13 | Popover, |
| 14 | PopoverContent, |
| 15 | PopoverTrigger, |
| 16 | } from "@/components/ui/popover"; |
| 17 | |
| 18 | export interface Currency |
| 19 | code: string; |
| 20 | decimals: number; |
| 21 | name: string; |
| 22 | number: string; |
| 23 | symbol?: string |
| 24 | } |
| 25 | |
| 26 | interface CurrencySelectProps { |
| 27 | value?: string |
| 28 | onValueChange?: (value |
| 29 | onCurrencySelect?: (currency |
| 30 | name: string; |
| 31 | placeholder?: string |
| 32 | currencies?: "custom |
| 33 | variant?: "default |
| 34 | valid?: boolean |
| 35 | disabled?: boolean |
| 36 | className?: string |
| 37 | } |
| 38 | |
| 39 | const CurrencySelect = React |
| 40 | ( |
| 41 | { |
| 42 | value, |
| 43 | onValueChange, |
| 44 | onCurrencySelect, |
| 45 | name, |
| 46 | placeholder = "Select currency" |
| 47 | currencies = "all" |
| 48 | variant = "default" |
| 49 | valid = true, |
| 50 | disabled, |
| 51 | className, |
| 52 | }, |
| 53 | ref, |
| 54 | ) => { |
| 55 | const [open, |
| 56 | const [search, |
| 57 | |
| 58 | const uniqueCurrencies = |
| 59 | const currencyMap = |
| 60 | |
| 61 | AllCurrencies.all.forEach |
| 62 | if (currency.code |
| 63 | const shouldInclude = |
| 64 | currencies === "custom" |
| 65 | ? allCurrencies.includes( |
| 66 | : !allCurrencies.includes |
| 67 | |
| 68 | if (shouldInclude) { |
| 69 | currencyMap.set(currency |
| 70 | code: currency.code |
| 71 | name: currency.code |
| 72 | symbol: currency.symbol |
| 73 | decimals: currency.decimals |
| 74 | number: currency.number |
| 75 | }); |
| 76 | } |
| 77 | } |
| 78 | }); |
| 79 | |
| 80 | return Array.from |
| 81 | a.name.localeCompare |
| 82 | ); |
| 83 | }, [currencies]); |
| 84 | |
| 85 | const filtered = |
| 86 | const query = |
| 87 | if (!query) |
| 88 | return uniqueCurrencies.filter |
| 89 | (curr) => |
| 90 | curr.code.toLowerCase |
| 91 | curr.name.toLowerCase |
| 92 | (curr.symbol ?? |
| 93 | ); |
| 94 | }, [uniqueCurrencies, |
| 95 | |
| 96 | const selected = |
| 97 | () => uniqueCurrencies. |
| 98 | [uniqueCurrencies, value] |
| 99 | ); |
| 100 | |
| 101 | const handleSelect = |
| 102 | onValueChange?.(currency.code |
| 103 | onCurrencySelect?.(currency); |
| 104 | setOpen(false); |
| 105 | setSearch(""); |
| 106 | }; |
| 107 | |
| 108 | return ( |
| 109 | <Popover |
| 110 | open={open} |
| 111 | onOpenChange={(o) |
| 112 | setOpen(o); |
| 113 | if (!o) setSearch |
| 114 | }} |
| 115 | > |
| 116 | <PopoverTrigger asChild> |
| 117 | <Button |
| 118 | ref={ref} |
| 119 | type="button" |
| 120 | variant="outline" |
| 121 | role="combobox" |
| 122 | aria-expanded={open} |
| 123 | disabled={disabled} |
| 124 | data-valid={valid} |
| 125 | name={name} |
| 126 | className={cn( |
| 127 | "w-full justify-between font-normal h-9", |
| 128 | |
| 129 | !selected && "text-muted-foreground |
| 130 | className, |
| 131 | )} |
| 132 | > |
| 133 | {selected ? ( |
| 134 | variant === "small" |
| 135 | <span>{selected.code |
| 136 | ) : ( |
| 137 | <span className="flex min-w-0 items-center gap-2 |
| 138 | <span className="font-semibold |
| 139 | <span className="truncate text-muted-foreground |
| 140 | {selected.name} |
| 141 | </span> |
| 142 | </span> |
| 143 | ) |
| 144 | ) : ( |
| 145 | <span>{placeholder}</ |
| 146 | )} |
| 147 | <ChevronsUpDown className=" |
| 148 | </Button> |
| 149 | </PopoverTrigger> |
| 150 | <PopoverContent |
| 151 | className="w-[var(--radix-popover-trigger-width)] min-w-[260px] p-0" |
| 152 | align="start" |
| 153 | > |
| 154 | <div className="p-2 border-b |
| 155 | <div className="relative |
| 156 | <Search className=" |
| 157 | <Input |
| 158 | placeholder="Search currencies..." |
| 159 | value={search} |
| 160 | onChange={(e) |
| 161 | className="h-8 pl-8" |
| 162 | autoFocus |
| 163 | /> |
| 164 | </div> |
| 165 | </div> |
| 166 | {filtered.length === |
| 167 | <div className="py-6 text-center text-sm text-muted-foreground |
| 168 | No currencies found. |
| 169 | </div> |
| 170 | ) : ( |
| 171 | <VirtualizedCurrencyList |
| 172 | currencies={filtered} |
| 173 | selectedValue={value} |
| 174 | onSelect={handleSelect} |
| 175 | /> |
| 176 | )} |
| 177 | </PopoverContent> |
| 178 | </Popover> |
| 179 | ); |
| 180 | }, |
| 181 | ); |
| 182 | |
| 183 | const ITEM_HEIGHT = 36 |
| 184 | const MAX_HEIGHT = 300 |
| 185 | |
| 186 | const VirtualizedCurrencyList = React |
| 187 | currencies: Currency |
| 188 | selectedValue: string |
| 189 | onSelect: (currency |
| 190 | }>(({ currencies, |
| 191 | const parentRef = |
| 192 | |
| 193 | const virtualizer = |
| 194 | count: currencies.length, |
| 195 | getScrollElement: () => |
| 196 | estimateSize: () => |
| 197 | overscan: 8, |
| 198 | }); |
| 199 | |
| 200 | return ( |
| 201 | <div |
| 202 | ref={parentRef} |
| 203 | className="overflow-y-auto overscroll-contain p-1" |
| 204 | style={{ |
| 205 | height: `${Math. |
| 206 | }} |
| 207 | onWheel={(e) |
| 208 | > |
| 209 | <div |
| 210 | style={{ |
| 211 | height: `${virtualizer. |
| 212 | width: "100%", |
| 213 | position: "relative", |
| 214 | }} |
| 215 | > |
| 216 | {virtualizer.getVirtualItems(). |
| 217 | const currency = |
| 218 | if (!currency) return |
| 219 | const isSelected = |
| 220 | return ( |
| 221 | <button |
| 222 | type="button" |
| 223 | key={virtualItem.key |
| 224 | onClick={() => |
| 225 | style={{ |
| 226 | position: "absolute", |
| 227 | top: 0, |
| 228 | left: 0, |
| 229 | width: "100%", |
| 230 | height: `${virtualItem. |
| 231 | transform: `translateY(${virtualItem |
| 232 | }} |
| 233 | className={cn( |
| 234 | "flex w-full cursor-pointer items-center gap-2 rounded-sm px-2 text-sm outline-none transition-colors hover:bg-accent hover:text-accent-foreground focus:bg-accent focus:text-accent-foreground", |
| 235 | isSelected && "bg-accent text-accent-foreground" |
| 236 | )} |
| 237 | > |
| 238 | <Check |
| 239 | className={cn( |
| 240 | "h-4 w-4 shrink-0", |
| 241 | isSelected ? "opacity-100" |
| 242 | )} |
| 243 | /> |
| 244 | <span className="w-12 shrink-0 text-left text-xs font-semibold text-muted-foreground truncate |
| 245 | {currency.code} |
| 246 | </span> |
| 247 | <span className="flex-1 truncate text-left |
| 248 | <span className="ml-auto shrink-0 text-xs text-muted-foreground |
| 249 | {currency.symbol} |
| 250 | </span> |
| 251 | </button> |
| 252 | ); |
| 253 | })} |
| 254 | </div> |
| 255 | </div> |
| 256 | ); |
| 257 | }); |
| 258 | |
| 259 | VirtualizedCurrencyList.displayName = " |
| 260 | CurrencySelect.displayName = " |
| 261 | |
| 262 | export { CurrencySelect }; |