tooot/src/components/Menu/Row.tsx

177 lines
5.3 KiB
TypeScript
Raw Normal View History

import Icon from '@components/Icon'
import CustomText from '@components/Text'
2021-04-09 21:43:12 +02:00
import { useAccessibility } from '@utils/accessibility/AccessibilityManager'
2020-12-27 00:55:22 +01:00
import { StyleConstants } from '@utils/styles/constants'
2020-12-13 14:04:25 +01:00
import { useTheme } from '@utils/styles/ThemeManager'
import { ColorDefinitions } from '@utils/styles/themes'
2020-12-27 00:55:22 +01:00
import React, { useMemo } from 'react'
import { View } from 'react-native'
2021-02-08 23:47:20 +01:00
import { Flow } from 'react-native-animated-spinkit'
2021-03-02 01:17:06 +01:00
import { State, Switch, TapGestureHandler } from 'react-native-gesture-handler'
2020-11-24 00:18:47 +01:00
2020-11-22 00:46:23 +01:00
export interface Props {
2020-12-12 12:49:29 +01:00
iconFront?: any
iconFrontColor?: ColorDefinitions
2020-12-27 00:55:22 +01:00
2020-11-22 00:46:23 +01:00
title: string
2020-12-29 16:19:04 +01:00
description?: string
2021-01-18 00:23:40 +01:00
content?: string | React.ReactNode
2021-05-09 21:59:03 +02:00
badge?: boolean
2020-12-27 00:55:22 +01:00
2020-12-29 16:19:04 +01:00
switchValue?: boolean
switchDisabled?: boolean
switchOnValueChange?: () => void
iconBack?: 'ChevronRight' | 'ExternalLink' | 'Check'
iconBackColor?: ColorDefinitions
2020-12-27 00:55:22 +01:00
loading?: boolean
onPress?: () => void
2020-11-22 00:46:23 +01:00
}
2020-12-28 00:59:57 +01:00
const MenuRow: React.FC<Props> = ({
iconFront,
iconFrontColor = 'primaryDefault',
title,
2020-12-29 16:19:04 +01:00
description,
content,
2021-05-09 21:59:03 +02:00
badge = false,
2020-12-29 16:19:04 +01:00
switchValue,
switchDisabled,
switchOnValueChange,
iconBack,
2020-12-28 00:59:57 +01:00
iconBackColor = 'secondary',
loading = false,
onPress
}) => {
2022-02-12 14:51:01 +01:00
const { colors, theme } = useTheme()
2021-04-09 21:43:12 +02:00
const { screenReaderEnabled } = useAccessibility()
2020-11-23 00:07:32 +01:00
2020-12-27 00:55:22 +01:00
const loadingSpinkit = useMemo(
() => (
<View style={{ position: 'absolute' }}>
<Flow size={StyleConstants.Font.Size.M * 1.25} color={colors.secondary} />
2020-12-27 00:55:22 +01:00
</View>
),
[theme]
)
return (
2021-04-09 21:43:12 +02:00
<View
style={{ minHeight: 50 }}
2021-04-09 21:43:12 +02:00
accessible
accessibilityRole={switchValue ? 'switch' : 'button'}
accessibilityState={switchValue ? { checked: switchValue } : undefined}
>
2021-01-14 22:53:01 +01:00
<TapGestureHandler
2021-04-09 21:43:12 +02:00
onHandlerStateChange={async ({ nativeEvent }) => {
2022-12-03 15:50:15 +01:00
if (typeof iconBack !== 'string') return // Let icon back handles the gesture
2021-04-09 21:43:12 +02:00
if (nativeEvent.state === State.ACTIVE && !loading) {
if (screenReaderEnabled && switchOnValueChange) {
switchOnValueChange()
} else {
if (onPress) onPress()
}
}
}}
2021-01-14 22:53:01 +01:00
>
2021-05-23 22:40:42 +02:00
<View style={{ flex: 1 }}>
<View
style={{
flex: 1,
flexDirection: 'row',
2022-12-03 15:50:15 +01:00
justifyContent: 'space-between',
marginTop: StyleConstants.Spacing.S
}}
>
<View
style={{
2022-12-12 20:43:45 +01:00
flexGrow: 3,
flexDirection: 'row',
2022-12-12 20:43:45 +01:00
alignItems: 'center',
marginRight: StyleConstants.Spacing.M
}}
>
2021-05-17 23:09:50 +02:00
{iconFront && (
2021-02-27 16:33:54 +01:00
<Icon
2021-05-17 23:09:50 +02:00
name={iconFront}
2021-02-27 16:33:54 +01:00
size={StyleConstants.Font.Size.L}
2022-02-12 14:51:01 +01:00
color={colors[iconFrontColor]}
style={{ marginRight: StyleConstants.Spacing.S }}
2021-05-17 23:09:50 +02:00
/>
)}
{badge ? (
<View
style={{
width: 8,
height: 8,
2022-02-12 14:51:01 +01:00
backgroundColor: colors.red,
2021-05-17 23:09:50 +02:00
borderRadius: 8,
marginRight: StyleConstants.Spacing.S
}}
2021-02-27 16:33:54 +01:00
/>
2021-01-14 22:53:01 +01:00
) : null}
<CustomText fontStyle='M' style={{ color: colors.primaryDefault }} numberOfLines={1}>
2022-06-21 23:31:31 +02:00
{title}
</CustomText>
2021-01-14 22:53:01 +01:00
</View>
2021-05-17 23:09:50 +02:00
{content || switchValue !== undefined || iconBack ? (
<View
style={{
flexShrink: 1,
flexDirection: 'row',
justifyContent: 'flex-end',
alignItems: 'center',
paddingLeft: StyleConstants.Spacing.L
}}
>
2021-05-17 23:09:50 +02:00
{content ? (
typeof content === 'string' ? (
<CustomText
style={{
color: colors.secondary,
opacity: !iconBack && loading ? 0 : 1
}}
2021-05-17 23:09:50 +02:00
numberOfLines={1}
>
{content}
</CustomText>
2021-05-17 23:09:50 +02:00
) : (
content
)
) : null}
{switchValue !== undefined ? (
<Switch
value={switchValue}
onValueChange={switchOnValueChange}
disabled={switchDisabled}
2022-02-12 14:51:01 +01:00
trackColor={{ true: colors.blue, false: colors.disabled }}
2021-05-17 23:09:50 +02:00
style={{ opacity: loading ? 0 : 1 }}
/>
) : null}
{iconBack ? (
<Icon
name={iconBack}
size={StyleConstants.Font.Size.L}
2022-02-12 14:51:01 +01:00
color={colors[iconBackColor]}
style={{ marginLeft: 8, opacity: loading ? 0 : 1 }}
2021-05-17 23:09:50 +02:00
/>
) : null}
{loading && loadingSpinkit}
</View>
) : null}
</View>
{description ? (
<CustomText fontStyle='S' style={{ color: colors.secondary }}>
2021-05-17 23:09:50 +02:00
{description}
</CustomText>
2021-01-14 22:53:01 +01:00
) : null}
</View>
</TapGestureHandler>
</View>
2020-11-22 00:46:23 +01:00
)
}
2020-12-27 00:55:22 +01:00
export default MenuRow