"use client"; import { useState, useRef, useEffect, Fragment } from 'react'; import { Combobox, Transition } from '@headlessui/react'; import { ChevronUpDownIcon, CheckIcon, MagnifyingGlassIcon } from '@heroicons/react/24/outline'; interface Option { id: string; name: string; subtitle?: string; } interface SearchableSelectProps { options: Option[]; value: string; onChange: (value: string | null) => void; placeholder?: string; emptyText?: string; label?: string; helperText?: string; } export default function SearchableSelect({ options, value, onChange, placeholder = 'Selecione...', emptyText = 'Nenhum resultado encontrado', label, helperText, }: SearchableSelectProps) { const [query, setQuery] = useState(''); const selectedOption = options.find(opt => opt.id === value); const filteredOptions = query === '' ? options : options.filter((option) => option.name.toLowerCase().includes(query.toLowerCase()) || option.subtitle?.toLowerCase().includes(query.toLowerCase()) ); return (
{label && ( )}
selectedOption ? `${selectedOption.name}${selectedOption.subtitle ? ` (${selectedOption.subtitle})` : ''}` : ''} onChange={(event) => setQuery(event.target.value)} placeholder={placeholder} />
setQuery('')} > {filteredOptions.length === 0 ? (
{emptyText}
) : ( <> {value && ( `relative cursor-pointer select-none py-2 pl-10 pr-4 ${active ? 'bg-brand-50 dark:bg-brand-900/20 text-brand-700 dark:text-brand-400' : 'text-zinc-700 dark:text-zinc-300' }` } > {({ selected }) => ( <> {placeholder || 'Nenhum'} {selected && ( )} )} )} {filteredOptions.map((option) => ( `relative cursor-pointer select-none py-2 pl-10 pr-4 ${active ? 'bg-brand-50 dark:bg-brand-900/20 text-brand-700 dark:text-brand-400' : 'text-zinc-700 dark:text-zinc-300' }` } value={option.id} > {({ selected, active }) => ( <>
{option.name} {option.subtitle && ( {option.subtitle} )}
{selected && ( )} )}
))} )}
{helperText && (

{helperText}

)}
); }