// 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 (
setModalVisible(true)}
activeOpacity={0.7}>
setModalVisible(false)}>
setModalVisible(false)}>
{search.length > 0 && (
setSearch("")}
hitSlop={{top: 10, bottom: 10, left: 10, right: 10}}>
)}
item.value}
keyboardShouldPersistTaps="always"
style={themed($optionsList)}
renderItem={({item}) => (
{
onValueChange(item.value)
setModalVisible(false)
setSearch("")
}}>
{item.value === value ? (
) : (
)}
)}
ListEmptyComponent={
{translate("appSettings:noOptionsFound")}
}
/>
)
}
const $container: ThemedStyle = () => ({
width: "100%",
})
const $selectButton: ThemedStyle = ({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 = ({colors}) => ({
flex: 1,
fontSize: 14,
fontWeight: "600",
color: colors.text,
})
const $valueContainer: ThemedStyle = ({spacing}) => ({
flexDirection: "row",
alignItems: "center",
gap: spacing.s1,
})
const $valueText: ThemedStyle = ({colors}) => ({
fontSize: 14,
color: colors.textDim,
})
const $modalOverlay: ThemedStyle = () => ({
flex: 1,
backgroundColor: "rgba(0,0,0,0.25)",
justifyContent: "center",
alignItems: "center",
})
const $modalContent: ThemedStyle = ({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 = ({spacing}) => ({
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center",
marginBottom: spacing.s3,
})
const $modalLabel: ThemedStyle = ({colors}) => ({
fontSize: 16,
fontWeight: "600",
color: colors.text,
})
const $searchContainer: ThemedStyle = ({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 = ({colors, spacing}) => ({
flex: 1,
fontSize: 16,
color: colors.text,
marginHorizontal: spacing.s2,
paddingVertical: 0,
})
const $optionsList: ThemedStyle = () => ({
flexGrow: 0,
maxHeight: 300,
})
const $optionItem: ThemedStyle = ({spacing}) => ({
flexDirection: "row",
alignItems: "center",
paddingVertical: spacing.s3,
paddingRight: spacing.s4,
})
const $optionText: ThemedStyle = ({colors, spacing}) => ({
flex: 1,
fontSize: 16,
color: colors.text,
marginLeft: spacing.s2,
})
const $emptyText: ThemedStyle = ({colors}) => ({
fontSize: 15,
textAlign: "center",
marginTop: 20,
color: colors.textDim,
})
export default SelectWithSearchSetting