|
1 | | -import { useDebounceEffect, useField } from '@siberiacancode/reactuse'; |
| 1 | +import { |
| 2 | + useAsync, |
| 3 | + useClickOutside, |
| 4 | + useDebounceEffect, |
| 5 | + useDisclosure |
| 6 | +} from '@siberiacancode/reactuse'; |
| 7 | +import { CheckIcon, ChevronDownIcon, Loader2Icon, SearchIcon } from 'lucide-react'; |
2 | 8 | import { useState } from 'react'; |
3 | 9 |
|
| 10 | +import { cn } from '@/utils/lib'; |
| 11 | + |
| 12 | +interface Animal { |
| 13 | + emoji: string; |
| 14 | + name: string; |
| 15 | +} |
| 16 | + |
| 17 | +const ANIMALS: Animal[] = [ |
| 18 | + { emoji: '🐶', name: 'Dog' }, |
| 19 | + { emoji: '🐱', name: 'Cat' }, |
| 20 | + { emoji: '🐸', name: 'Frog' }, |
| 21 | + { emoji: '🐙', name: 'Octopus' }, |
| 22 | + { emoji: '🦀', name: 'Crab' }, |
| 23 | + { emoji: '🐠', name: 'Fish' }, |
| 24 | + { emoji: '🐧', name: 'Penguin' }, |
| 25 | + { emoji: '🦅', name: 'Eagle' }, |
| 26 | + { emoji: '🦆', name: 'Duck' } |
| 27 | +]; |
| 28 | + |
| 29 | +const searchAnimals = (query: string): Promise<Animal[]> => |
| 30 | + new Promise((resolve) => { |
| 31 | + setTimeout( |
| 32 | + () => |
| 33 | + resolve( |
| 34 | + ANIMALS.filter((animal) => animal.name.toLowerCase().includes(query.toLowerCase())) |
| 35 | + ), |
| 36 | + 300 |
| 37 | + ); |
| 38 | + }); |
| 39 | + |
4 | 40 | const Demo = () => { |
5 | | - const inputField = useField(); |
6 | | - const [list, setList] = useState<string[]>([]); |
| 41 | + const [search, setSearch] = useState(''); |
| 42 | + const [debouncedQuery, setDebouncedQuery] = useState(''); |
| 43 | + const [selected, setSelected] = useState<Animal | null>(null); |
| 44 | + |
| 45 | + const dropdown = useDisclosure(); |
| 46 | + const dropdownRef = useClickOutside<HTMLDivElement>(() => dropdown.close()); |
7 | 47 |
|
8 | | - const inputValue = inputField.watch(); |
| 48 | + const animalsQuery = useAsync(() => searchAnimals(debouncedQuery), [debouncedQuery]); |
9 | 49 |
|
10 | 50 | useDebounceEffect( |
11 | 51 | () => { |
12 | | - setList((currentList) => [...currentList, inputValue]); |
| 52 | + setDebouncedQuery(search); |
13 | 53 | }, |
14 | 54 | 500, |
15 | | - [inputValue] |
| 55 | + [search] |
16 | 56 | ); |
17 | 57 |
|
| 58 | + const onSelect = (animal: Animal) => { |
| 59 | + setSelected(animal); |
| 60 | + dropdown.close(); |
| 61 | + setSearch(''); |
| 62 | + setDebouncedQuery(''); |
| 63 | + }; |
| 64 | + |
| 65 | + const results = animalsQuery.data ?? ANIMALS; |
| 66 | + const isLoading = animalsQuery.isLoading || search !== debouncedQuery; |
| 67 | + |
18 | 68 | return ( |
19 | | - <> |
20 | | - <div>Write to see the debounce effect</div> |
21 | | - <input {...inputField.register()} /> |
22 | | - <ul className='text-sm'> |
23 | | - {list.map((item) => ( |
24 | | - <li key={item}>{item}</li> |
25 | | - ))} |
26 | | - </ul> |
27 | | - </> |
| 69 | + <section className='flex w-full max-w-sm flex-col gap-4 p-4'> |
| 70 | + <div className='flex flex-col gap-2'> |
| 71 | + <h3>Find your spirit animal</h3> |
| 72 | + <p className='text-muted-foreground text-sm'> |
| 73 | + The search waits <code>delay</code> after you stop typing before firing a request — no |
| 74 | + spam, no jank. |
| 75 | + </p> |
| 76 | + </div> |
| 77 | + |
| 78 | + <div ref={dropdownRef} className='relative'> |
| 79 | + <div |
| 80 | + className='border-input bg-background flex h-10 w-full cursor-pointer items-center justify-between rounded-lg border px-3 text-sm transition-colors' |
| 81 | + onClick={() => dropdown.toggle()} |
| 82 | + > |
| 83 | + {selected && ( |
| 84 | + <span className='flex items-center gap-2'> |
| 85 | + <span className='text-lg'>{selected.emoji}</span> |
| 86 | + <span>{selected.name}</span> |
| 87 | + </span> |
| 88 | + )} |
| 89 | + {!selected && <span className='text-muted-foreground'>Select an animal...</span>} |
| 90 | + <ChevronDownIcon |
| 91 | + className={cn('text-muted-foreground size-4', dropdown.opened && 'rotate-180')} |
| 92 | + /> |
| 93 | + </div> |
| 94 | + |
| 95 | + {dropdown.opened && ( |
| 96 | + <div className='bg-popover text-popover-foreground absolute top-full right-0 left-0 z-50 mt-2 overflow-hidden rounded-lg border shadow-md'> |
| 97 | + <div className='relative'> |
| 98 | + <SearchIcon className='text-muted-foreground pointer-events-none absolute top-1/2 left-3 size-4 -translate-y-1/2' /> |
| 99 | + <input |
| 100 | + autoFocus |
| 101 | + className='h-10! w-full border-0! bg-inherit! pl-9! focus-visible:ring-0!' |
| 102 | + placeholder='Search animals...' |
| 103 | + type='text' |
| 104 | + value={search} |
| 105 | + onChange={(event) => setSearch(event.target.value)} |
| 106 | + /> |
| 107 | + {isLoading && ( |
| 108 | + <Loader2Icon className='text-muted-foreground pointer-events-none absolute top-1/2 right-3 size-4 -translate-y-1/2 animate-spin' /> |
| 109 | + )} |
| 110 | + </div> |
| 111 | + |
| 112 | + <div className='no-scrollbar max-h-60 overflow-y-auto p-1'> |
| 113 | + {!results.length && !isLoading && ( |
| 114 | + <p className='text-muted-foreground py-6 text-center text-sm'>No animals found</p> |
| 115 | + )} |
| 116 | + |
| 117 | + {results.map((animal) => { |
| 118 | + const isSelected = selected?.name === animal.name; |
| 119 | + return ( |
| 120 | + <div |
| 121 | + key={animal.name} |
| 122 | + className='hover:bg-accent hover:text-accent-foreground flex cursor-pointer items-center justify-between gap-3 rounded-md px-2 py-1.5 text-sm' |
| 123 | + onClick={() => onSelect(animal)} |
| 124 | + > |
| 125 | + <span className='flex items-center gap-2'> |
| 126 | + <span className='text-lg'>{animal.emoji}</span> |
| 127 | + <span>{animal.name}</span> |
| 128 | + </span> |
| 129 | + {isSelected && <CheckIcon className='size-4' />} |
| 130 | + </div> |
| 131 | + ); |
| 132 | + })} |
| 133 | + </div> |
| 134 | + </div> |
| 135 | + )} |
| 136 | + </div> |
| 137 | + </section> |
28 | 138 | ); |
29 | 139 | }; |
30 | 140 |
|
|
0 commit comments