File size: 8,783 Bytes
68465df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// SelectWithSearchSetting.tsx
import {useState, useMemo, useEffect} from "react"
import {
  View,
  ViewStyle,
  TextStyle,
  TextInput,
  Modal,
  TouchableOpacity,
  FlatList,
  Pressable,
  KeyboardAvoidingView,
  Platform,
  TouchableWithoutFeedback,
} from "react-native"
import MaterialCommunityIcons from "react-native-vector-icons/MaterialCommunityIcons"

import {Icon, Text} from "@/components/ignite"
import {useAppTheme} from "@/contexts/ThemeContext"
import {translate} from "@/i18n"
import {ThemedStyle} from "@/theme"

import SearchIcon from "assets/icons/component/SearchIcon"

type Option = {
  label: string
  value: string
}

type SelectWithSearchSettingProps = {
  label: string
  value: string
  options: Option[]
  onValueChange: (value: string) => void
  defaultValue?: string
  isFirst?: boolean
  isLast?: boolean
}

const SelectWithSearchSetting = ({

  label,

  value,

  options,

  onValueChange,

  defaultValue,

  isFirst,

  isLast,

}: SelectWithSearchSettingProps) => {
  const {theme, themed} = useAppTheme()

  const [search, setSearch] = useState("")
  const [modalVisible, setModalVisible] = useState(false)

  const groupedStyle: ViewStyle | undefined =
    isFirst !== undefined || isLast !== undefined
      ? {
          borderTopLeftRadius: isFirst ? theme.spacing.s4 : theme.spacing.s1,
          borderTopRightRadius: isFirst ? theme.spacing.s4 : theme.spacing.s1,
          borderBottomLeftRadius: isLast ? theme.spacing.s4 : theme.spacing.s1,
          borderBottomRightRadius: isLast ? theme.spacing.s4 : theme.spacing.s1,
          marginBottom: isLast ? 0 : theme.spacing.s2,
        }
      : undefined

  // If the current value doesn't match any option, use the defaultValue
  useEffect(() => {
    if (options.length > 0 && !options.find((option) => option.value === value)) {
      // Value doesn't match any option
      if (defaultValue !== undefined && options.find((option) => option.value === defaultValue)) {
        // Default value exists and is valid, use it
        onValueChange(defaultValue)
      }
    }
  }, [value, options, defaultValue, onValueChange])

  // Filter options based on search
  const filteredOptions = useMemo(() => {
    if (!search) return options
    return options.filter((option) => option.label.toLowerCase().includes(search.toLowerCase()))
  }, [search, options])

  const selectedLabel = options.find((option) => option.value === value)?.label || translate("appSettings:select")

  return (
    <View style={themed($container)}>

      <TouchableOpacity

        style={[themed($selectButton), groupedStyle]}

        onPress={() => setModalVisible(true)}

        activeOpacity={0.7}>

        <Text text={label} style={themed($label)} />

        <View style={themed($valueContainer)}>

          <Text text={selectedLabel} style={themed($valueText)} />

          <Icon icon="caretRight" size={16} color={theme.colors.textDim} />

        </View>

      </TouchableOpacity>



      <Modal

        visible={modalVisible}

        animationType="fade"

        transparent={true}

        onRequestClose={() => setModalVisible(false)}>

        <KeyboardAvoidingView behavior={Platform.OS === "ios" ? "padding" : "height"} style={{flex: 1}}>

          <TouchableWithoutFeedback onPress={() => setModalVisible(false)}>

            <View style={themed($modalOverlay)}>

              <TouchableWithoutFeedback>

                <View style={themed($modalContent)}>

                  <View style={themed($modalHeader)}>

                    <Text text={label} style={themed($modalLabel)} />

                  </View>

                  <View style={themed($searchContainer)}>

                    <SearchIcon size={20} color={theme.colors.textDim} />

                    <TextInput

                      style={themed($searchInput)}

                      placeholder={translate("appSettings:search")}

                      placeholderTextColor={theme.colors.textDim}

                      value={search}

                      onChangeText={setSearch}

                      autoFocus

                    />

                    {search.length > 0 && (

                      <TouchableOpacity

                        onPress={() => setSearch("")}

                        hitSlop={{top: 10, bottom: 10, left: 10, right: 10}}>

                        <MaterialCommunityIcons name="close" size={20} color={theme.colors.textDim} />

                      </TouchableOpacity>

                    )}

                  </View>

                  <FlatList

                    data={filteredOptions}

                    keyExtractor={(item) => item.value}

                    keyboardShouldPersistTaps="always"

                    style={themed($optionsList)}

                    renderItem={({item}) => (

                      <Pressable

                        style={themed($optionItem)}

                        onPress={() => {

                          onValueChange(item.value)

                          setModalVisible(false)

                          setSearch("")

                        }}>

                        {item.value === value ? (

                          <MaterialCommunityIcons name="check" size={24} color={theme.colors.primary} />

                        ) : (

                          <View style={{width: 24, height: 24}} />

                        )}

                        <Text text={item.label} style={themed($optionText)} />

                      </Pressable>

                    )}

                    ListEmptyComponent={

                      <Text style={themed($emptyText)}>{translate("appSettings:noOptionsFound")}</Text>

                    }

                  />

                </View>

              </TouchableWithoutFeedback>

            </View>

          </TouchableWithoutFeedback>

        </KeyboardAvoidingView>

      </Modal>

    </View>
  )
}

