about summary refs log tree commit diff
path: root/src/view/com/util/forms/SearchInput.tsx
blob: 5a21d8fdd03336c54bd01676dba73cd9c177ccb3 (plain) (blame)
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
import React from 'react'
import {
  StyleProp,
  StyleSheet,
  TextInput,
  TouchableOpacity,
  View,
  ViewStyle,
} from 'react-native'
import {
  FontAwesomeIcon,
  FontAwesomeIconStyle,
} from '@fortawesome/react-native-fontawesome'
import {HITSLOP_10} from 'lib/constants'
import {MagnifyingGlassIcon} from 'lib/icons'
import {useTheme} from 'lib/ThemeContext'
import {usePalette} from 'lib/hooks/usePalette'
import {useLingui} from '@lingui/react'
import {msg} from '@lingui/macro'

interface Props {
  query: string
  setIsInputFocused?: (v: boolean) => void
  onChangeQuery: (v: string) => void
  onPressCancelSearch: () => void
  onSubmitQuery: () => void
  style?: StyleProp<ViewStyle>
}

export interface SearchInputRef {
  focus?: () => void
}

export const SearchInput = React.forwardRef<SearchInputRef, Props>(
  function SearchInput(
    {
      query,
      setIsInputFocused,
      onChangeQuery,
      onPressCancelSearch,
      onSubmitQuery,
      style,
    },
    ref,
  ) {
    const theme = useTheme()
    const pal = usePalette('default')
    const {_} = useLingui()
    const textInput = React.useRef<TextInput>(null)

    const onPressCancelSearchInner = React.useCallback(() => {
      onPressCancelSearch()
      textInput.current?.blur()
    }, [onPressCancelSearch, textInput])

    React.useImperativeHandle(ref, () => ({
      focus: () => textInput.current?.focus(),
      blur: () => textInput.current?.blur(),
    }))

    return (
      <View style={[pal.viewLight, styles.container, style]}>
        <MagnifyingGlassIcon style={[pal.icon, styles.icon]} size={21} />
        <TextInput
          testID="searchTextInput"
          ref={textInput}
          placeholder={_(msg`Search`)}
          placeholderTextColor={pal.colors.textLight}
          selectTextOnFocus
          returnKeyType="search"
          value={query}
          style={[pal.text, styles.input]}
          keyboardAppearance={theme.colorScheme}
          onFocus={() => setIsInputFocused?.(true)}
          onBlur={() => setIsInputFocused?.(false)}
          onChangeText={onChangeQuery}
          onSubmitEditing={onSubmitQuery}
          accessibilityRole="search"
          accessibilityLabel={_(msg`Search`)}
          accessibilityHint=""
          autoCorrect={false}
          autoCapitalize="none"
        />
        {query ? (
          <TouchableOpacity
            onPress={onPressCancelSearchInner}
            accessibilityRole="button"
            accessibilityLabel={_(msg`Clear search query`)}
            accessibilityHint=""
            hitSlop={HITSLOP_10}>
            <FontAwesomeIcon
              icon="xmark"
              size={16}
              style={pal.textLight as FontAwesomeIconStyle}
            />
          </TouchableOpacity>
        ) : undefined}
      </View>
    )
  },
)

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'row',
    alignItems: 'center',
    borderRadius: 30,
    paddingHorizontal: 12,
    paddingVertical: 8,
  },
  icon: {
    marginRight: 6,
    alignSelf: 'center',
  },
  input: {
    flex: 1,
    fontSize: 17,
    minWidth: 0, // overflow mitigation for firefox
  },
  cancelBtn: {
    paddingLeft: 10,
  },
})