import React, { useState } from 'react'; import { View, StyleSheet, Pressable, TextInput, Platform } from 'react-native'; import { useTheme } from '~/features/theme/ThemeProvider'; import Icon from '~/components/atoms/Icon'; import Text from '~/components/atoms/Text'; import colors from '~/tailwind.config.js'; interface SearchBarProps { onSearch: (query: string) => void; onClose: () => void; placeholder?: string; autoFocus?: boolean; inputRef?: React.RefObject; currentIndex?: number; totalResults?: number; onNext?: () => void; onPrevious?: () => void; onChange?: (text: string) => void; } /** * SearchBar Component * * A dedicated search input field with navigation controls. * Positioned at the top of the screen for search functionality. */ const SearchBar: React.FC = ({ onSearch, onClose, placeholder = 'Suche...', autoFocus = false, inputRef, currentIndex = 0, totalResults = 0, onNext, onPrevious, onChange, }) => { const internalInputRef = React.useRef(null); const textInputRef = inputRef || internalInputRef; const { isDark, themeVariant } = useTheme(); const [inputValue, setInputValue] = useState(''); // Direct access to colors from Tailwind config const menuBackgroundColor = React.useMemo(() => { const themeColors = colors.theme?.extend?.colors as Record; return isDark ? themeColors?.dark?.[themeVariant]?.menuBackground || '#252525' : themeColors?.[themeVariant]?.menuBackground || '#FFFFFF'; }, [isDark, themeVariant]); const borderColor = React.useMemo(() => { const themeColors = colors.theme?.extend?.colors as Record; return isDark ? themeColors?.dark?.[themeVariant]?.border || '#424242' : themeColors?.[themeVariant]?.border || '#e6e6e6'; }, [isDark, themeVariant]); const borderLightColor = React.useMemo(() => { const themeColors = colors.theme?.extend?.colors as Record; return isDark ? themeColors?.dark?.[themeVariant]?.borderLight || '#333333' : themeColors?.[themeVariant]?.borderLight || '#f2f2f2'; }, [isDark, themeVariant]); const handleSearch = () => { if (inputValue.trim()) { onSearch(inputValue.trim()); } }; const handleTextChange = (text: string) => { setInputValue(text); onChange?.(text); // Automatically search as user types (with debouncing handled by parent) if (text.trim()) { onSearch(text.trim()); } }; // Auto-focus when component mounts React.useEffect(() => { if (autoFocus && textInputRef.current) { const timer = setTimeout(() => { textInputRef.current?.focus(); }, 100); return () => clearTimeout(timer); } }, [autoFocus]); return ( {/* Search input container */} {/* Search icon */} {/* Close button */} {/* Navigation controls - show when there are results */} {totalResults > 0 && ( {currentIndex} von {totalResults} )} ); }; const styles = StyleSheet.create({ container: { width: '100%', paddingVertical: 12, paddingHorizontal: 16, }, inputContainer: { flexDirection: 'row', alignItems: 'center', backgroundColor: 'rgba(0, 0, 0, 0.05)', borderRadius: 24, paddingHorizontal: 12, paddingVertical: Platform.OS === 'ios' ? 8 : 10, borderWidth: 1, height: Platform.OS === 'ios' ? 44 : 48, // Standard heights for touch targets }, searchIconContainer: { justifyContent: 'center', alignItems: 'center', marginRight: 8, paddingHorizontal: 4, }, closeButton: { padding: 6, borderRadius: 20, justifyContent: 'center', alignItems: 'center', marginLeft: 8, }, input: { flex: 1, fontSize: 16, paddingVertical: 0, paddingHorizontal: 0, margin: 0, textAlignVertical: 'center', // Android specific includeFontPadding: false, // Android specific - removes extra padding ...Platform.select({ ios: { lineHeight: 20, // Explicit line height for iOS }, android: { paddingVertical: 2, }, }), }, searchButton: { padding: 8, borderRadius: 20, justifyContent: 'center', alignItems: 'center', marginLeft: 4, }, searchButtonPressed: { opacity: 0.7, }, navigationContainer: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', paddingHorizontal: 16, }, resultCount: { fontSize: 14, fontWeight: '500', }, navigationButtons: { flexDirection: 'row', gap: 8, }, navButton: { padding: 8, borderRadius: 20, justifyContent: 'center', alignItems: 'center', }, }); export default SearchBar;