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

Danh bạ

Danh sách chia nhóm, tô đậm phần chữ khớp.

Demo chạy thật, hãy bấm thử.Mở toàn màn hình ↗
  1. collapseOnBlur thu ô lại khi chạm vào danh sách, chữ vẫn giữ nguyên.
  2. backgroundColor trong suốt giúp ô hoà vào nền màn hình.
  3. Highlight tách tên quanh phần khớp.

Dùng

Đây chính là đoạn code đang chạy trong điện thoại. Chép vào app của bạn là dùng được.

example/demos/contacts.tsx
import { useState } from 'react';
import { SectionList, StyleSheet, Text, View } from 'react-native';
import Search from 'react-native-search-box';
const NAMES =
'Ada Park,Aiko Mori,An Nguyen,Ben Okafor,Bianca Rossi,Chi Tran,Clara Weiss,Dara Singh,Elena Petrova,Emil Larsen,Farah Khan,Gia Pham,Hana Sato,Ivan Horvat,Jude Martin,Kofi Mensah,Lena Vogel,Minh Le,Noor Haddad,Omar Reyes,Petra Novak'.split(
',',
);
function getSections(query: string) {
const sections = new Map<string, string[]>();
for (const name of NAMES) {
if (!name.toLowerCase().includes(query)) continue;
sections.set(name[0], [...(sections.get(name[0]) ?? []), name]);
}
return [...sections].map(([title, data]) => ({ title, data }));
}
/** The name, with the part that matches in bold. */
function Highlight({ text, query }: { text: string; query: string }) {
const at = text.toLowerCase().indexOf(query);
if (!query || at < 0) return <Text style={styles.name}>{text}</Text>;
return (
<Text style={styles.name}>
{text.slice(0, at)}
<Text style={styles.match}>{text.slice(at, at + query.length)}</Text>
{text.slice(at + query.length)}
</Text>
);
}
export default function Contacts() {
const [text, setText] = useState('');
const query = text.trim().toLowerCase();
const reset = () => setText('');
return (
<View style={styles.screen}>
<Text style={styles.title}>Contacts</Text>
<View style={styles.boxRow}>
<Search
backgroundColor="transparent"
titleCancelColor="#007aff"
inputStyle={styles.input}
inputHeight={36}
inputBorderRadius={10}
searchIconCollapsedMargin={34}
searchIconExpandedMargin={14}
placeholderExpandedMargin={30}
collapseOnBlur
onChangeText={setText}
onDelete={reset}
onCancel={reset}
/>
</View>
<SectionList
style={styles.list}
sections={getSections(query)}
keyExtractor={(name) => name}
keyboardShouldPersistTaps="handled"
stickySectionHeadersEnabled
renderSectionHeader={({ section }) => <Text style={styles.section}>{section.title}</Text>}
renderItem={({ item }) => (
<View style={styles.row}>
<View style={styles.avatar}>
<Text style={styles.initials}>{item.split(' ').map((word) => word[0])}</Text>
</View>
<Highlight text={item} query={query} />
</View>
)}
ListEmptyComponent={<Text style={styles.empty}>No contacts</Text>}
/>
</View>
);
}
const styles = StyleSheet.create({
screen: { flex: 1, backgroundColor: '#f2f2f7' },
title: { fontSize: 32, fontWeight: '700', marginHorizontal: 16, marginTop: 12, color: '#111' },
boxRow: { paddingHorizontal: 11 },
input: { fontSize: 16, backgroundColor: '#e3e3e8', color: '#111' },
list: { flex: 1, backgroundColor: '#fff' },
section: {
paddingHorizontal: 16,
paddingVertical: 4,
fontWeight: '600',
color: '#6b7280',
backgroundColor: '#f2f2f7',
},
row: { flexDirection: 'row', alignItems: 'center', gap: 12, paddingHorizontal: 16, paddingVertical: 8 },
avatar: {
width: 36,
height: 36,
borderRadius: 18,
backgroundColor: '#dbeafe',
alignItems: 'center',
justifyContent: 'center',
},
initials: { color: '#1d4ed8', fontWeight: '600', fontSize: 13 },
name: { fontSize: 16, color: '#111' },
match: { fontWeight: '700', color: '#007aff' },
empty: { padding: 32, textAlign: 'center', color: '#6b7280' },
});