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 | 2x 2x 2x 8x 8x 8x 3x 3x 8x 8x 1x 8x 1x 1x 8x 1x 1x 8x 8x 2x | /**
* GlassSearchBar - 毛玻璃搜索栏组件
*
* 带有毛玻璃效果的搜索输入框
*/
import React, { memo, useState, useCallback } from 'react';
import { Pressable, Platform } from 'react-native';
import { Search, X } from '@tamagui/lucide-icons';
import { styled, XStack, Input, Stack } from 'tamagui';
export interface GlassSearchBarProps {
placeholder?: string;
value?: string;
onChangeText?: (text: string) => void;
onSubmit?: (text: string) => void;
onFocus?: () => void;
onBlur?: () => void;
}
const SearchContainer = styled(XStack, {
name: 'SearchContainer',
height: 44,
borderRadius: '$6',
alignItems: 'center',
paddingHorizontal: '$3',
gap: '$2',
overflow: 'hidden',
});
const SearchInput = styled(Input, {
name: 'SearchInput',
flex: 1,
height: '100%',
fontSize: '$3',
backgroundColor: 'transparent',
borderWidth: 0,
placeholderTextColor: '$colorMuted',
color: '$color',
paddingHorizontal: 0,
});
const IconButton = styled(Stack, {
name: 'IconButton',
width: 28,
height: 28,
borderRadius: 14,
alignItems: 'center',
justifyContent: 'center',
});
function GlassSearchBarComponent({
placeholder = '搜索话题、品种...',
value = '',
onChangeText,
onSubmit,
onFocus,
onBlur,
}: GlassSearchBarProps) {
const [localValue, setLocalValue] = useState(value);
const [isFocused, setIsFocused] = useState(false);
const handleChangeText = useCallback(
(text: string) => {
setLocalValue(text);
onChangeText?.(text);
},
[onChangeText]
);
const handleClear = useCallback(() => {
setLocalValue('');
onChangeText?.('');
}, [onChangeText]);
const handleSubmit = useCallback(() => {
onSubmit?.(localValue);
}, [localValue, onSubmit]);
const handleFocus = useCallback(() => {
setIsFocused(true);
onFocus?.();
}, [onFocus]);
const handleBlur = useCallback(() => {
setIsFocused(false);
onBlur?.();
}, [onBlur]);
const content = (
<SearchContainer
backgroundColor={Platform.OS === 'ios' ? 'transparent' : '$backgroundMuted'}
borderWidth={isFocused ? 1 : 0}
borderColor={isFocused ? '$primary' : 'transparent'}
>
<Search size={18} color="$color6" />
<SearchInput
value={localValue}
onChangeText={handleChangeText}
onSubmitEditing={handleSubmit}
onFocus={handleFocus}
onBlur={handleBlur}
placeholder={placeholder}
returnKeyType="search"
/>
{localValue.length > 0 && (
<Pressable onPress={handleClear}>
<IconButton backgroundColor="$color5">
<X size={14} color="$background" />
</IconButton>
</Pressable>
)}
</SearchContainer>
);
return content;
}
export const GlassSearchBar = memo(GlassSearchBarComponent);
|