import * as React from "react"; export interface SelectProps { value?: string; onValueChange?: (value: string) => void; defaultValue?: string; disabled?: boolean; children?: React.ReactNode; } export interface SelectTriggerProps { id?: string; className?: string; children?: React.ReactNode; } export interface SelectContentProps { children?: React.ReactNode; } export interface SelectItemProps { value: string; children?: React.ReactNode; } export interface SelectValueProps { placeholder?: string; } const SelectContext = React.createContext<{ value?: string; onValueChange?: (value: string) => void; isOpen: boolean; setIsOpen: (open: boolean) => void; }>({ isOpen: false, setIsOpen: () => { // Default no-op }, }); export function Select({ value, onValueChange, defaultValue, children, }: SelectProps): React.JSX.Element { const [isOpen, setIsOpen] = React.useState(false); const [internalValue, setInternalValue] = React.useState(defaultValue); const currentValue = value ?? internalValue; const handleValueChange = (newValue: string): void => { if (value === undefined) { setInternalValue(newValue); } onValueChange?.(newValue); setIsOpen(false); }; const contextValue: { value?: string; onValueChange?: (value: string) => void; isOpen: boolean; setIsOpen: (open: boolean) => void; } = { isOpen, setIsOpen }; if (currentValue !== undefined) { contextValue.value = currentValue; } contextValue.onValueChange = handleValueChange; return (
{children}
); } export function SelectTrigger({ id, className = "", children, }: SelectTriggerProps): React.JSX.Element { const { isOpen, setIsOpen } = React.useContext(SelectContext); return ( ); } export function SelectValue({ placeholder }: SelectValueProps): React.JSX.Element { const { value } = React.useContext(SelectContext); return {value ?? placeholder}; } export function SelectContent({ children }: SelectContentProps): React.JSX.Element | null { const { isOpen } = React.useContext(SelectContext); if (!isOpen) return null; return (
{children}
); } export function SelectItem({ value, children }: SelectItemProps): React.JSX.Element { const { onValueChange } = React.useContext(SelectContext); return (
onValueChange?.(value)} className="cursor-pointer px-3 py-2 text-sm hover:bg-gray-100" > {children}
); }