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

Sân thử

Đổi tuỳ chọn và xem ngay ô tìm kiếm, đoạn code và các sự kiện.

Demo chạy thật, hãy bấm thử.Mở toàn màn hình ↗
  1. Chọn giá trị. Khung tối hiện đúng đoạn JSX tạo ra ô tìm kiếm phía trên.
  2. Focus, gõ, tìm, xoá, huỷ. Bốn sự kiện gần nhất hiện dưới đoạn code.
  3. Đặt animationDuration là 1500 để thấy rõ phần nào chuyển động.

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/playground.tsx
import { useState } from 'react';
import { Pressable, ScrollView, StyleSheet, Switch, Text, View } from 'react-native';
import Search, { type SearchProps } from 'react-native-search-box';
const SWITCHES = [
'showCancelButton',
'useClearButton',
'animated',
'collapseOnBlur',
'keyboardShouldPersist',
'editable',
'shadowVisible',
] as const;
const CHOICES = {
placeholderAlign: ['center', 'left'],
direction: ['ltr', 'rtl'],
cancelTitle: ['Cancel', 'Discard search'],
inputHeight: [30, 40, 56],
inputBorderRadius: [5, 12, 28],
animationDuration: [200, 600, 1500],
backgroundColor: ['grey', '#1d4ed8', '#166534'],
} as const;
const DEFAULTS: SearchProps = {
showCancelButton: true,
useClearButton: true,
animated: true,
collapseOnBlur: false,
keyboardShouldPersist: false,
editable: true,
shadowVisible: false,
placeholderAlign: 'center',
direction: 'ltr',
cancelTitle: 'Cancel',
inputHeight: 30,
inputBorderRadius: 5,
animationDuration: 200,
backgroundColor: 'grey',
};
/** The JSX that gives the box on the screen. */
function getCode(props: SearchProps): string {
const lines = (Object.keys(props) as (keyof SearchProps)[])
.filter((name) => props[name] !== DEFAULTS[name])
.map((name) => {
const value = props[name];
if (value === true) return ` ${name}`;
return typeof value === 'string' ? ` ${name}="${value}"` : ` ${name}={${String(value)}}`;
});
return lines.length ? ['<Search', ...lines, '/>'].join('\n') : '<Search />';
}
export default function Playground() {
const [props, setProps] = useState(DEFAULTS);
const [events, setEvents] = useState<string[]>([]);
const set = (name: string, value: unknown) => setProps((current) => ({ ...current, [name]: value }));
const log = (event: string) => setEvents((list) => [event, ...list].slice(0, 4));
return (
<View style={styles.screen}>
<Search
{...props}
onFocus={() => log('onFocus')}
onBlur={() => log('onBlur')}
onChangeText={(text) => log(`onChangeText("${text}")`)}
onSearch={(text) => log(`onSearch("${text}")`)}
onDelete={() => log('onDelete')}
onCancel={() => log('onCancel')}
/>
<ScrollView contentContainerStyle={styles.content} keyboardShouldPersistTaps="handled">
<Text style={styles.code} selectable>
{getCode(props)}
</Text>
<Text style={styles.events}>{events.length ? events.join('\n') : 'Events show up here.'}</Text>
{Object.entries(CHOICES).map(([name, values]) => (
<View key={name} style={styles.option}>
<Text style={styles.name}>{name}</Text>
<View style={styles.values}>
{values.map((value) => {
const selected = props[name as keyof SearchProps] === value;
return (
<Pressable
key={value}
accessibilityRole="button"
accessibilityState={{ selected }}
onPress={() => set(name, value)}
style={[styles.value, selected && styles.selected]}
>
<Text style={selected ? styles.selectedText : styles.valueText}>{value}</Text>
</Pressable>
);
})}
</View>
</View>
))}
{SWITCHES.map((name) => (
<View key={name} style={styles.switch}>
<Text style={styles.name}>{name}</Text>
<Switch
accessibilityLabel={name}
value={props[name]}
onValueChange={(value) => set(name, value)}
/>
</View>
))}
</ScrollView>
</View>
);
}
const styles = StyleSheet.create({
screen: { flex: 1, backgroundColor: '#fff' },
content: { padding: 16, gap: 14 },
code: { padding: 12, backgroundColor: '#111827', color: '#bef264', fontFamily: 'monospace', fontSize: 13 },
events: { minHeight: 72, color: '#6b7280', fontFamily: 'monospace', fontSize: 13 },
option: { gap: 6 },
name: { fontSize: 15, fontWeight: '600', color: '#111' },
values: { flexDirection: 'row', flexWrap: 'wrap', gap: 8 },
value: { paddingHorizontal: 12, paddingVertical: 7, borderRadius: 16, backgroundColor: '#f3f4f6' },
valueText: { color: '#374151' },
selected: { backgroundColor: '#1d4ed8' },
selectedText: { color: '#fff' },
switch: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between' },
});