forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
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 {useEnableSquareButtons} from '#/state/preferences/enable-square-buttons'
8import {atoms as a, useTheme} from '#/alf'
9import {Button, ButtonIcon} from '#/components/Button'
10import * as TextField from '#/components/forms/TextField'
11import {MagnifyingGlass_Stroke2_Corner0_Rounded as MagnifyingGlassIcon} from '#/components/icons/MagnifyingGlass'
12import {TimesLarge_Stroke2_Corner0_Rounded as X} from '#/components/icons/Times'
13import {IS_NATIVE} from '#/env'
14
15type SearchInputProps = Omit<TextField.InputProps, 'label'> & {
16 label?: TextField.InputProps['label']
17 /**
18 * Called when the user presses the (X) button
19 */
20 onClearText?: () => void
21}
22
23export const SearchInput = React.forwardRef<TextInput, SearchInputProps>(
24 function SearchInput({value, label, onClearText, ...rest}, ref) {
25 const t = useTheme()
26 const {_} = useLingui()
27 const showClear = value && value.length > 0
28
29 const enableSquareButtons = useEnableSquareButtons()
30
31 return (
32 <View style={[a.w_full, a.relative]}>
33 <TextField.Root>
34 <TextField.Icon icon={MagnifyingGlassIcon} />
35 <TextField.Input
36 inputRef={ref}
37 label={label || _(msg`Search`)}
38 value={value}
39 placeholder={_(msg`Search`)}
40 returnKeyType="search"
41 keyboardAppearance={t.scheme}
42 selectTextOnFocus={IS_NATIVE}
43 autoFocus={false}
44 accessibilityRole="search"
45 autoCorrect={false}
46 autoComplete="off"
47 autoCapitalize="none"
48 style={[
49 showClear
50 ? {
51 paddingRight: 24,
52 }
53 : {},
54 ]}
55 {...rest}
56 />
57 </TextField.Root>
58
59 {showClear && (
60 <View
61 style={[
62 a.absolute,
63 a.z_20,
64 a.my_auto,
65 a.inset_0,
66 a.justify_center,
67 a.pr_sm,
68 {left: 'auto'},
69 ]}>
70 <Button
71 testID="searchTextInputClearBtn"
72 onPress={onClearText}
73 label={_(msg`Clear search query`)}
74 hitSlop={HITSLOP_10}
75 size="tiny"
76 shape={enableSquareButtons ? 'square' : 'round'}
77 variant="ghost"
78 color="secondary">
79 <ButtonIcon icon={X} size="xs" />
80 </Button>
81 </View>
82 )}
83 </View>
84 )
85 },
86)