import React, {useState, useCallback} from 'react' import {useCombobox} from 'downshift' import classNames from 'classnames' import debounce from 'debounce-promise' import { ChevronDownIcon } from '@heroicons/react/solid' function selectInputText(e) { e.target.select() } function ChevronDown() { return ( ) } function Spinner() { return ( ) } export default function SearchSelect(props) { const [items, setItems] = useState([]) const [loading, setLoading] = useState(false) function fetchOptions({inputValue, isOpen}) { setLoading(isOpen) return props.fetchOptions(inputValue).then((loadedItems) => { setLoading(false) setItems(loadedItems) }) } const debouncedFetchOptions = useCallback(debounce(fetchOptions, 200), []) const { isOpen, inputValue, getToggleButtonProps, getLabelProps, getMenuProps, getInputProps, getComboboxProps, highlightedIndex, getItemProps, selectItem, closeMenu, setInputValue } = useCombobox({ items: items, onInputValueChange: (changes) => { debouncedFetchOptions(changes) props.onInput(changes.inputValue) }, initialSelectedItem: props.initialSelectedItem, onIsOpenChange: ({isOpen, inputValue}) => { if (isOpen) { fetchOptions({inputValue, isOpen}) } } }) function keydown(e) { if (e.ctrlKey || e.metaKey || e.shiftKey || e.altKey || e.isComposing || e.keyCode === 229) return if (e.key == 'Enter' && isOpen && highlightedIndex === -1) { closeMenu() e.preventDefault() } } return (