mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import React from 'react' 2import {type TextInput, View} from 'react-native' 3import {msg} from '@lingui/macro' 4import {useLingui} from '@lingui/react' 5 6import {HITSLOP_10} from '#/lib/constants' 7import {isNative} from '#/platform/detection' 8import {atoms as a, useTheme} from '#/alf' 9import {Button, ButtonIcon} from '#/components/Button' 10import * as TextField from '#/components/forms/TextField' 11import {MagnifyingGlass2_Stroke2_Corner0_Rounded as MagnifyingGlassIcon} from '#/components/icons/MagnifyingGlass2' 12import {TimesLarge_Stroke2_Corner0_Rounded as X} from '#/components/icons/Times' 13 14type SearchInputProps = Omit<TextField.InputProps, 'label'> & { 15 label?: TextField.InputProps['label'] 16 /** 17 * Called when the user presses the (X) button 18 */ 19 onClearText?: () => void 20} 21 22export const SearchInput = React.forwardRef<TextInput, SearchInputProps>( 23 function SearchInput({value, label, onClearText, ...rest}, ref) { 24 const t = useTheme() 25 const {_} = useLingui() 26 const showClear = value && value.length > 0 27 28 return ( 29 <View style={[a.w_full, a.relative]}> 30 <TextField.Root> 31 <TextField.Icon icon={MagnifyingGlassIcon} /> 32 <TextField.Input 33 inputRef={ref} 34 label={label || _(msg`Search`)} 35 value={value} 36 placeholder={_(msg`Search`)} 37 returnKeyType="search" 38 keyboardAppearance={t.scheme} 39 selectTextOnFocus={isNative} 40 autoFocus={false} 41 accessibilityRole="search" 42 autoCorrect={false} 43 autoComplete="off" 44 autoCapitalize="none" 45 style={[ 46 showClear 47 ? { 48 paddingRight: 24, 49 } 50 : {}, 51 ]} 52 {...rest} 53 /> 54 </TextField.Root> 55 56 {showClear && ( 57 <View 58 style={[ 59 a.absolute, 60 a.z_20, 61 a.my_auto, 62 a.inset_0, 63 a.justify_center, 64 a.pr_sm, 65 {left: 'auto'}, 66 ]}> 67 <Button 68 testID="searchTextInputClearBtn" 69 onPress={onClearText} 70 label={_(msg`Clear search query`)} 71 hitSlop={HITSLOP_10} 72 size="tiny" 73 shape="round" 74 variant="ghost" 75 color="secondary"> 76 <ButtonIcon icon={X} size="xs" /> 77 </Button> 78 </View> 79 )} 80 </View> 81 ) 82 }, 83)