Bỏ qua để đến nội dung

Bắt đầu

Terminal window
npm install react-native-search-box

Không cần link, không cần provider. React 16.8 và React Native 0.60 trở lên. Được kiểm thử với React 19 trên React Native Web và trên Android (React Native 0.88, New Architecture); xem Nâng cấp để biết thay đổi từ 0.0.20.

Demo chạy thật, hãy bấm thử.Mở toàn màn hình ↗
  1. Đặt <Search /> ở đầu màn hình.
  2. Lưu từ khoá vào state bằng onChangeText.
  3. Đặt lại trong onDelete và onCancel, hoặc truyền value.
example/demos/basic.tsx
import { useState } from 'react';
import { FlatList, StyleSheet, Text, View } from 'react-native';
import Search from 'react-native-search-box';
const CITIES =
'Amsterdam,Bangkok,Barcelona,Berlin,Buenos Aires,Cairo,Cape Town,Đà Nẵng,Dublin,Hà Nội,Helsinki,Istanbul,Jakarta,Kyoto,Lima,Lisbon,London,Madrid,Mexico City,Montreal,Nairobi,New York,Oslo,Paris,Prague,Rome,Sài Gòn,Seoul,Singapore,Sydney,Taipei,Tokyo,Vienna,Warsaw,Zurich'.split(
',',
);
/** Case and accents do not matter: "ha noi" finds "Hà Nội". */
const normalize = (text: string) =>
text
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.replace(/đ/gi, 'd')
.toLowerCase();
export default function Basic() {
const [query, setQuery] = useState('');
const results = CITIES.filter((city) => normalize(city).includes(normalize(query.trim())));
const reset = () => setQuery('');
return (
<View style={styles.screen}>
<Search
backgroundColor="#e5e5ea"
titleCancelColor="#007aff"
placeholder="City"
inputHeight={36}
inputBorderRadius={10}
searchIconCollapsedMargin={34}
searchIconExpandedMargin={14}
placeholderExpandedMargin={30}
inputStyle={styles.input}
onChangeText={setQuery}
onDelete={reset}
onCancel={reset}
/>
<FlatList
data={results}
keyExtractor={(city) => city}
keyboardShouldPersistTaps="handled"
renderItem={({ item }) => <Text style={styles.row}>{item}</Text>}
ListEmptyComponent={<Text style={styles.empty}>No city matches “{query}”.</Text>}
/>
</View>
);
}
const styles = StyleSheet.create({
screen: { flex: 1, backgroundColor: '#fff' },
input: { fontSize: 16, color: '#111' },
row: {
paddingVertical: 13,
marginLeft: 16,
fontSize: 16,
color: '#111',
borderBottomWidth: 1,
borderColor: '#eee',
},
empty: { padding: 32, textAlign: 'center', color: '#6b7280' },
});
Terminal window
git clone https://github.com/anhtuank7c/react-native-search-box
cd react-native-search-box
npm install
npm run example