import { useMemo, useState } from 'react'; import { FlatList, Modal, Pressable, View } from 'react-native'; import { Icon } from '../../atoms/Icon'; import { Text } from '../../atoms/Text'; import { cn } from '../../lib/cn'; import { FormField } from '../FormField'; import { SearchInput } from '../SearchInput'; import type { SelectOption, SelectProps } from './Select.types'; import { SelectOptionRow } from './SelectOptionRow'; /** * Select universal basado en `Modal` + `FlatList`. * Reemplaza `react-native-element-dropdown`. Cuando React Native Reusables * publique un `select` estable se puede sustituir manteniendo la API. */ export function Select({ value, options, onSelect, placeholder = 'Selecciona una opción', label, helperText, error, disabled = false, searchable = false, searchPlaceholder = 'Buscar...', emptyText = 'No hay opciones', className, triggerClassName, testID, }: SelectProps) { const [open, setOpen] = useState(false); const [query, setQuery] = useState(''); const selected = useMemo | undefined>( () => options.find((option) => option.value === value), [options, value], ); const filtered = useMemo(() => { if (!searchable || !query) { return options; } const normalized = query.toLowerCase(); return options.filter((option) => option.label.toLowerCase().includes(normalized)); }, [options, query, searchable]); const handleOpen = () => { if (disabled) return; setOpen(true); }; const handleSelect = (option: SelectOption) => { if (option.disabled) return; onSelect(option.value); setOpen(false); setQuery(''); }; const hasError = Boolean(error); return ( {selected?.label ?? placeholder} setOpen(false)} > setOpen(false)} className="flex-1 bg-overlay/40" > {label ? ( {label} ) : null} {searchable ? ( setQuery('')} /> ) : null} {filtered.length === 0 ? ( {emptyText} ) : ( item.value} ItemSeparatorComponent={() => } className="max-h-80" renderItem={({ item }) => ( )} /> )} ); }