Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | 6x 6x 6x 2x 2x 6x 6x 6x 1x 6x | import { useState, useCallback } from 'react';
import { Input, XStack, type SizeTokens } from 'tamagui';
import { IconSymbol } from '@/src/components/ui/IconSymbol';
interface SearchBoxProps {
size?: SizeTokens;
value?: string;
placeholder?: string;
onChangeText?: (text: string) => void;
onSearch?: (text: string) => void; // 点击搜索按钮或按回车时触发
onClear?: () => void;
autoFocus?: boolean;
disabled?: boolean;
showSearchButton?: boolean; // 是否显示搜索按钮
testID?: string; // E2E 测试 ID
}
export default function SearchBox({
size = '$4',
value = '',
placeholder = '搜索...',
onChangeText,
onSearch,
onClear,
autoFocus = false,
disabled = false,
showSearchButton = true,
testID,
}: SearchBoxProps) {
// 内部状态管理输入值(如果没有外部控制)
const [internalValue, setInternalValue] = useState(value);
const currentValue = value !== undefined ? value : internalValue;
const handleTextChange = useCallback(
(text: string) => {
setInternalValue(text);
onChangeText?.(text);
},
[onChangeText]
);
const handleClear = useCallback(() => {
setInternalValue('');
onChangeText?.('');
onClear?.();
onSearch?.(''); // 清空时也触发搜索
}, [onChangeText, onClear, onSearch]);
const handleSearch = useCallback(() => {
onSearch?.(currentValue);
}, [onSearch, currentValue]);
const handleSubmitEditing = useCallback(() => {
onSearch?.(currentValue);
}, [onSearch, currentValue]);
return (
<XStack
testID={testID || 'search-box'}
alignItems="center"
gap="$2"
paddingLeft="$3"
paddingRight="$2"
paddingVertical="$2"
backgroundColor="$background"
borderRadius="$4"
borderWidth={1}
borderColor="$borderColor"
focusStyle={{ borderColor: '$borderColorFocus' }}
>
<IconSymbol name="magnifyingglass" size={20} color="$foregroundSubtle" />
<Input
testID="search-input"
flex={1}
size={size}
placeholder={placeholder}
placeholderTextColor="$placeholderColor"
value={currentValue}
onChangeText={handleTextChange}
onSubmitEditing={handleSubmitEditing}
autoFocus={autoFocus}
disabled={disabled}
backgroundColor="transparent"
borderWidth={0}
color="$color"
focusStyle={{ borderWidth: 0, outlineWidth: 0 }}
paddingHorizontal="$2"
paddingVertical={0}
height={36}
returnKeyType="search"
autoCapitalize="none"
autoCorrect={false}
clearButtonMode="never"
/>
{/* 清除按钮 */}
{currentValue && currentValue.length > 0 && (
<XStack
onPress={handleClear}
padding="$1"
borderRadius={9999}
backgroundColor="$backgroundMuted"
pressStyle={{ backgroundColor: '$backgroundPress', scale: 0.95 }}
cursor="pointer"
>
<IconSymbol name="xmark.circle.fill" size={18} color="$foregroundMuted" />
</XStack>
)}
{/* 搜索按钮 */}
{showSearchButton && (
<XStack
testID="search-submit"
onPress={handleSearch}
paddingHorizontal="$3"
paddingVertical="$2"
borderRadius="$3"
backgroundColor="#FEBE98"
pressStyle={{ backgroundColor: '#E5A882', scale: 0.98 }}
cursor="pointer"
>
<IconSymbol name="magnifyingglass" size={18} color="white" />
</XStack>
)}
</XStack>
);
}
export type { SearchBoxProps };
|