'use client'; import * as React from 'react'; import { cn } from '@/lib/utils'; import { ChevronDown } from 'lucide-react'; interface SelectContextValue { value: string; onValueChange: (value: string) => void; open: boolean; setOpen: (open: boolean) => void; } const SelectContext = React.createContext(undefined); interface SelectProps { value?: string; defaultValue?: string; onValueChange?: (value: string) => void; children: React.ReactNode; } export function Select({ value, defaultValue = '', onValueChange, children }: SelectProps) { const [internalValue, setInternalValue] = React.useState(defaultValue); const [open, setOpen] = React.useState(false); const currentValue = value ?? internalValue; const handleValueChange = React.useCallback((newValue: string) => { setInternalValue(newValue); onValueChange?.(newValue); setOpen(false); }, [onValueChange]); return (
{children}
); } export function SelectTrigger({ children, className }: { children: React.ReactNode; className?: string }) { const context = React.useContext(SelectContext); if (!context) throw new Error('SelectTrigger must be used within Select'); return ( ); } export function SelectValue({ placeholder }: { placeholder?: string }) { const context = React.useContext(SelectContext); if (!context) throw new Error('SelectValue must be used within Select'); return {context.value || placeholder}; } export function SelectContent({ children, className }: { children: React.ReactNode; className?: string }) { const context = React.useContext(SelectContext); if (!context) throw new Error('SelectContent must be used within Select'); if (!context.open) return null; return (
{children}
); } export function SelectItem({ value, children, className }: { value: string; children: React.ReactNode; className?: string }) { const context = React.useContext(SelectContext); if (!context) throw new Error('SelectItem must be used within Select'); return (
context.onValueChange(value)} className={cn( 'relative flex w-full cursor-pointer select-none items-center rounded-sm py-1.5 px-2 text-sm outline-none hover:bg-accent hover:text-accent-foreground', context.value === value && 'bg-accent text-accent-foreground', className )} > {children}
); }