// components/ui/select.tsx import * as React from 'react'; import { Pressable, ScrollView, Text, View } from 'react-native'; import { cn } from '@/lib/utils'; import { Dialog, DialogContent } from './dialog'; interface SelectOption { label: string; value: string; } interface SelectProps { value?: string; onValueChange?: (value: string) => void; children: React.ReactNode; disabled?: boolean; } interface SelectTriggerProps { className?: string; children?: React.ReactNode; size?: 'sm' | 'md' | 'lg'; } interface SelectValueProps { placeholder?: string; className?: string; } interface SelectContentProps { className?: string; children: React.ReactNode; } interface SelectItemProps { value: string; label: string; className?: string; disabled?: boolean; } interface SelectGroupProps { className?: string; children: React.ReactNode; } interface SelectLabelProps { className?: string; children: React.ReactNode; } interface SelectSeparatorProps { className?: string; } const SelectContext = React.createContext<{ value?: string; onValueChange?: (value: string) => void; open: boolean; setOpen: (open: boolean) => void; disabled?: boolean; options: Map; registerOption: (value: string, label: string) => void; } | null>(null); function useSelect() { const context = React.useContext(SelectContext); if (!context) { throw new Error('Select components must be used within Select'); } return context; } export function Select({ value, onValueChange, children, disabled = false }: SelectProps) { const [open, setOpen] = React.useState(false); const [options] = React.useState(() => new Map()); const registerOption = React.useCallback((val: string, label: string) => { options.set(val, label); }, [options]); return ( !disabled && setOpen(val), disabled, options, registerOption, }} > {children} ); } export function SelectTrigger({ className, children, size = 'md' }: SelectTriggerProps) { const { setOpen, disabled } = useSelect(); const sizeStyles = { sm: 'h-9 px-3 py-2', md: 'h-10 px-4 py-3', lg: 'h-12 px-4 py-3.5', }; return ( setOpen(true)} disabled={disabled} className={cn( 'flex-row items-center justify-between gap-2 rounded-lg border border-slate-300 bg-white shadow-sm', sizeStyles[size], disabled && 'opacity-50', className )} > {children} ▼ ); } export function SelectValue({ placeholder = 'Select...', className }: SelectValueProps) { const { value, options } = useSelect(); const displayValue = value ? options.get(value) : null; return ( {displayValue || placeholder} ); } export function SelectContent({ className, children }: SelectContentProps) { return ( {children} ); } export function SelectItem({ value, label, className, disabled = false }: SelectItemProps) { const { value: selectedValue, onValueChange, setOpen, registerOption } = useSelect(); const isSelected = selectedValue === value; React.useEffect(() => { registerOption(value, label); }, [value, label, registerOption]); const handleSelect = () => { if (disabled) return; onValueChange?.(value); setOpen(false); }; return ( {label} {isSelected && ( ✓ )} ); } export function SelectGroup({ className, children }: SelectGroupProps) { return {children}; } export function SelectLabel({ className, children }: SelectLabelProps) { return ( {children} ); } export function SelectSeparator({ className }: SelectSeparatorProps) { return ; } export type { SelectOption };