@momo-kits/tab-view 0.169.8 → 0.169.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.tsx +15 -12
- package/package.json +1 -1
- package/tabBar/SrollableTabBar.tsx +8 -6
package/index.tsx
CHANGED
|
@@ -25,6 +25,9 @@ import {
|
|
|
25
25
|
ScreenContext,
|
|
26
26
|
} from '@momo-kits/foundation';
|
|
27
27
|
|
|
28
|
+
const WEB_INDICATOR_DURATION = 200;
|
|
29
|
+
|
|
30
|
+
/** Render function of `TabView`; pages mount on first visit and stay mounted. */
|
|
28
31
|
const TabComponent: ForwardRefRenderFunction<TabViewRef, TabViewProps> = (
|
|
29
32
|
{
|
|
30
33
|
scrollable = false,
|
|
@@ -53,7 +56,7 @@ const TabComponent: ForwardRefRenderFunction<TabViewRef, TabViewProps> = (
|
|
|
53
56
|
const { theme } = useContext(ApplicationContext);
|
|
54
57
|
const app = useContext<any>(MiniAppContext);
|
|
55
58
|
const screen = useContext<any>(ScreenContext);
|
|
56
|
-
|
|
59
|
+
const isScrollEnabled = useRef(false);
|
|
57
60
|
|
|
58
61
|
const componentName = 'TabView';
|
|
59
62
|
|
|
@@ -65,7 +68,7 @@ const TabComponent: ForwardRefRenderFunction<TabViewRef, TabViewProps> = (
|
|
|
65
68
|
return id;
|
|
66
69
|
}, [componentName, accessibilityLabel, app, screen]);
|
|
67
70
|
|
|
68
|
-
const
|
|
71
|
+
const handlePressTabItem = (index: number) => {
|
|
69
72
|
onPressTabItem?.(index);
|
|
70
73
|
|
|
71
74
|
if (!fitContent) {
|
|
@@ -78,11 +81,11 @@ const TabComponent: ForwardRefRenderFunction<TabViewRef, TabViewProps> = (
|
|
|
78
81
|
setSelectedIndex(index);
|
|
79
82
|
};
|
|
80
83
|
|
|
81
|
-
const
|
|
84
|
+
const handleLayout = (e: LayoutChangeEvent) => {
|
|
82
85
|
setTabBarWidth(e.nativeEvent.layout.width);
|
|
83
86
|
};
|
|
84
87
|
|
|
85
|
-
const
|
|
88
|
+
const handlePageSelected = (e: PagerViewOnPageSelectedEvent) => {
|
|
86
89
|
const { position } = e.nativeEvent;
|
|
87
90
|
if (!lazy.includes(position)) {
|
|
88
91
|
lazy.push(position);
|
|
@@ -97,7 +100,7 @@ const TabComponent: ForwardRefRenderFunction<TabViewRef, TabViewProps> = (
|
|
|
97
100
|
TabBarComponent = CardTabBar;
|
|
98
101
|
}
|
|
99
102
|
|
|
100
|
-
const
|
|
103
|
+
const handlePageScroll = (e: PagerViewOnPageScrollEvent) => {
|
|
101
104
|
const { position, offset } = e.nativeEvent;
|
|
102
105
|
scrollX.current.setValue(position + offset);
|
|
103
106
|
pagerProps?.onPageScroll?.(e);
|
|
@@ -109,7 +112,7 @@ const TabComponent: ForwardRefRenderFunction<TabViewRef, TabViewProps> = (
|
|
|
109
112
|
} else if (Platform.OS === 'web') {
|
|
110
113
|
Animated.timing(scrollX.current, {
|
|
111
114
|
toValue: selectedIndex,
|
|
112
|
-
duration:
|
|
115
|
+
duration: WEB_INDICATOR_DURATION,
|
|
113
116
|
useNativeDriver: false,
|
|
114
117
|
}).start();
|
|
115
118
|
}
|
|
@@ -140,13 +143,13 @@ const TabComponent: ForwardRefRenderFunction<TabViewRef, TabViewProps> = (
|
|
|
140
143
|
|
|
141
144
|
return (
|
|
142
145
|
<View
|
|
143
|
-
onLayout={
|
|
146
|
+
onLayout={handleLayout}
|
|
144
147
|
style={[styles.tabView, !fitContent && styles.flex1]}
|
|
145
148
|
accessibilityLabel={componentId}
|
|
146
149
|
>
|
|
147
150
|
<Animated.View style={style}>
|
|
148
151
|
<TabBarComponent
|
|
149
|
-
onPressTabItem={
|
|
152
|
+
onPressTabItem={handlePressTabItem}
|
|
150
153
|
selectedIndex={selectedIndex}
|
|
151
154
|
tabs={tabs}
|
|
152
155
|
containerWidth={tabBarWidth}
|
|
@@ -163,14 +166,14 @@ const TabComponent: ForwardRefRenderFunction<TabViewRef, TabViewProps> = (
|
|
|
163
166
|
) : (
|
|
164
167
|
<PagerView
|
|
165
168
|
{...pagerProps}
|
|
166
|
-
onPageScroll={
|
|
169
|
+
onPageScroll={handlePageScroll}
|
|
167
170
|
ref={pagerRef}
|
|
168
|
-
scrollEnabled={
|
|
171
|
+
scrollEnabled={isScrollEnabled.current}
|
|
169
172
|
onLayout={e => {
|
|
170
173
|
pagerProps?.onLayout?.(e);
|
|
171
|
-
|
|
174
|
+
isScrollEnabled.current = pagerProps?.scrollEnabled ?? false;
|
|
172
175
|
}}
|
|
173
|
-
onPageSelected={
|
|
176
|
+
onPageSelected={handlePageSelected}
|
|
174
177
|
style={styles.pagerView}
|
|
175
178
|
initialPage={startPage}
|
|
176
179
|
>
|
package/package.json
CHANGED
|
@@ -13,6 +13,8 @@ import styles from '../styles';
|
|
|
13
13
|
import { ApplicationContext } from '@momo-kits/foundation';
|
|
14
14
|
import TabItem from '../tabItem/TabItem';
|
|
15
15
|
|
|
16
|
+
const INDICATOR_DURATION = 250;
|
|
17
|
+
|
|
16
18
|
/**
|
|
17
19
|
* Scrollable tabs sized to their titles. Measures each item so the indicator
|
|
18
20
|
* animates to the selected tab and the bar scrolls it into view.
|
|
@@ -104,18 +106,18 @@ const ScrollableTabBar: FC<TabBarProps> = ({
|
|
|
104
106
|
Animated.parallel([
|
|
105
107
|
Animated.timing(animatedWidth, {
|
|
106
108
|
toValue: w,
|
|
107
|
-
duration:
|
|
109
|
+
duration: INDICATOR_DURATION,
|
|
108
110
|
useNativeDriver: false,
|
|
109
111
|
}),
|
|
110
112
|
Animated.timing(animX, {
|
|
111
113
|
toValue: x,
|
|
112
|
-
duration:
|
|
114
|
+
duration: INDICATOR_DURATION,
|
|
113
115
|
useNativeDriver: false,
|
|
114
116
|
}),
|
|
115
117
|
]).start();
|
|
116
118
|
}, [animatedWidth, animX, itemWidthMap, selectedIndex]);
|
|
117
119
|
|
|
118
|
-
const
|
|
120
|
+
const handleLayout = useCallback(
|
|
119
121
|
(e: LayoutChangeEvent, index: number) => {
|
|
120
122
|
const { width, x } = e.nativeEvent.layout;
|
|
121
123
|
if (width != null && x != null) setMeasured(index, width, x);
|
|
@@ -133,7 +135,7 @@ const ScrollableTabBar: FC<TabBarProps> = ({
|
|
|
133
135
|
[itemWidthMap],
|
|
134
136
|
);
|
|
135
137
|
|
|
136
|
-
const
|
|
138
|
+
const handlePressTabItem = useCallback(
|
|
137
139
|
(index: number) => {
|
|
138
140
|
ensureMeasured(index, () => {
|
|
139
141
|
scrollToIndex(index);
|
|
@@ -166,13 +168,13 @@ const ScrollableTabBar: FC<TabBarProps> = ({
|
|
|
166
168
|
key={`tab-view-item-wrapper-${index}`}
|
|
167
169
|
ref={itemRefs.current[index]}
|
|
168
170
|
collapsable={false}
|
|
169
|
-
onLayout={e =>
|
|
171
|
+
onLayout={e => handleLayout(e, index)}
|
|
170
172
|
style={styles.tabItemWrapper}
|
|
171
173
|
>
|
|
172
174
|
<TabItem
|
|
173
175
|
key={`Tab View Item ${item.title}-${index}`}
|
|
174
176
|
onPressTabItem={() => {
|
|
175
|
-
|
|
177
|
+
handlePressTabItem(index);
|
|
176
178
|
}}
|
|
177
179
|
active={isActive}
|
|
178
180
|
tab={item}
|