Recent searches
History and suggestions that fill the box and search.
How it works
Section titled “How it works”valueandonChangeTextkeep the text in the parent. Withvalue, clearing is reported too.- A tap on a suggestion calls
focus(text)and thensearch(). - The collapsed margins are half the width of the placeholder, which centers a long one.
Uses
Full source
Section titled “Full source”This is the code running in the phone. Copy it into your app.
import { useRef, useState } from 'react';import { Pressable, ScrollView, StyleSheet, Text, View } from 'react-native';import Search from 'react-native-search-box';
const POPULAR = ['Headphones', 'Desk lamp', 'Running shoes', 'Coffee beans'];
export default function Recent() { const box = useRef<Search>(null); const [value, setValue] = useState(''); const [recent, setRecent] = useState(['Mechanical keyboard', 'Monstera']); const [result, setResult] = useState('');
const search = (text: string) => { const query = text.trim(); if (!query) return; setRecent((list) => [query, ...list.filter((item) => item !== query)].slice(0, 5)); setResult(query); }; // Fills the box, then searches as if the user had typed it. const pick = async (text: string) => { await box.current?.focus(text); await box.current?.search(); };
return ( <View style={styles.screen}> <Search ref={box} value={value} onChangeText={setValue} onSearch={search} onCancel={() => setResult('')} backgroundColor="#fff" titleCancelColor="#be185d" cancelTitle="Close" placeholder="What are you looking for?" placeholderCollapsedMargin={75} searchIconCollapsedMargin={85} inputHeight={40} inputBorderRadius={12} searchIconExpandedMargin={14} placeholderExpandedMargin={30} inputStyle={styles.input} /> <ScrollView contentContainerStyle={styles.content} keyboardShouldPersistTaps="handled"> {result !== '' && <Text style={styles.result}>Results for “{result}”</Text>} <View style={styles.heading}> <Text style={styles.headingText}>Recent</Text> {recent.length > 0 && ( <Pressable accessibilityRole="button" onPress={() => setRecent([])}> <Text style={styles.link}>Clear all</Text> </Pressable> )} </View> {recent.length === 0 && <Text style={styles.empty}>Your searches will show up here.</Text>} {recent.map((item) => ( <Pressable key={item} accessibilityRole="button" onPress={() => void pick(item)} style={styles.row}> <Text style={styles.rowText}>{item}</Text> <Text style={styles.arrow}>↖</Text> </Pressable> ))} <Text style={[styles.headingText, styles.popular]}>Popular</Text> <View style={styles.chips}> {POPULAR.map((item) => ( <Pressable key={item} accessibilityRole="button" onPress={() => void pick(item)} style={styles.chip} > <Text style={styles.chipText}>{item}</Text> </Pressable> ))} </View> </ScrollView> </View> );}
const styles = StyleSheet.create({ screen: { flex: 1, backgroundColor: '#fff' }, input: { fontSize: 15, backgroundColor: '#fce7f3', color: '#500724' }, content: { padding: 16 }, result: { padding: 12, marginBottom: 16, backgroundColor: '#fdf2f8', color: '#9d174d' }, heading: { flexDirection: 'row', justifyContent: 'space-between' }, headingText: { fontSize: 18, fontWeight: '700', color: '#111' }, link: { color: '#be185d', fontWeight: '600', paddingVertical: 2 }, empty: { color: '#6b7280', paddingVertical: 12 }, row: { flexDirection: 'row', paddingVertical: 12, borderBottomWidth: 1, borderColor: '#f3f4f6' }, rowText: { flex: 1, fontSize: 16, color: '#111' }, arrow: { color: '#9ca3af' }, popular: { marginTop: 24, marginBottom: 12 }, chips: { flexDirection: 'row', flexWrap: 'wrap', gap: 8 }, chip: { paddingHorizontal: 14, paddingVertical: 8, borderRadius: 18, backgroundColor: '#fce7f3' }, chipText: { color: '#9d174d' },});