屏幕
配色方案
明暗
圆角
颜色
所有状态
- 何时出现
- 行为规则
把这 2 个文件放进 components/mobilens/。只依赖 react-native(已用 Expo SDK 58 检查)。
import { useRef, useState, type ComponentRef, type ReactNode } from 'react';
import { Platform, Pressable, StyleSheet, Text, TextInput, View, type TextStyle } from 'react-native';
import { colors, radius } from './theme';
export type SearchBarProps = {
/** Current query. An empty string represents all results. */
value: string;
/** Called for typing, paste, clear and cancel. Filter results using the new value. */
onChangeText: (value: string) => void;
/** Search hint and accessible name, for example "Search contacts". */
placeholder?: string;
/** Receives the trimmed query when the user presses Search on the keyboard. */
onSubmit?: (query: string) => void;
/** Enables the cancel action, called after clearing the query and dismissing the keyboard. */
onCancel?: () => void;
/** Accessible name of the clear button. */
clearLabel?: string;
/** Visible label for the cancel action. */
cancelLabel?: string;
/** Prevents edits while still displaying the current query. */
disabled?: boolean;
/** Focus the field when it mounts. */
autoFocus?: boolean;
};
export function SearchBar({
value, onChangeText, placeholder = 'Search', onSubmit, onCancel,
clearLabel = 'Clear search', cancelLabel = 'Cancel', disabled = false, autoFocus = false,
}: SearchBarProps) {
const inputRef = useRef<ComponentRef<typeof TextInput>>(null);
const [focused, setFocused] = useState(false);
const [editing, setEditing] = useState(false);
function clear() {
onChangeText('');
inputRef.current?.focus();
}
function cancel() {
onChangeText('');
inputRef.current?.blur();
setEditing(false);
onCancel?.();
}
return (
<SearchShell focused={focused && !disabled} disabled={disabled} clearLabel={clearLabel}
showClear={!!value && !disabled} onClear={clear} cancelLabel={cancelLabel}
showCancel={!!onCancel && (editing || !!value) && !disabled} onCancel={cancel}>
<TextInput
ref={inputRef}
value={value}
placeholder={placeholder}
placeholderTextColor={colors.onSurfaceVariant}
accessibilityLabel={placeholder}
accessibilityState={{ disabled }}
editable={!disabled}
onChangeText={onChangeText}
autoFocus={autoFocus}
autoCapitalize="none"
autoCorrect={false}
returnKeyType="search"
submitBehavior="blurAndSubmit"
onSubmitEditing={(event) => { setEditing(false); onSubmit?.(event.nativeEvent.text.trim()); }}
onFocus={() => { setFocused(true); setEditing(true); }}
onBlur={() => setFocused(false)}
underlineColorAndroid="transparent"
style={[styles.input, NO_WEB_RING, disabled && styles.muted]}
/>
</SearchShell>
);
}
type SearchShellProps = {
focused: boolean;
disabled: boolean;
showClear: boolean;
clearLabel?: string;
onClear?: () => void;
showCancel: boolean;
cancelLabel: string;
onCancel?: () => void;
children: ReactNode;
};
function SearchShell({ focused, disabled, showClear, clearLabel, onClear, showCancel, cancelLabel, onCancel, children }: SearchShellProps) {
const cancelText = <Text style={styles.cancelText}>{cancelLabel}</Text>;
const clearMark = <Text style={styles.clearGlyph}>×</Text>;
return (
<View style={styles.row}>
<View style={[styles.bar, focused && styles.focused, disabled && styles.disabled]}>
<View aria-hidden style={styles.lens}>
<View style={styles.ring} />
<View style={styles.handle} />
</View>
{children}
{showClear && (onClear
? <Pressable accessibilityRole="button" accessibilityLabel={clearLabel} onPress={onClear} hitSlop={6} style={styles.clear}>{clearMark}</Pressable>
: <View style={styles.clear}>{clearMark}</View>)}
</View>
{showCancel && (onCancel
? <Pressable accessibilityRole="button" onPress={onCancel} hitSlop={4} style={styles.cancel}>{cancelText}</Pressable>
: <View style={styles.cancel}>{cancelText}</View>)}
</View>
);
}
/** Read-only representation of a particular search field state. */
export function SearchBarFace({ value, placeholder, focused = false, disabled = false, cancelLabel = 'Cancel' }: {
value: string; placeholder: string; focused?: boolean; disabled?: boolean; cancelLabel?: string;
}) {
return (
<SearchShell focused={focused} disabled={disabled} showClear={!!value && !disabled}
showCancel={(focused || !!value) && !disabled} cancelLabel={cancelLabel}>
<Text numberOfLines={1} style={[styles.fieldValue, (!value || disabled) && styles.muted]}>{value || placeholder}</Text>
</SearchShell>
);
}
const NO_WEB_RING = Platform.OS === 'web' ? ({ outlineStyle: 'none' } as unknown as TextStyle) : undefined;
const styles = StyleSheet.create({
row: { flexDirection: 'row', gap: 10, alignItems: 'center' },
bar: {
flex: 1, minWidth: 0, flexDirection: 'row', alignItems: 'center', gap: 10, minHeight: 48,
paddingHorizontal: 12, borderRadius: radius.md, borderWidth: 1, borderColor: colors.outlineVariant, backgroundColor: colors.surfaceVariant,
},
focused: { borderColor: colors.primary, backgroundColor: colors.surface },
disabled: { opacity: 0.65 },
lens: { width: 18, height: 18 },
ring: { width: 13, height: 13, borderWidth: 1.75, borderRadius: 7, borderColor: colors.onSurfaceVariant },
handle: { position: 'absolute', left: 10, top: 12, width: 7, height: 2, borderRadius: 1, transform: [{ rotate: '45deg' }], backgroundColor: colors.onSurfaceVariant },
fieldValue: { minWidth: 0, flex: 1, color: colors.onSurface, fontSize: 16 },
input: { minWidth: 0, flex: 1, paddingVertical: 10, paddingHorizontal: 0, color: colors.onSurface, fontSize: 16 },
muted: { color: colors.onSurfaceVariant },
clear: { minWidth: 28, minHeight: 36, alignItems: 'center', justifyContent: 'center' },
clearGlyph: { color: colors.onSurfaceVariant, fontSize: 23, lineHeight: 26, textAlign: 'center' },
cancel: { minHeight: 44, alignItems: 'center', justifyContent: 'center' },
cancelText: { fontSize: 15, fontWeight: '600', color: colors.primary },
});
import { useRef, useState, type ComponentRef, type ReactNode } from 'react';
import { Platform, Pressable, StyleSheet, Text, TextInput, View, type TextStyle } from 'react-native';
import { colors, radius } from './theme';
export type SearchBarProps = {
/** Current query. An empty string represents all results. */
value: string;
/** Called for typing, paste, clear and cancel. Filter results using the new value. */
onChangeText: (value: string) => void;
/** Search hint and accessible name, for example "Search contacts". */
placeholder?: string;
/** Receives the trimmed query when the user presses Search on the keyboard. */
onSubmit?: (query: string) => void;
/** Enables the cancel action, called after clearing the query and dismissing the keyboard. */
onCancel?: () => void;
/** Accessible name of the clear button. */
clearLabel?: string;
/** Visible label for the cancel action. */
cancelLabel?: string;
/** Prevents edits while still displaying the current query. */
disabled?: boolean;
/** Focus the field when it mounts. */
autoFocus?: boolean;
};
export function SearchBar({
value, onChangeText, placeholder = 'Search', onSubmit, onCancel,
clearLabel = 'Clear search', cancelLabel = 'Cancel', disabled = false, autoFocus = false,
}: SearchBarProps) {
const inputRef = useRef<ComponentRef<typeof TextInput>>(null);
const [focused, setFocused] = useState(false);
const [editing, setEditing] = useState(false);
function clear() {
onChangeText('');
inputRef.current?.focus();
}
function cancel() {
onChangeText('');
inputRef.current?.blur();
setEditing(false);
onCancel?.();
}
return (
<SearchShell focused={focused && !disabled} disabled={disabled} clearLabel={clearLabel}
showClear={!!value && !disabled} onClear={clear} cancelLabel={cancelLabel}
showCancel={!!onCancel && (editing || !!value) && !disabled} onCancel={cancel}>
<TextInput
ref={inputRef}
value={value}
placeholder={placeholder}
placeholderTextColor={colors.onSurfaceVariant}
accessibilityLabel={placeholder}
accessibilityState={{ disabled }}
editable={!disabled}
onChangeText={onChangeText}
autoFocus={autoFocus}
autoCapitalize="none"
autoCorrect={false}
returnKeyType="search"
submitBehavior="blurAndSubmit"
onSubmitEditing={(event) => { setEditing(false); onSubmit?.(event.nativeEvent.text.trim()); }}
onFocus={() => { setFocused(true); setEditing(true); }}
onBlur={() => setFocused(false)}
underlineColorAndroid="transparent"
style={[styles.input, NO_WEB_RING, disabled && styles.muted]}
/>
</SearchShell>
);
}
type SearchShellProps = {
focused: boolean;
disabled: boolean;
showClear: boolean;
clearLabel?: string;
onClear?: () => void;
showCancel: boolean;
cancelLabel: string;
onCancel?: () => void;
children: ReactNode;
};
function SearchShell({ focused, disabled, showClear, clearLabel, onClear, showCancel, cancelLabel, onCancel, children }: SearchShellProps) {
const cancelText = <Text style={styles.cancelText}>{cancelLabel}</Text>;
const clearMark = <Text style={styles.clearGlyph}>×</Text>;
return (
<View style={styles.row}>
<View style={[styles.bar, focused && styles.focused, disabled && styles.disabled]}>
<View aria-hidden style={styles.lens}>
<View style={styles.ring} />
<View style={styles.handle} />
</View>
{children}
{showClear && (onClear
? <Pressable accessibilityRole="button" accessibilityLabel={clearLabel} onPress={onClear} hitSlop={6} style={styles.clear}>{clearMark}</Pressable>
: <View style={styles.clear}>{clearMark}</View>)}
</View>
{showCancel && (onCancel
? <Pressable accessibilityRole="button" onPress={onCancel} hitSlop={4} style={styles.cancel}>{cancelText}</Pressable>
: <View style={styles.cancel}>{cancelText}</View>)}
</View>
);
}
/** Read-only representation of a particular search field state. */
export function SearchBarFace({ value, placeholder, focused = false, disabled = false, cancelLabel = 'Cancel' }: {
value: string; placeholder: string; focused?: boolean; disabled?: boolean; cancelLabel?: string;
}) {
return (
<SearchShell focused={focused} disabled={disabled} showClear={!!value && !disabled}
showCancel={(focused || !!value) && !disabled} cancelLabel={cancelLabel}>
<Text numberOfLines={1} style={[styles.fieldValue, (!value || disabled) && styles.muted]}>{value || placeholder}</Text>
</SearchShell>
);
}
const NO_WEB_RING = Platform.OS === 'web' ? ({ outlineStyle: 'none' } as unknown as TextStyle) : undefined;
const styles = StyleSheet.create({
row: { flexDirection: 'row', gap: 10, alignItems: 'center' },
bar: {
flex: 1, minWidth: 0, flexDirection: 'row', alignItems: 'center', gap: 10, minHeight: 48,
paddingHorizontal: 12, borderRadius: radius.md, borderWidth: 1, borderColor: colors.outlineVariant, backgroundColor: colors.surfaceVariant,
},
focused: { borderColor: colors.primary, backgroundColor: colors.surface },
disabled: { opacity: 0.65 },
lens: { width: 18, height: 18 },
ring: { width: 13, height: 13, borderWidth: 1.75, borderRadius: 7, borderColor: colors.onSurfaceVariant },
handle: { position: 'absolute', left: 10, top: 12, width: 7, height: 2, borderRadius: 1, transform: [{ rotate: '45deg' }], backgroundColor: colors.onSurfaceVariant },
fieldValue: { minWidth: 0, flex: 1, color: colors.onSurface, fontSize: 16 },
input: { minWidth: 0, flex: 1, paddingVertical: 10, paddingHorizontal: 0, color: colors.onSurface, fontSize: 16 },
muted: { color: colors.onSurfaceVariant },
clear: { minWidth: 28, minHeight: 36, alignItems: 'center', justifyContent: 'center' },
clearGlyph: { color: colors.onSurfaceVariant, fontSize: 23, lineHeight: 26, textAlign: 'center' },
cancel: { minHeight: 44, alignItems: 'center', justifyContent: 'center' },
cancelText: { fontSize: 15, fontWeight: '600', color: colors.primary },
});
// Theme: Emerald, light, default corners. Shared by every Mobilens component; edit the values to match your app.
export const colors = {
background: '#fafafa',
surface: '#ffffff',
surfaceVariant: '#f4f4f5',
onBackground: '#18181b',
onSurface: '#18181b',
onSurfaceVariant: '#6b6b74',
outline: '#8a8a93',
outlineVariant: '#e4e4e7',
error: '#ba1a1a',
success: '#296b2a',
warning: '#b45309',
shadow: '#000000',
scrim: 'rgba(0,0,0,0.32)',
primary: '#006c50',
onPrimary: '#ffffff',
primaryContainer: '#82f8cb',
onPrimaryContainer: '#002116',
secondary: '#3e6374',
onSecondary: '#ffffff',
secondaryContainer: '#d8dfe2',
onSecondaryContainer: '#1c2d34',
tertiary: '#1f6862',
onTertiary: '#ffffff',
tertiaryContainer: '#d3e0df',
onTertiaryContainer: '#0e2f2c',
onError: '#ffffff',
onSuccess: '#ffffff',
onWarning: '#ffffff',
} as const;
export const radius = {
sm: 6,
md: 12,
lg: 20,
} as const;
// Theme: Emerald, light, default corners. Shared by every Mobilens component; edit the values to match your app.
export const colors = {
background: '#fafafa',
surface: '#ffffff',
surfaceVariant: '#f4f4f5',
onBackground: '#18181b',
onSurface: '#18181b',
onSurfaceVariant: '#6b6b74',
outline: '#8a8a93',
outlineVariant: '#e4e4e7',
error: '#ba1a1a',
success: '#296b2a',
warning: '#b45309',
shadow: '#000000',
scrim: 'rgba(0,0,0,0.32)',
primary: '#006c50',
onPrimary: '#ffffff',
primaryContainer: '#82f8cb',
onPrimaryContainer: '#002116',
secondary: '#3e6374',
onSecondary: '#ffffff',
secondaryContainer: '#d8dfe2',
onSecondaryContainer: '#1c2d34',
tertiary: '#1f6862',
onTertiary: '#ffffff',
tertiaryContainer: '#d3e0df',
onTertiaryContainer: '#0e2f2c',
onError: '#ffffff',
onSuccess: '#ffffff',
onWarning: '#ffffff',
} as const;
export const radius = {
sm: 6,
md: 12,
lg: 20,
} as const;
用法
import { useState } from 'react';
import { Text, View } from 'react-native';
import { SearchBar } from '@/components/mobilens/SearchBar';
export function ContactSearch({ names }: { names: string[] }) {
const [query, setQuery] = useState('');
const term = query.trim().toLocaleLowerCase();
const matches = names.filter((name) => name.toLocaleLowerCase().includes(term));
return (
<View>
<SearchBar value={query} onChangeText={setQuery} placeholder="Search contacts" />
{matches.map((name, index) => <Text key={name + index}>{name}</Text>)}
{!matches.length && <Text>No matching contacts</Text>}
</View>
);
}
import { useState } from 'react';
import { Text, View } from 'react-native';
import { SearchBar } from '@/components/mobilens/SearchBar';
export function ContactSearch({ names }: { names: string[] }) {
const [query, setQuery] = useState('');
const term = query.trim().toLocaleLowerCase();
const matches = names.filter((name) => name.toLocaleLowerCase().includes(term));
return (
<View>
<SearchBar value={query} onChangeText={setQuery} placeholder="Search contacts" />
{matches.map((name, index) => <Text key={name + index}>{name}</Text>)}
{!matches.length && <Text>No matching contacts</Text>}
</View>
);
}
API
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| value | string | – | Current query. An empty string represents all results. |
| onChangeText | (value: string) => void | – | Called for typing, paste, clear and cancel. Filter results using the new value. |
| placeholder? | string | 'Search' | Search hint and accessible name, for example "Search contacts". |
| onSubmit? | (query: string) => void | – | Receives the trimmed query when the user presses Search on the keyboard. |
| onCancel? | () => void | – | Enables the cancel action, called after clearing the query and dismissing the keyboard. |
| clearLabel? | string | 'Clear search' | Accessible name of the clear button. |
| cancelLabel? | string | 'Cancel' | Visible label for the cancel action. |
| disabled? | boolean | false | Prevents edits while still displaying the current query. |
| autoFocus? | boolean | false | Focus the field when it mounts. |
Add the "Search bar" control to this Expo or React Native project. Create the files below with exactly this content. Do not restyle or rewrite them.
FILES
Put them in components/mobilens/. theme.ts is shared by every Mobilens component: if it already exists, keep one copy, and skip any other file that is already there with the same content. If the project has no "@/..." import alias, change those imports to relative paths.
--- components/mobilens/theme.ts ---
// Theme: Emerald, light, default corners. Shared by every Mobilens component; edit the values to match your app.
export const colors = {
background: '#fafafa',
surface: '#ffffff',
surfaceVariant: '#f4f4f5',
onBackground: '#18181b',
onSurface: '#18181b',
onSurfaceVariant: '#6b6b74',
outline: '#8a8a93',
outlineVariant: '#e4e4e7',
error: '#ba1a1a',
success: '#296b2a',
warning: '#b45309',
shadow: '#000000',
scrim: 'rgba(0,0,0,0.32)',
primary: '#006c50',
onPrimary: '#ffffff',
primaryContainer: '#82f8cb',
onPrimaryContainer: '#002116',
secondary: '#3e6374',
onSecondary: '#ffffff',
secondaryContainer: '#d8dfe2',
onSecondaryContainer: '#1c2d34',
tertiary: '#1f6862',
onTertiary: '#ffffff',
tertiaryContainer: '#d3e0df',
onTertiaryContainer: '#0e2f2c',
onError: '#ffffff',
onSuccess: '#ffffff',
onWarning: '#ffffff',
} as const;
export const radius = {
sm: 6,
md: 12,
lg: 20,
} as const;
--- end of theme.ts ---
--- components/mobilens/SearchBar.tsx ---
import { useRef, useState, type ComponentRef, type ReactNode } from 'react';
import { Platform, Pressable, StyleSheet, Text, TextInput, View, type TextStyle } from 'react-native';
import { colors, radius } from './theme';
export type SearchBarProps = {
/** Current query. An empty string represents all results. */
value: string;
/** Called for typing, paste, clear and cancel. Filter results using the new value. */
onChangeText: (value: string) => void;
/** Search hint and accessible name, for example "Search contacts". */
placeholder?: string;
/** Receives the trimmed query when the user presses Search on the keyboard. */
onSubmit?: (query: string) => void;
/** Enables the cancel action, called after clearing the query and dismissing the keyboard. */
onCancel?: () => void;
/** Accessible name of the clear button. */
clearLabel?: string;
/** Visible label for the cancel action. */
cancelLabel?: string;
/** Prevents edits while still displaying the current query. */
disabled?: boolean;
/** Focus the field when it mounts. */
autoFocus?: boolean;
};
export function SearchBar({
value, onChangeText, placeholder = 'Search', onSubmit, onCancel,
clearLabel = 'Clear search', cancelLabel = 'Cancel', disabled = false, autoFocus = false,
}: SearchBarProps) {
const inputRef = useRef<ComponentRef<typeof TextInput>>(null);
const [focused, setFocused] = useState(false);
const [editing, setEditing] = useState(false);
function clear() {
onChangeText('');
inputRef.current?.focus();
}
function cancel() {
onChangeText('');
inputRef.current?.blur();
setEditing(false);
onCancel?.();
}
return (
<SearchShell focused={focused && !disabled} disabled={disabled} clearLabel={clearLabel}
showClear={!!value && !disabled} onClear={clear} cancelLabel={cancelLabel}
showCancel={!!onCancel && (editing || !!value) && !disabled} onCancel={cancel}>
<TextInput
ref={inputRef}
value={value}
placeholder={placeholder}
placeholderTextColor={colors.onSurfaceVariant}
accessibilityLabel={placeholder}
accessibilityState={{ disabled }}
editable={!disabled}
onChangeText={onChangeText}
autoFocus={autoFocus}
autoCapitalize="none"
autoCorrect={false}
returnKeyType="search"
submitBehavior="blurAndSubmit"
onSubmitEditing={(event) => { setEditing(false); onSubmit?.(event.nativeEvent.text.trim()); }}
onFocus={() => { setFocused(true); setEditing(true); }}
onBlur={() => setFocused(false)}
underlineColorAndroid="transparent"
style={[styles.input, NO_WEB_RING, disabled && styles.muted]}
/>
</SearchShell>
);
}
type SearchShellProps = {
focused: boolean;
disabled: boolean;
showClear: boolean;
clearLabel?: string;
onClear?: () => void;
showCancel: boolean;
cancelLabel: string;
onCancel?: () => void;
children: ReactNode;
};
function SearchShell({ focused, disabled, showClear, clearLabel, onClear, showCancel, cancelLabel, onCancel, children }: SearchShellProps) {
const cancelText = <Text style={styles.cancelText}>{cancelLabel}</Text>;
const clearMark = <Text style={styles.clearGlyph}>×</Text>;
return (
<View style={styles.row}>
<View style={[styles.bar, focused && styles.focused, disabled && styles.disabled]}>
<View aria-hidden style={styles.lens}>
<View style={styles.ring} />
<View style={styles.handle} />
</View>
{children}
{showClear && (onClear
? <Pressable accessibilityRole="button" accessibilityLabel={clearLabel} onPress={onClear} hitSlop={6} style={styles.clear}>{clearMark}</Pressable>
: <View style={styles.clear}>{clearMark}</View>)}
</View>
{showCancel && (onCancel
? <Pressable accessibilityRole="button" onPress={onCancel} hitSlop={4} style={styles.cancel}>{cancelText}</Pressable>
: <View style={styles.cancel}>{cancelText}</View>)}
</View>
);
}
/** Read-only representation of a particular search field state. */
export function SearchBarFace({ value, placeholder, focused = false, disabled = false, cancelLabel = 'Cancel' }: {
value: string; placeholder: string; focused?: boolean; disabled?: boolean; cancelLabel?: string;
}) {
return (
<SearchShell focused={focused} disabled={disabled} showClear={!!value && !disabled}
showCancel={(focused || !!value) && !disabled} cancelLabel={cancelLabel}>
<Text numberOfLines={1} style={[styles.fieldValue, (!value || disabled) && styles.muted]}>{value || placeholder}</Text>
</SearchShell>
);
}
const NO_WEB_RING = Platform.OS === 'web' ? ({ outlineStyle: 'none' } as unknown as TextStyle) : undefined;
const styles = StyleSheet.create({
row: { flexDirection: 'row', gap: 10, alignItems: 'center' },
bar: {
flex: 1, minWidth: 0, flexDirection: 'row', alignItems: 'center', gap: 10, minHeight: 48,
paddingHorizontal: 12, borderRadius: radius.md, borderWidth: 1, borderColor: colors.outlineVariant, backgroundColor: colors.surfaceVariant,
},
focused: { borderColor: colors.primary, backgroundColor: colors.surface },
disabled: { opacity: 0.65 },
lens: { width: 18, height: 18 },
ring: { width: 13, height: 13, borderWidth: 1.75, borderRadius: 7, borderColor: colors.onSurfaceVariant },
handle: { position: 'absolute', left: 10, top: 12, width: 7, height: 2, borderRadius: 1, transform: [{ rotate: '45deg' }], backgroundColor: colors.onSurfaceVariant },
fieldValue: { minWidth: 0, flex: 1, color: colors.onSurface, fontSize: 16 },
input: { minWidth: 0, flex: 1, paddingVertical: 10, paddingHorizontal: 0, color: colors.onSurface, fontSize: 16 },
muted: { color: colors.onSurfaceVariant },
clear: { minWidth: 28, minHeight: 36, alignItems: 'center', justifyContent: 'center' },
clearGlyph: { color: colors.onSurfaceVariant, fontSize: 23, lineHeight: 26, textAlign: 'center' },
cancel: { minHeight: 44, alignItems: 'center', justifyContent: 'center' },
cancelText: { fontSize: 15, fontWeight: '600', color: colors.primary },
});
--- end of SearchBar.tsx ---
HOW TO USE IT
import { useState } from 'react';
import { Text, View } from 'react-native';
import { SearchBar } from '@/components/mobilens/SearchBar';
export function ContactSearch({ names }: { names: string[] }) {
const [query, setQuery] = useState('');
const term = query.trim().toLocaleLowerCase();
const matches = names.filter((name) => name.toLocaleLowerCase().includes(term));
return (
<View>
<SearchBar value={query} onChangeText={setQuery} placeholder="Search contacts" />
{matches.map((name, index) => <Text key={name + index}>{name}</Text>)}
{!matches.length && <Text>No matching contacts</Text>}
</View>
);
}
STATES: all 5 must work in the screen where you use it
- Browse contacts: when the search field has no query.. Show the directory. The field has a search icon and a visible hint.
- Search active: when the user taps the search field.. Open the keyboard. Cancel clears the query and leaves search.
- Results: when the typed or pasted query matches a contact.. Update the list as the query changes. Clear restores the directory and keeps the input ready for typing.
- No matches: when no contact matches the current text.. Keep the query visible so it can be edited or cleared.
- Disabled: when the containing screen disables search.. Show a read-only field without clear or cancel actions.
ACCESSIBILITY
- The field uses its search hint as its accessible name.
- Clear and cancel have distinct accessible names. The search glyph is decorative.
- Submitting with the keyboard keeps the current query.
WHEN YOU FINISH
List every state and rule above and say how you verified each one.