tooot/src/components/Header/Right.tsx

107 lines
2.3 KiB
TypeScript
Raw Normal View History

import Icon from '@components/Icon'
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
2021-02-08 23:47:20 +01:00
import React, { useMemo } from 'react'
2020-12-26 23:27:53 +01:00
import { Pressable, StyleSheet, Text, View } from 'react-native'
2021-02-08 23:47:20 +01:00
import { Flow } from 'react-native-animated-spinkit'
2020-11-30 00:24:53 +01:00
2020-12-26 23:27:53 +01:00
export interface Props {
type?: 'icon' | 'text'
content: string
2021-01-30 01:29:15 +01:00
native?: boolean
2020-11-30 00:24:53 +01:00
2020-12-26 23:27:53 +01:00
loading?: boolean
disabled?: boolean
2020-11-30 00:24:53 +01:00
2020-12-26 23:27:53 +01:00
onPress: () => void
2020-11-30 00:24:53 +01:00
}
2020-12-26 23:27:53 +01:00
const HeaderRight: React.FC<Props> = ({
type = 'icon',
content,
2021-01-30 01:29:15 +01:00
native = true,
2020-12-26 23:27:53 +01:00
loading,
2020-12-03 01:28:56 +01:00
disabled,
2020-12-26 23:27:53 +01:00
onPress
2020-11-30 00:24:53 +01:00
}) => {
const { theme } = useTheme()
2020-12-26 23:27:53 +01:00
const loadingSpinkit = useMemo(
() => (
<View style={{ position: 'absolute' }}>
2021-02-08 23:47:20 +01:00
<Flow
2020-12-26 23:27:53 +01:00
size={StyleConstants.Font.Size.M * 1.25}
color={theme.secondary}
/>
</View>
),
[theme]
)
const children = useMemo(() => {
switch (type) {
case 'icon':
return (
<>
<Icon
name={content}
2020-12-26 23:27:53 +01:00
style={{ opacity: loading ? 0 : 1 }}
size={StyleConstants.Spacing.M * 1.25}
color={disabled ? theme.secondary : theme.primaryDefault}
2020-12-26 23:27:53 +01:00
/>
{loading && loadingSpinkit}
</>
)
case 'text':
return (
<>
<Text
style={[
styles.text,
{
color: disabled ? theme.secondary : theme.primaryDefault,
2020-12-26 23:27:53 +01:00
opacity: loading ? 0 : 1
}
]}
children={content}
/>
{loading && loadingSpinkit}
</>
)
}
}, [theme, loading, disabled])
2020-11-30 00:24:53 +01:00
return (
2020-12-13 23:38:37 +01:00
<Pressable
2020-12-28 16:54:19 +01:00
onPress={onPress}
2020-12-26 23:27:53 +01:00
children={children}
2020-12-28 16:54:19 +01:00
disabled={disabled || loading}
2020-12-13 23:38:37 +01:00
style={[
styles.base,
{
backgroundColor: theme.backgroundOverlayDefault,
2021-01-30 01:29:15 +01:00
...(type === 'icon' && {
height: 44,
width: 44,
marginRight: native ? -9 : 9
})
2020-12-13 23:38:37 +01:00
}
]}
2020-12-26 23:27:53 +01:00
/>
2020-11-30 00:24:53 +01:00
)
}
const styles = StyleSheet.create({
base: {
2020-12-13 23:38:37 +01:00
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
borderRadius: 100
2020-11-30 00:24:53 +01:00
},
text: {
...StyleConstants.FontStyle.M
2020-11-30 00:24:53 +01:00
}
})
2020-12-12 12:49:29 +01:00
export default HeaderRight