Skip to content

Commit 72e1bff

Browse files
authored
v4.1.1 #65
2 parents 7684195 + cfde9eb commit 72e1bff

File tree

2 files changed

+61
-48
lines changed

2 files changed

+61
-48
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-material-tabs",
3-
"version": "4.1.0",
3+
"version": "4.1.1",
44
"description": "Material Design implementation of Tabs",
55
"keywords": [
66
"react",

src/components/MaterialTabs.tsx

+60-47
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useEffect } from 'react';
1+
import React, { useState, useEffect, useCallback } from 'react';
22
import {
33
Animated,
44
ScrollView,
@@ -57,63 +57,76 @@ const MaterialTabs: React.FC<Props> = ({
5757
const scrollView = React.createRef<ScrollView>();
5858
const bar = React.createRef<View>();
5959

60-
useEffect(() => {
61-
bar.current &&
62-
bar.current.measure((_, b, width) => {
63-
getTabWidth(width);
64-
});
60+
const getTabWidth = useCallback(
61+
(width: number) => {
62+
if (!scrollable) {
63+
setTabWidth(width / items.length);
64+
}
6565

66-
selectTab();
67-
// eslint-disable-next-line react-hooks/exhaustive-deps
68-
}, [items, barWidth]);
66+
setBarWidth(width);
67+
},
68+
[items.length, scrollable]
69+
);
6970

70-
const getAnimateValues = () => {
71-
const scrollValue = !scrollable ? tabWidth : barWidth * 0.4;
71+
useEffect(() => {
72+
const getAnimateValues = () => {
73+
const scrollValue = !scrollable ? tabWidth : barWidth * 0.4;
7274

73-
const indicator = I18nManager.isRTL
74-
? -selectedIndex * scrollValue
75-
: selectedIndex * scrollValue;
75+
const indicator = I18nManager.isRTL
76+
? -selectedIndex * scrollValue
77+
: selectedIndex * scrollValue;
78+
79+
// All props for fixed tabs are the same
80+
if (!scrollable) {
81+
return {
82+
indicatorPosition: indicator,
83+
scrollPosition: 0,
84+
};
85+
}
7686

77-
// All props for fixed tabs are the same
78-
if (!scrollable) {
7987
return {
8088
indicatorPosition: indicator,
81-
scrollPosition: 0,
89+
scrollPosition: I18nManager.isRTL
90+
? scrollValue * 0.25 +
91+
scrollValue * (items.length - selectedIndex - 2)
92+
: scrollValue * 0.25 + scrollValue * (selectedIndex - 1),
8293
};
83-
}
84-
85-
return {
86-
indicatorPosition: indicator,
87-
scrollPosition: I18nManager.isRTL
88-
? scrollValue * 0.25 + scrollValue * (items.length - selectedIndex - 2)
89-
: scrollValue * 0.25 + scrollValue * (selectedIndex - 1),
9094
};
91-
};
9295

93-
const getTabWidth = (width: number) => {
94-
if (!scrollable) {
95-
setTabWidth(width / items.length);
96-
}
97-
98-
setBarWidth(width);
99-
};
100-
101-
const selectTab = () => {
102-
const values = getAnimateValues();
103-
104-
Animated.spring(indicatorPosition, {
105-
toValue: values.indicatorPosition,
106-
tension: 300,
107-
friction: 20,
108-
useNativeDriver: true,
109-
}).start();
96+
const selectTab = () => {
97+
const values = getAnimateValues();
98+
99+
Animated.spring(indicatorPosition, {
100+
toValue: values.indicatorPosition,
101+
tension: 300,
102+
friction: 20,
103+
useNativeDriver: true,
104+
}).start();
105+
106+
if (scrollView.current) {
107+
scrollView.current.scrollTo({
108+
x: values.scrollPosition,
109+
});
110+
}
111+
};
110112

111-
if (scrollView.current) {
112-
scrollView.current.scrollTo({
113-
x: values.scrollPosition,
113+
bar.current &&
114+
bar.current.measure((_, b, width) => {
115+
getTabWidth(width);
114116
});
115-
}
116-
};
117+
118+
selectTab();
119+
}, [
120+
bar,
121+
barWidth,
122+
getTabWidth,
123+
indicatorPosition,
124+
items.length,
125+
scrollView,
126+
scrollable,
127+
selectedIndex,
128+
tabWidth,
129+
]);
117130

118131
return (
119132
items && (

0 commit comments

Comments
 (0)