import * as React from "react" import * as SelectPrimitive from "@radix-ui/react-select" import { Check, ChevronDown, ChevronUp } from "lucide-react" import { cn } from "@/lib/utils" // Enhanced Select with controlled open state const SelectRoot = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ open, defaultOpen, onOpenChange, ...props }, ref) => { // Control open state manually to prevent unwanted closures const [isOpen, setIsOpen] = React.useState(defaultOpen || false); // Sync with parent's open state React.useEffect(() => { if (open !== undefined) { setIsOpen(open); } }, [open]); // Notify parent of open state changes const handleOpenChange = (newOpen: boolean) => { // Add some debouncing to prevent rapid open/close cycles setTimeout(() => { setIsOpen(newOpen); if (onOpenChange) { onOpenChange(newOpen); } }, 0); }; return ( ); }); SelectRoot.displayName = "SelectRoot"; const Select = SelectRoot const SelectGroup = SelectPrimitive.Group const SelectValue = SelectPrimitive.Value const SelectTrigger = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, children, ...props }, ref) => { // Add event handlers to prevent touch events from causing immediate closures const handleTouchStart = (e: React.TouchEvent) => { e.stopPropagation(); }; const handleTouchEnd = (e: React.TouchEvent) => { e.stopPropagation(); }; const handleClick = (e: React.MouseEvent) => { e.stopPropagation(); // Don't add any logic here as it might interfere with radix's internal handling }; return ( span]:line-clamp-1", className )} onTouchStart={handleTouchStart} onTouchEnd={handleTouchEnd} onClick={handleClick} {...props} > {children} ); }) SelectTrigger.displayName = SelectPrimitive.Trigger.displayName const SelectScrollUpButton = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, ...props }, ref) => ( )) SelectScrollUpButton.displayName = SelectPrimitive.ScrollUpButton.displayName const SelectScrollDownButton = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, ...props }, ref) => ( )) SelectScrollDownButton.displayName = SelectPrimitive.ScrollDownButton.displayName const SelectContent = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, children, position = "popper", ...props }, ref) => { // Comprehensive set of event handlers to prevent immediate closing on mobile const handleTouchStart = (e: React.TouchEvent) => { e.stopPropagation(); }; const handleTouchMove = (e: React.TouchEvent) => { e.stopPropagation(); }; const handleTouchEnd = (e: React.TouchEvent) => { e.stopPropagation(); }; const handleMouseDown = (e: React.MouseEvent) => { e.stopPropagation(); }; const handleClick = (e: React.MouseEvent) => { e.stopPropagation(); }; return ( {children} ) }) SelectContent.displayName = SelectPrimitive.Content.displayName const SelectLabel = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, ...props }, ref) => ( )) SelectLabel.displayName = SelectPrimitive.Label.displayName const SelectItem = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, children, ...props }, ref) => { // Add event handlers to prevent touch events from causing immediate closures const handleTouchStart = (e: React.TouchEvent) => { e.stopPropagation(); }; const handleTouchEnd = (e: React.TouchEvent) => { e.stopPropagation(); }; // Use mousedown instead of click to prevent the dropdown from closing immediately const handleMouseDown = (e: React.MouseEvent) => { // Stop propagation but don't prevent default to allow selection e.stopPropagation(); }; return ( {children} ); }) SelectItem.displayName = SelectPrimitive.Item.displayName const SelectSeparator = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, ...props }, ref) => ( )) SelectSeparator.displayName = SelectPrimitive.Separator.displayName export { Select, SelectGroup, SelectValue, SelectTrigger, SelectContent, SelectLabel, SelectItem, SelectSeparator, SelectScrollUpButton, SelectScrollDownButton, }