import React, { createContext, useContext, useState, useRef, useEffect } from 'react'; const SelectContext = createContext(null); export const SelectRoot = ({ children, onValueChange, value, defaultValue }: any) => { const [open, setOpen] = useState(false); const [internalVal, setInternalVal] = useState(defaultValue); const currentVal = value !== undefined ? value : internalVal; const ref = useRef(null); useEffect(() => { const handleClickOutside = (e: MouseEvent) => { if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false); }; document.addEventListener('mousedown', handleClickOutside); return () => document.removeEventListener('mousedown', handleClickOutside); }, []); const handleValueChange = (v: string) => { if (value === undefined) setInternalVal(v); onValueChange?.(v); setOpen(false); }; return (
{children}
); }; SelectRoot.displayName = "Select"; export const SelectTrigger = React.forwardRef(({ className = '', children, onClick, ...props }, ref) => { const { open, setOpen } = useContext(SelectContext); return ( ); }); SelectTrigger.displayName = "SelectTrigger"; export const SelectValue = React.forwardRef(({ placeholder, ...props }, ref) => { const { value } = useContext(SelectContext); return {value || placeholder}; }); SelectValue.displayName = "SelectValue"; export const SelectContent = React.forwardRef(({ className = '', children, ...props }, ref) => { const { open } = useContext(SelectContext); if (!open) return null; return (
{children}
); }); SelectContent.displayName = "SelectContent"; export const SelectItem = React.forwardRef(({ className = '', value, children, ...props }, ref) => { const { onValueChange, value: selectedValue } = useContext(SelectContext); const isSelected = selectedValue === value; return (
onValueChange(value)} className={`relative flex w-full cursor-pointer select-none items-center rounded-sm py-1.5 pl-8 pr-2 text-sm outline-none hover:bg-surface-hover hover:text-text focus:bg-surface-hover focus:text-text data-[disabled]:pointer-events-none data-[disabled]:opacity-50 ${isSelected ? 'bg-surface-hover' : ''} ${className}`} {...props}> {isSelected && "✓"} {children}
); }); SelectItem.displayName = "SelectItem"; export const SelectGroup = ({ children }: any) => <>{children}; export const SelectLabel = ({ className = '', ...props }: any) =>
; export const SelectSeparator = ({ className = '', ...props }: any) =>
; export const SelectScrollUpButton = () => null; export const SelectScrollDownButton = () => null;