import { useEffect, useState } from 'react'; import { useSelect } from 'downshift'; import classNames from 'classnames'; import { SelectButton } from '../button'; import { SelectOption } from '../option'; import { SelectList } from '../list'; interface SelectProps { className?: string; placeholder?: string; defaultValue?: { label: string; value: string }; value?: { label: string; value: string }; onChange?: ( selectedOption: { label: string; value: string } | null | undefined ) => void; options?: { value: string; label: string }[]; } export function Select({ className, placeholder, defaultValue, value, onChange, options = [], }: SelectProps) { const [inputItems, setInputItems] = useState< { label: string; value: string }[] >(options); const { isOpen, selectedItem, getToggleButtonProps, getMenuProps, highlightedIndex, getItemProps, } = useSelect({ items: options, itemToString: item => (!!item?.label ? item.label : ''), onSelectedItemChange: ({ selectedItem }) => onChange?.(selectedItem), selectedItem: value, defaultSelectedItem: defaultValue, }); useEffect(() => { setInputItems(options); }, [options]); return (
{selectedItem?.label || placeholder}
{isOpen && ( {inputItems.map((item, index) => ( {item.label} ))} )}
); } Select.defaultProps = { children: null, className: '', placeholder: '', defaultValue: undefined, options: [], };