import '@reach/combobox/styles.css'; import { useState, ChangeEvent } from 'react'; import { Combobox, ComboboxInput, ComboboxList, ComboboxOption, ComboboxPopover, } from '@reach/combobox'; import clsx from 'clsx'; import { useDebounce } from '@/react/hooks/useDebounce'; import { Option } from '@@/form-components/PortainerSelect'; import styles from './AutocompleteSelect.module.css'; export function AutocompleteSelect({ value, onChange, placeholder, searchResults, readOnly, inputId, }: { value: string; /** * onChange is called whenever the input is changed or an option is selected * * when the input is changed, the call is debounced */ onChange(value: string): void; placeholder?: string; searchResults?: Option[]; readOnly?: boolean; inputId: string; }) { const [searchTerm, setSearchTerm] = useDebounce(value, onChange); const [selected, setSelected] = useState(false); return ( {!selected && searchResults && searchResults.length > 0 && ( {searchResults.map((option: Option) => ( ))} )} ); function handleChange(e: ChangeEvent) { setSearchTerm(e.target.value); setSelected(false); } function onSelect(value: string) { onChange(value); setSelected(true); } }