import React, {useState, useCallback} from 'react' import {useCombobox} from 'downshift' import classNames from 'classnames' import debounce from 'debounce-promise' 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) const [initialLoadComplete, setInitialLoadComplete] = 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: ({inputValue}) => { if (!initialLoadComplete) { fetchOptions({inputValue: inputValue, isOpen: true}).then(() => { setInitialLoadComplete(true) }) } } }) 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 (