import { useRef, useEffect } from 'react' import { View, Text, StyleSheet, TextInput, Pressable, } from 'react-native' import { useTranslation } from 'react-i18next' import { LeftArrowIcon, CloseIcon } from '@/components/icon' interface SearchBarProps { searchText: string onSearchTextChange: (text: string) => void onSearch: (text: string) => void onBack: () => void placeholder?: string autoFocus?: boolean inputRef?: React.RefObject showBackButton?: boolean showSearchButton?: boolean readOnly?: boolean onInputPress?: () => void onClearPress?: () => void marginBottom?: number } export default function SearchBar({ searchText, onSearchTextChange, onSearch, onBack, placeholder, autoFocus = false, inputRef: externalInputRef, showBackButton = true, showSearchButton = true, readOnly = false, onInputPress, onClearPress, marginBottom = 20, }: SearchBarProps) { const { t } = useTranslation() const internalInputRef = useRef(null) const inputRef = externalInputRef || internalInputRef const defaultPlaceholder = placeholder || t('searchBar.placeholder') useEffect(() => { if (autoFocus) { const timer = setTimeout(() => { inputRef.current?.focus() }, 100) return () => clearTimeout(timer) } }, [autoFocus, inputRef]) const handleClear = () => { if (onClearPress) { onClearPress() } else { onSearchTextChange('') } } const inputContainer = ( <> { if (searchText.trim()) { onSearch(searchText.trim()) } }} /> {searchText.length > 0 && ( { if (readOnly && onInputPress) { e.stopPropagation() } handleClear() }} style={styles.clearButton} > )} ) return ( {showBackButton && ( )} {readOnly && onInputPress ? ( {inputContainer} ) : ( {inputContainer} )} {showSearchButton && ( { if (searchText.trim()) { onSearch(searchText.trim()) } }} > {t('searchBar.button')} )} ) } const styles = StyleSheet.create({ topBar: { height: 44, flexDirection: 'row', alignItems: 'center', paddingHorizontal: 12, marginTop: 8, marginBottom: 20, }, searchInputContainer: { flex: 1, marginLeft: 4, marginRight: 12, position: 'relative', height: 40, }, searchInput: { flex: 1, color: '#F5F5F5', fontSize: 14, height: 40, paddingVertical: 8, paddingLeft: 12, paddingRight: 36, borderRadius: 100, backgroundColor: '#16181B', }, clearButton: { position: 'absolute', right: 8, top: 0, bottom: 0, justifyContent: 'center', alignItems: 'center', padding: 4, }, searchButton: { paddingHorizontal: 0, paddingVertical: 8, }, searchButtonText: { color: '#F5F5F5', fontSize: 14, fontWeight: '400', }, })