2022-11-30 22:42:42 +01:00
|
|
|
import { StyleConstants } from '@utils/styles/constants'
|
|
|
|
import { useTheme } from '@utils/styles/ThemeManager'
|
|
|
|
import React from 'react'
|
|
|
|
import { Pressable, View } from 'react-native'
|
|
|
|
import haptics from './haptics'
|
|
|
|
import Icon from './Icon'
|
|
|
|
import { ParseEmojis } from './Parse'
|
|
|
|
import CustomText from './Text'
|
|
|
|
|
|
|
|
export interface Props {
|
2023-01-26 00:57:48 +01:00
|
|
|
title?: string
|
|
|
|
|
2022-11-30 22:42:42 +01:00
|
|
|
multiple?: boolean
|
|
|
|
options: { selected: boolean; content: string }[]
|
|
|
|
setOptions: React.Dispatch<React.SetStateAction<{ selected: boolean; content: string }[]>>
|
2022-12-22 01:21:51 +01:00
|
|
|
disabled?: boolean
|
2023-01-26 00:57:48 +01:00
|
|
|
invalid?: boolean
|
2022-11-30 22:42:42 +01:00
|
|
|
}
|
|
|
|
|
2022-12-22 01:21:51 +01:00
|
|
|
const Selections: React.FC<Props> = ({
|
2023-01-26 00:57:48 +01:00
|
|
|
title,
|
2022-12-22 01:21:51 +01:00
|
|
|
multiple = false,
|
|
|
|
options,
|
|
|
|
setOptions,
|
2023-01-26 00:57:48 +01:00
|
|
|
disabled = false,
|
|
|
|
invalid = false
|
2022-12-22 01:21:51 +01:00
|
|
|
}) => {
|
2022-11-30 22:42:42 +01:00
|
|
|
const { colors } = useTheme()
|
|
|
|
|
2023-01-25 00:15:46 +01:00
|
|
|
const isSelected = (index: number) =>
|
2022-11-30 22:42:42 +01:00
|
|
|
options[index].selected
|
2023-01-25 00:15:46 +01:00
|
|
|
? multiple
|
|
|
|
? 'check-square'
|
|
|
|
: 'check-circle'
|
|
|
|
: multiple
|
|
|
|
? 'square'
|
|
|
|
: 'circle'
|
2022-11-30 22:42:42 +01:00
|
|
|
|
|
|
|
return (
|
2023-01-26 00:57:48 +01:00
|
|
|
<View style={{ marginVertical: StyleConstants.Spacing.S }}>
|
|
|
|
{title ? (
|
|
|
|
<CustomText
|
|
|
|
fontStyle='M'
|
|
|
|
children={title}
|
|
|
|
style={{ color: disabled ? colors.disabled : colors.primaryDefault }}
|
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
<View
|
|
|
|
style={{
|
|
|
|
paddingHorizontal: StyleConstants.Spacing.M,
|
|
|
|
paddingVertical: StyleConstants.Spacing.XS,
|
|
|
|
marginTop: StyleConstants.Spacing.S,
|
|
|
|
borderWidth: 1,
|
|
|
|
borderColor: disabled ? colors.disabled : invalid ? colors.red : colors.border
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{options.map((option, index) => (
|
|
|
|
<Pressable
|
|
|
|
key={index}
|
|
|
|
disabled={disabled}
|
|
|
|
style={{ flex: 1, paddingVertical: StyleConstants.Spacing.S }}
|
|
|
|
onPress={() => {
|
|
|
|
if (multiple) {
|
2022-11-30 22:42:42 +01:00
|
|
|
haptics('Light')
|
2023-01-26 00:57:48 +01:00
|
|
|
|
2022-11-30 22:42:42 +01:00
|
|
|
setOptions(
|
2023-01-26 00:57:48 +01:00
|
|
|
options.map((o, i) => (i === index ? { ...o, selected: !o.selected } : o))
|
2022-11-30 22:42:42 +01:00
|
|
|
)
|
2023-01-26 00:57:48 +01:00
|
|
|
} else {
|
|
|
|
if (!option.selected) {
|
|
|
|
haptics('Light')
|
|
|
|
setOptions(
|
|
|
|
options.map((o, i) => {
|
|
|
|
if (i === index) {
|
|
|
|
return { ...o, selected: true }
|
|
|
|
} else {
|
|
|
|
return { ...o, selected: false }
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
2022-11-30 22:42:42 +01:00
|
|
|
}
|
2023-01-26 00:57:48 +01:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<View style={{ flex: 1, flexDirection: 'row' }}>
|
|
|
|
<Icon
|
|
|
|
style={{
|
|
|
|
marginTop: (StyleConstants.Font.LineHeight.M - StyleConstants.Font.Size.M) / 2,
|
|
|
|
marginRight: StyleConstants.Spacing.S
|
|
|
|
}}
|
|
|
|
name={isSelected(index)}
|
|
|
|
size={StyleConstants.Font.Size.M}
|
|
|
|
color={disabled ? colors.disabled : colors.primaryDefault}
|
2022-12-22 01:21:51 +01:00
|
|
|
/>
|
2023-01-26 00:57:48 +01:00
|
|
|
<CustomText fontStyle='S' style={{ flex: 1 }}>
|
|
|
|
<ParseEmojis
|
|
|
|
content={option.content}
|
|
|
|
style={{ color: disabled ? colors.disabled : colors.primaryDefault }}
|
|
|
|
/>
|
|
|
|
</CustomText>
|
|
|
|
</View>
|
|
|
|
</Pressable>
|
|
|
|
))}
|
|
|
|
</View>
|
2022-12-22 01:21:51 +01:00
|
|
|
</View>
|
2022-11-30 22:42:42 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Selections
|