-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathTabSelectorItem.tsx
114 lines (99 loc) · 4.27 KB
/
TabSelectorItem.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import React, {useState} from 'react';
// eslint-disable-next-line no-restricted-imports
import {Animated} from 'react-native';
import PressableWithFeedback from '@components/Pressable/PressableWithFeedback';
import {useProductTrainingContext} from '@components/ProductTrainingContext';
import Tooltip from '@components/Tooltip';
import EducationalTooltip from '@components/Tooltip/EducationalTooltip';
import useThemeStyles from '@hooks/useThemeStyles';
import CONST from '@src/CONST';
import type IconAsset from '@src/types/utils/IconAsset';
import TabIcon from './TabIcon';
import TabLabel from './TabLabel';
const AnimatedPressableWithFeedback = Animated.createAnimatedComponent(PressableWithFeedback);
type TabSelectorItemProps = {
/** Function to call when onPress */
onPress?: () => void;
/** Icon to display on tab */
icon?: IconAsset;
/** Title of the tab */
title?: string;
/** Animated background color value for the tab button */
backgroundColor?: string | Animated.AnimatedInterpolation<string>;
/** Animated opacity value while the tab is in inactive state */
inactiveOpacity?: number | Animated.AnimatedInterpolation<number>;
/** Animated opacity value while the tab is in active state */
activeOpacity?: number | Animated.AnimatedInterpolation<number>;
/** Whether this tab is active */
isActive?: boolean;
/** Whether to show the label when the tab is inactive */
shouldShowLabelWhenInactive?: boolean;
/** Whether to show the test receipt tooltip */
shouldShowTestReceiptTooltip?: boolean;
};
function TabSelectorItem({
icon,
title = '',
onPress = () => {},
backgroundColor = '',
activeOpacity = 0,
inactiveOpacity = 1,
isActive = false,
shouldShowLabelWhenInactive = true,
shouldShowTestReceiptTooltip = false,
}: TabSelectorItemProps) {
const styles = useThemeStyles();
const [isHovered, setIsHovered] = useState(false);
const {shouldShowProductTrainingTooltip, renderProductTrainingTooltip} = useProductTrainingContext(CONST.PRODUCT_TRAINING_TOOLTIP_NAMES.SCAN_TEST_TOOLTIP, shouldShowTestReceiptTooltip);
const content = () => {
return (
<AnimatedPressableWithFeedback
accessibilityLabel={title}
style={[styles.tabSelectorButton, styles.tabBackground(isHovered, isActive, backgroundColor), styles.userSelectNone]}
wrapperStyle={[styles.flexGrow1]}
onPress={onPress}
onHoverIn={() => setIsHovered(true)}
onHoverOut={() => setIsHovered(false)}
role={CONST.ROLE.BUTTON}
dataSet={{[CONST.SELECTION_SCRAPER_HIDDEN_ELEMENT]: true}}
>
<TabIcon
icon={icon}
activeOpacity={styles.tabOpacity(isHovered, isActive, activeOpacity, inactiveOpacity).opacity}
inactiveOpacity={styles.tabOpacity(isHovered, isActive, inactiveOpacity, activeOpacity).opacity}
/>
{(shouldShowLabelWhenInactive || isActive) && (
<TabLabel
title={title}
activeOpacity={styles.tabOpacity(isHovered, isActive, activeOpacity, inactiveOpacity).opacity}
inactiveOpacity={styles.tabOpacity(isHovered, isActive, inactiveOpacity, activeOpacity).opacity}
/>
)}
</AnimatedPressableWithFeedback>
);
};
return shouldShowTestReceiptTooltip ? (
<EducationalTooltip
shouldRender={shouldShowProductTrainingTooltip}
renderTooltipContent={renderProductTrainingTooltip}
shouldHideOnNavigate
anchorAlignment={{
horizontal: CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL.CENTER,
vertical: CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.TOP,
}}
wrapperStyle={styles.productTrainingTooltipWrapper}
shiftVertical={25}
>
{content()}
</EducationalTooltip>
) : (
<Tooltip
shouldRender={!shouldShowLabelWhenInactive && !isActive}
text={title}
>
{content()}
</Tooltip>
);
}
TabSelectorItem.displayName = 'TabSelectorItem';
export default TabSelectorItem;