import { Client } from '@/types/types'; import { ChevronDown, Search, X } from 'lucide-react-native'; import React, { useState, useEffect, useMemo } from 'react'; import { Modal, FlatList, Text, TextInput, TouchableOpacity, View, Keyboard, KeyboardEvent, Platform } from 'react-native'; interface ClientFilterProps { clients: Client[]; selectedClientId: any; onClientSelect: (clientId: any) => void; textColor?: string; } export default function ClientFilter({ clients, selectedClientId, onClientSelect, textColor }: ClientFilterProps) { const [showPicker, setShowPicker] = useState(false); const [searchQuery, setSearchQuery] = useState(''); const [keyboardHeight, setKeyboardHeight] = useState(0); // Keyboard height listener useEffect(() => { const showSubscription = Keyboard.addListener( Platform.OS === 'ios' ? 'keyboardWillShow' : 'keyboardDidShow', (e: KeyboardEvent) => setKeyboardHeight(e.endCoordinates.height) ); const hideSubscription = Keyboard.addListener( Platform.OS === 'ios' ? 'keyboardWillHide' : 'keyboardDidHide', () => setKeyboardHeight(0) ); return () => { showSubscription.remove(); hideSubscription.remove(); }; }, []); // Reset search query every time modal is opened useEffect(() => { if (showPicker) { setSearchQuery(''); } }, [showPicker]); const filteredClients = useMemo(() => { if (!showPicker) return []; if (!searchQuery.trim()) return clients; const lowerQuery = searchQuery.toLowerCase(); return clients.filter(c => c.label.toLowerCase().includes(lowerQuery)); }, [clients, searchQuery, showPicker]); const selectedClientLabel = selectedClientId ? clients.find(c => c.id === selectedClientId)?.label || 'Committente Selezionato' : 'Tutti i committenti'; return ( Committente setShowPicker(true)} className="flex-1 flex-row items-center justify-between bg-white px-5 py-4 rounded-2xl border border-gray-200 shadow-sm" > {selectedClientLabel} {selectedClientId !== null && ( onClientSelect(null)} className="bg-gray-50 p-4 rounded-2xl border border-gray-200 shadow-sm justify-center items-center" > )} {/* Nested Picker Modal */} setShowPicker(false)}> setShowPicker(false)} className="flex-1 bg-black/50 justify-end" style={{ paddingBottom: keyboardHeight }} > true}> Seleziona Committente setShowPicker(false)} className="p-2 bg-gray-100 rounded-full active:bg-gray-200"> {/* Search Bar */} {searchQuery.length > 0 && ( setSearchQuery('')} className="p-1"> )} item.id.toString()} ListHeaderComponent={() => ( searchQuery.trim() === '' ? ( { onClientSelect(null); setShowPicker(false); }} > Tutti i committenti ) : null )} ListEmptyComponent={() => ( Nessun committente trovato per "{searchQuery}" )} initialNumToRender={15} maxToRenderPerBatch={20} windowSize={5} removeClippedSubviews={true} renderItem={({ item }) => ( { onClientSelect(item.id); setShowPicker(false); }} > {item.label} )} /> ); }