Accordion.jsx
1 month ago
Card.jsx
1 month ago
CardSkeleton.jsx
1 month ago
FileUploaderField.jsx
1 month ago
SearchComboBox.jsx
1 month ago
SearchComboBox.jsx
70 lines
| 1 | /** |
| 2 | * External Dependencies |
| 3 | */ |
| 4 | import { Search } from 'lucide-react'; |
| 5 | /** |
| 6 | * Internal Dependencies |
| 7 | */ |
| 8 | import { clsx } from '@admin/utils'; |
| 9 | import { useSearchCombobox } from '@admin/hooks/useSearchCombobox'; |
| 10 | |
| 11 | export function SearchComboBox( { |
| 12 | id, |
| 13 | placeholder = 'Search…', |
| 14 | endpoint, |
| 15 | formatSuggestion, |
| 16 | onSelect, |
| 17 | debounceDelay = 300, |
| 18 | } ) { |
| 19 | const { |
| 20 | suggestions, |
| 21 | isOpen, |
| 22 | activeIndex, |
| 23 | inputProps, |
| 24 | listProps, |
| 25 | handleSelect, |
| 26 | } = useSearchCombobox( { endpoint, onSelect, delay: debounceDelay } ); |
| 27 | |
| 28 | return ( |
| 29 | <div className="relative w-full"> |
| 30 | <Search className="absolute top-1/2 right-4 -translate-y-1/2 stroke-gray-600 size-4.5" /> |
| 31 | <input |
| 32 | id={ id } |
| 33 | placeholder={ placeholder } |
| 34 | className="border border-border rounded-lg px-4 py-3 w-full leading-0 focus:shadow-none placeholder:text-gray-400" |
| 35 | { ...inputProps } |
| 36 | /> |
| 37 | |
| 38 | { isOpen && ( |
| 39 | <ul |
| 40 | className="absolute w-full bg-white text-left border border-border max-h-60 overflow-y-auto m-0 mt-0.75 rounded-lg shadow-lg py-2 advads-scrollbar" |
| 41 | { ...listProps } |
| 42 | > |
| 43 | { suggestions.length === 0 ? ( |
| 44 | <li className="p-2.5 text-gray-400 cursor-default"> |
| 45 | No results found |
| 46 | </li> |
| 47 | ) : ( |
| 48 | suggestions.map( ( suggestion, index ) => ( |
| 49 | <li key={ index } id={ `suggestion-${ index }` }> |
| 50 | <button |
| 51 | type="button" |
| 52 | onClick={ () => handleSelect( suggestion ) } |
| 53 | className={ clsx( |
| 54 | 'flex w-full bg-transparent border-0! cursor-pointer px-4 py-2 text-sm transition-colors text-left', |
| 55 | index === activeIndex |
| 56 | ? 'bg-gray-100' |
| 57 | : '' |
| 58 | ) } |
| 59 | > |
| 60 | { formatSuggestion( suggestion ) } |
| 61 | </button> |
| 62 | </li> |
| 63 | ) ) |
| 64 | ) } |
| 65 | </ul> |
| 66 | ) } |
| 67 | </div> |
| 68 | ); |
| 69 | } |
| 70 |