-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathTabSelectorBase.tsx
More file actions
140 lines (128 loc) · 5.6 KB
/
Copy pathTabSelectorBase.tsx
File metadata and controls
140 lines (128 loc) · 5.6 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import React, {useEffect, useMemo, useState} from 'react';
import ScrollView from '@components/ScrollView';
import useScrollEventEmitter from '@hooks/useScrollEventEmitter';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import CONST from '@src/CONST';
import getBackgroundColor from './getBackground';
import getOpacity from './getOpacity';
import {useTabSelectorActions, useTabSelectorState} from './TabSelectorContext';
import TabSelectorItem from './TabSelectorItem';
import type {TabSelectorBaseProps} from './types';
/**
* Navigation-agnostic tab selector UI that renders a row of TabSelectorItem components.
*
* This component owns the shared layout, width/position measurements, and animation helpers
* (getOpacity / getBackgroundColor). It is reused by both navigation-based TabSelector and
* inline tab selectors like SplitExpensePage.
*/
function TabSelectorBase({
tabs,
activeTabKey,
onTabPress = () => {},
onLongTabPress,
onActiveTabPress = () => {},
position,
shouldShowLabelWhenInactive = true,
equalWidth = false,
contentContainerStyles,
}: TabSelectorBaseProps) {
const theme = useTheme();
const styles = useThemeStyles();
const routesLength = tabs.length;
const defaultAffectedAnimatedTabs = useMemo(() => Array.from({length: routesLength}, (_v, i) => i), [routesLength]);
const [affectedAnimatedTabs, setAffectedAnimatedTabs] = useState(defaultAffectedAnimatedTabs);
const {containerRef} = useTabSelectorState();
const {onContainerLayout, onContainerScroll} = useTabSelectorActions();
const triggerScrollEvent = useScrollEventEmitter();
const activeIndex = tabs.findIndex((tab) => tab.key === activeTabKey);
// After a tab change, reset affectedAnimatedTabs once the transition is done so
// tabs settle back into the default animated state.
useEffect(() => {
const timerID = setTimeout(() => {
setAffectedAnimatedTabs(defaultAffectedAnimatedTabs);
}, CONST.ANIMATED_TRANSITION);
return () => clearTimeout(timerID);
}, [defaultAffectedAnimatedTabs, activeIndex]);
return (
<ScrollView
scrollEventThrottle={CONST.TIMING.MIN_SMOOTH_SCROLL_EVENT_THROTTLE}
onLayout={onContainerLayout}
onScroll={(e) => {
onContainerScroll(e);
triggerScrollEvent();
}}
ref={containerRef}
style={styles.scrollableTabSelector}
// On iOS a horizontal ScrollView lays out its content along an unbounded main axis, so flex-1 tabs
// (equalWidth) divide their intrinsic content width instead of the viewport. Giving the content
// container a definite width lets the flex children split it evenly. Scoped to equalWidth so normal
// overflowing/scrollable tab rows are not constrained.
contentContainerStyle={[styles.tabSelectorContentContainer, equalWidth && styles.w100, contentContainerStyles]}
horizontal
showsHorizontalScrollIndicator={false}
keyboardShouldPersistTaps="handled"
>
{tabs.map((tab, index) => {
const isActive = index === activeIndex;
const activeOpacity = getOpacity({
routesLength,
tabIndex: index,
active: true,
affectedTabs: affectedAnimatedTabs,
position,
isActive,
});
const inactiveOpacity = getOpacity({
routesLength,
tabIndex: index,
active: false,
affectedTabs: affectedAnimatedTabs,
position,
isActive,
});
const backgroundColor = getBackgroundColor({
routesLength,
tabIndex: index,
affectedTabs: affectedAnimatedTabs,
theme,
position,
isActive,
});
const handlePress = () => {
if (isActive) {
onActiveTabPress(tab.key);
return;
}
setAffectedAnimatedTabs([activeIndex, index]);
onTabPress(tab.key);
};
return (
<TabSelectorItem
tabKey={tab.key}
key={tab.key}
icon={tab.icon}
title={tab.title}
onPress={handlePress}
onLongPress={onLongTabPress ? () => onLongTabPress(tab.key) : undefined}
activeOpacity={activeOpacity}
inactiveOpacity={inactiveOpacity}
backgroundColor={backgroundColor}
isActive={isActive}
testID={tab.testID}
sentryLabel={tab.sentryLabel}
shouldShowLabelWhenInactive={shouldShowLabelWhenInactive}
equalWidth={equalWidth}
badgeText={tab.badgeText}
isBadgeCondensed={tab.isBadgeCondensed}
badgeStyles={tab.badgeStyles}
pendingAction={tab.pendingAction}
isDisabled={tab.isDisabled}
/>
);
})}
</ScrollView>
);
}
TabSelectorBase.displayName = 'TabSelectorBase';
export default TabSelectorBase;