const $container: ThemedStyle<ViewStyle> = () => ({
  width: "100%",
})

const $selectButton: ThemedStyle<ViewStyle> = ({colors, spacing}) => ({
  flexDirection: "row",
  justifyContent: "space-between",
  alignItems: "center",
  width: "100%",
  backgroundColor: colors.primary_foreground,
  paddingVertical: spacing.s4,
  paddingHorizontal: spacing.s4,
  borderRadius: spacing.s4,
})

const $label: ThemedStyle<TextStyle> = ({colors}) => ({
  flex: 1,
  fontSize: 14,
  fontWeight: "600",
  color: colors.text,
})

const $valueContainer: ThemedStyle<ViewStyle> = ({spacing}) => ({
  flexDirection: "row",
  alignItems: "center",
  gap: spacing.s1,
})

const $valueText: ThemedStyle<TextStyle> = ({colors}) => ({
  fontSize: 14,
  color: colors.textDim,
})

const $modalOverlay: ThemedStyle<ViewStyle> = () => ({
  flex: 1,
  backgroundColor: "rgba(0,0,0,0.25)",
  justifyContent: "center",
  alignItems: "center",
})

const $modalContent: ThemedStyle<ViewStyle> = ({colors, spacing}) => ({
  width: "90%",
  maxHeight: "70%",
  backgroundColor: colors.primary_foreground,
  borderRadius: spacing.s4,
  padding: spacing.s4,
  shadowColor: "#000",
  shadowOffset: {width: 0, height: 2},
  shadowOpacity: 0.2,
  shadowRadius: spacing.s2,
  elevation: 5,
})

const $modalHeader: ThemedStyle<ViewStyle> = ({spacing}) => ({
  flexDirection: "row",
  justifyContent: "space-between",
  alignItems: "center",
  marginBottom: spacing.s3,
})

const $modalLabel: ThemedStyle<TextStyle> = ({colors}) => ({
  fontSize: 16,
  fontWeight: "600",
  color: colors.text,
})

const $searchContainer: ThemedStyle<ViewStyle> = ({colors, spacing}) => ({
  flexDirection: "row",
  alignItems: "center",
  borderWidth: 1,
  borderColor: colors.border,
  backgroundColor: colors.background,
  borderRadius: 100,
  marginBottom: spacing.s3,
  paddingHorizontal: spacing.s3,
  paddingVertical: spacing.s2,
})

const $searchInput: ThemedStyle<TextStyle> = ({colors, spacing}) => ({
  flex: 1,
  fontSize: 16,
  color: colors.text,
  marginHorizontal: spacing.s2,
  paddingVertical: 0,
})

const $optionsList: ThemedStyle<ViewStyle> = () => ({
  flexGrow: 0,
  maxHeight: 300,
})

const $optionItem: ThemedStyle<ViewStyle> = ({spacing}) => ({
  flexDirection: "row",
  alignItems: "center",
  paddingVertical: spacing.s3,
  paddingRight: spacing.s4,
})

const $optionText: ThemedStyle<TextStyle> = ({colors, spacing}) => ({
  flex: 1,
  fontSize: 16,
  color: colors.text,
  marginLeft: spacing.s2,
})

const $emptyText: ThemedStyle<TextStyle> = ({colors}) => ({
  fontSize: 15,
  textAlign: "center",
  marginTop: 20,
  color: colors.textDim,
})

export default SelectWithSearchSetting