import React, { useState } from 'react'; import { View, Text, TouchableOpacity, Modal, ScrollView, Pressable } from 'react-native'; import { Ionicons } from '@expo/vector-icons'; import { useTheme } from '~/hooks/useTheme'; interface DropdownOption { label: string; value: string; } interface DropdownGroup { title: string; options: DropdownOption[]; } interface DropdownProps { options: DropdownOption[]; groups?: DropdownGroup[]; value: string; onValueChange: (value: string) => void; placeholder?: string; disabled?: boolean; title?: string; } export function Dropdown({ options, groups, value, onValueChange, placeholder = 'Select an option', disabled = false, title = 'Select Option', }: DropdownProps) { const [isOpen, setIsOpen] = useState(false); const { colors } = useTheme(); // Find selected option from either flat options or groups const allOptions = groups ? groups.flatMap((g) => g.options) : options; const selectedOption = allOptions.find((opt) => opt.value === value); const handleSelect = (optionValue: string) => { onValueChange(optionValue); setIsOpen(false); }; return ( !disabled && setIsOpen(true)} className={`flex-row items-center justify-between rounded-lg border ${colors.border} ${colors.surface} px-4 py-3 ${ disabled ? 'opacity-50' : '' }`} disabled={disabled} > {selectedOption?.label || placeholder} setIsOpen(false)} > setIsOpen(false)} > e.stopPropagation()} className={`max-h-[80%] rounded-xl border ${colors.border} ${colors.surface} shadow-xl`} > {title} setIsOpen(false)}> {groups ? // Render grouped options groups.map((group, groupIndex) => ( 0 ? 'mt-4' : ''}> {group.title} {group.options.map((option) => ( handleSelect(option.value)} className={`mx-2 mb-1 rounded-lg px-4 py-3 ${ option.value === value ? colors.primary : colors.surfaceSecondary }`} > {option.label} ))} )) : // Render flat options options.map((option) => ( handleSelect(option.value)} className={`mx-2 mb-1 rounded-lg px-4 py-3 ${ option.value === value ? colors.primary : colors.surfaceSecondary }`} > {option.label} ))} ); }