tooot/src/components/Menu/Row.tsx

131 lines
2.9 KiB
TypeScript
Raw Normal View History

2020-11-22 00:46:23 +01:00
import React from 'react'
import { Pressable, StyleSheet, Text, View } from 'react-native'
import { Feather } from '@expo/vector-icons'
2020-11-23 00:07:32 +01:00
import { useTheme } from 'src/utils/styles/ThemeManager'
2020-11-22 00:46:23 +01:00
import { ColorDefinitions } from 'src/utils/styles/themes'
2020-11-30 00:24:53 +01:00
import { StyleConstants } from 'src/utils/styles/constants'
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-11-22 00:46:23 +01:00
title: string
content?: string
iconBack?: 'chevron-right' | 'check'
iconBackColor?: ColorDefinitions
onPress?: () => void
2020-11-22 00:46:23 +01:00
}
const Core: React.FC<Props> = ({
iconFront,
iconFrontColor,
title,
content,
iconBack,
iconBackColor
}) => {
2020-11-23 00:07:32 +01:00
const { theme } = useTheme()
iconFrontColor = iconFrontColor || 'primary'
iconBackColor = iconBackColor || 'secondary'
2020-11-23 00:07:32 +01:00
2020-11-22 00:46:23 +01:00
return (
<View style={styles.core}>
<View style={styles.front}>
{iconFront && (
<Feather
name={iconFront}
2020-11-30 00:24:53 +01:00
size={StyleConstants.Font.Size.M + 2}
color={theme[iconFrontColor]}
style={styles.iconFront}
/>
)}
<Text style={[styles.text, { color: theme.primary }]} numberOfLines={1}>
{title}
</Text>
</View>
2020-12-03 01:28:56 +01:00
{(content || iconBack) && (
<View style={styles.back}>
{content && (
<Text
style={[styles.content, { color: theme.secondary }]}
numberOfLines={1}
>
{content}
</Text>
)}
{iconBack && (
<Feather
name={iconBack}
size={StyleConstants.Font.Size.M + 2}
color={theme[iconBackColor]}
style={styles.iconBack}
/>
)}
</View>
)}
2020-11-22 00:46:23 +01:00
</View>
)
}
2020-12-03 01:28:56 +01:00
const MenuRow: React.FC<Props> = ({ ...props }) => {
2020-11-24 00:18:47 +01:00
const { theme } = useTheme()
2020-11-22 00:46:23 +01:00
return props.onPress ? (
2020-11-22 00:46:23 +01:00
<Pressable
2020-11-24 00:18:47 +01:00
style={[styles.base, { borderBottomColor: theme.separator }]}
onPress={props.onPress}
2020-11-22 00:46:23 +01:00
>
<Core {...props} />
</Pressable>
) : (
<View style={[styles.base, { borderBottomColor: theme.separator }]}>
2020-11-22 00:46:23 +01:00
<Core {...props} />
</View>
)
}
const styles = StyleSheet.create({
base: {
height: 50,
borderBottomWidth: 1
},
core: {
flex: 1,
flexDirection: 'row',
2020-11-30 00:24:53 +01:00
paddingLeft: StyleConstants.Spacing.Global.PagePadding,
paddingRight: StyleConstants.Spacing.Global.PagePadding
2020-11-22 00:46:23 +01:00
},
front: {
flex: 1,
2020-12-03 01:28:56 +01:00
flexBasis: '70%',
flexDirection: 'row',
alignItems: 'center'
},
back: {
flex: 1,
2020-12-03 01:28:56 +01:00
flexBasis: '30%',
flexDirection: 'row',
justifyContent: 'flex-end',
alignItems: 'center'
},
iconFront: {
2020-11-22 00:46:23 +01:00
marginRight: 8
},
text: {
2020-12-03 01:28:56 +01:00
flex: 1,
2020-11-30 00:24:53 +01:00
fontSize: StyleConstants.Font.Size.M
},
content: {
2020-11-30 00:24:53 +01:00
fontSize: StyleConstants.Font.Size.M
},
iconBack: {
marginLeft: 8
2020-11-22 00:46:23 +01:00
}
})
2020-12-10 19:19:56 +01:00
export default React.memo(MenuRow, (prev, next) => {
let skipUpdate = true
skipUpdate = prev.content === next.content
return skipUpdate
})