tooot/src/components/Header/Right.tsx

113 lines
2.9 KiB
TypeScript
Raw Normal View History

import Icon, { IconName } from '@components/Icon'
import { Loading } from '@components/Loading'
import CustomText from '@components/Text'
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
import React from 'react'
import { AccessibilityProps, Pressable, View } from 'react-native'
2020-11-30 00:24:53 +01:00
export type Props = {
2021-04-09 21:43:12 +02:00
accessibilityLabel?: string
accessibilityHint?: string
accessibilityState?: AccessibilityProps['accessibilityState']
2021-01-30 01:29:15 +01:00
native?: boolean
2021-03-22 00:25:53 +01:00
background?: boolean
2020-11-30 00:24:53 +01:00
2020-12-26 23:27:53 +01:00
loading?: boolean
disabled?: boolean
2022-12-22 01:21:51 +01:00
destructive?: boolean
2023-07-13 21:55:26 +02:00
destructiveColor?: string
2020-11-30 00:24:53 +01:00
2020-12-26 23:27:53 +01:00
onPress: () => void
} & ({ type?: undefined; content: IconName } | { type: 'text'; content: string })
2020-11-30 00:24:53 +01:00
2020-12-26 23:27:53 +01:00
const HeaderRight: React.FC<Props> = ({
2021-04-09 21:43:12 +02:00
// Accessibility - Start
accessibilityLabel,
accessibilityHint,
accessibilityState,
// Accessibility - End
type,
2020-12-26 23:27:53 +01:00
content,
2021-01-30 01:29:15 +01:00
native = true,
2021-03-22 00:25:53 +01:00
background = false,
2020-12-26 23:27:53 +01:00
loading,
2020-12-03 01:28:56 +01:00
disabled,
2022-12-22 01:21:51 +01:00
destructive = false,
2023-07-13 21:55:26 +02:00
destructiveColor,
2020-12-26 23:27:53 +01:00
onPress
2020-11-30 00:24:53 +01:00
}) => {
const { colors } = useTheme()
2020-11-30 00:24:53 +01:00
const loadingSpinkit = () =>
loading ? (
2020-12-26 23:27:53 +01:00
<View style={{ position: 'absolute' }}>
<Loading />
2020-12-26 23:27:53 +01:00
</View>
) : null
2020-12-26 23:27:53 +01:00
const children = () => {
2020-12-26 23:27:53 +01:00
switch (type) {
case 'text':
return (
<>
<CustomText
fontStyle='M'
2022-12-22 01:21:51 +01:00
fontWeight={destructive ? 'Bold' : 'Normal'}
style={{
2022-12-22 01:21:51 +01:00
color: disabled
? colors.secondary
: destructive
2023-07-13 21:55:26 +02:00
? destructiveColor || colors.red
2022-12-22 01:21:51 +01:00
: colors.primaryDefault,
opacity: loading ? 0 : 1
}}
2020-12-26 23:27:53 +01:00
children={content}
/>
{loadingSpinkit()}
2020-12-26 23:27:53 +01:00
</>
)
default:
return (
<>
<Icon
name={content}
style={{ opacity: loading ? 0 : 1 }}
size={StyleConstants.Spacing.M * 1.25}
color={disabled ? colors.secondary : destructive ? colors.red : colors.primaryDefault}
/>
{loadingSpinkit()}
</>
)
2020-12-26 23:27:53 +01:00
}
}
2020-12-26 23:27:53 +01:00
2020-11-30 00:24:53 +01:00
return (
2020-12-13 23:38:37 +01:00
<Pressable
2021-04-09 21:43:12 +02:00
accessibilityLabel={accessibilityLabel}
accessibilityHint={accessibilityHint}
accessibilityRole='button'
accessibilityState={accessibilityState}
2020-12-28 16:54:19 +01:00
onPress={onPress}
children={children()}
2020-12-28 16:54:19 +01:00
disabled={disabled || loading}
style={{
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
2022-12-22 01:21:51 +01:00
backgroundColor: background ? colors.backgroundOverlayDefault : undefined,
minHeight: 44,
minWidth: 44,
2022-12-22 01:21:51 +01:00
marginRight: native ? -StyleConstants.Spacing.S : StyleConstants.Spacing.S,
2023-02-12 18:38:06 +01:00
...(type === undefined && { borderRadius: 99 }),
...(type === 'text' && {
paddingHorizontal: StyleConstants.Spacing.S
})
}}
2020-12-26 23:27:53 +01:00
/>
2020-11-30 00:24:53 +01:00
)
}
2020-12-12 12:49:29 +01:00
export default HeaderRight