import { useEffect, useRef, useState } from 'react'; type SelectOption = { value: T; label: string; }; type SelectProps = { value: T; options: SelectOption[]; onChange: (value: T) => void; }; export function Select({ value, options, onChange, }: SelectProps) { const [open, setOpen] = useState(false); const ref = useRef(null); const selected = options.find((option) => option.value === value) ?? options[0]; useEffect(() => { function handleClick(event: MouseEvent) { if ( ref.current && !ref.current.contains(event.target as Node) ) { setOpen(false); } } window.addEventListener('mousedown', handleClick); return () => { window.removeEventListener('mousedown', handleClick); }; }, []); return (
{open && (
{options.map((option) => { const active = option.value === value; return ( ); })}
)}
); }