import { useState, useRef, useEffect } from 'react'; import styles from './Select.module.css'; interface SelectOption { label: string; value: string; } interface SelectProps { options: SelectOption[]; value: string; onChange: (value: string) => void; placeholder?: string; minWidth?: number; } export function Select({ options, value, onChange, placeholder = '请选择', minWidth = 120 }: SelectProps) { const [open, setOpen] = useState(false); const ref = useRef(null); useEffect(() => { if (!open) return; const handle = (e: MouseEvent) => { if (ref.current && !ref.current.contains(e.target as Node)) { setOpen(false); } }; document.addEventListener('mousedown', handle); return () => document.removeEventListener('mousedown', handle); }, [open]); const selected = options.find((o) => o.value === value); return (
{options.map((opt) => (
{ onChange(opt.value); setOpen(false); }} > {opt.label}
))}
); }