forked from ptomasroos/react-native-scrollable-tab-view
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScrollableTabBar.js
202 lines (173 loc) · 5.72 KB
/
ScrollableTabBar.js
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const React = require('react-native');
const {
View,
Animated,
StyleSheet,
ScrollView,
TouchableOpacity,
Text,
Platform,
} = React;
const TAB_HEIGHT = 50;
const ScrollableTabBar = React.createClass({
getDefaultProps() {
return {
scrollOffset: 52,
};
},
propTypes: {
goToPage: React.PropTypes.func,
activeTab: React.PropTypes.number,
tabs: React.PropTypes.array,
underlineColor: React.PropTypes.string,
backgroundColor: React.PropTypes.string,
activeTextColor: React.PropTypes.string,
inactiveTextColor: React.PropTypes.string,
scrollOffset: React.PropTypes.number,
},
getInitialState() {
this._tabsMeasurements = [];
return {
_leftTabUnderline: new Animated.Value(0),
_widthTabUnderline: new Animated.Value(0),
};
},
updateView(offset) {
const position = Math.floor(offset.value);
const pageOffset = offset.value % 1;
const tabCount = this.props.tabs.length;
if (tabCount === 0 || offset.value < 0 || offset.value > tabCount - 1) {
return;
}
if (this.necessarilyMeasurementsCompleted(position)) {
this.updateTabPanel(position, pageOffset);
this.updateTabUnderline(position, pageOffset, tabCount);
}
},
necessarilyMeasurementsCompleted(position) {
return this._tabsMeasurements[position] && this._tabsMeasurements[position + 1];
},
updateTabPanel(position, pageOffset) {
const absolutePageOffset = pageOffset * this._tabsMeasurements[position].width;
let newScrollX = this._tabsMeasurements[position].left + absolutePageOffset;
newScrollX -= this.props.scrollOffset;
newScrollX = newScrollX >= 0 ? newScrollX : 0;
if (Platform === 'android') {
this._scrollView.scrollTo({x: newScrollX, y: 0, });
} else {
const rightBoundScroll = this._tabContainerMeasurements.width - (this._containerMeasurements.width);
newScrollX = newScrollX > rightBoundScroll ? rightBoundScroll : newScrollX;
this._scrollView.scrollTo({x: newScrollX, y: 0, });
}
},
updateTabUnderline(position, pageOffset, tabCount) {
const lineLeft = this._tabsMeasurements[position].left;
const lineRight = this._tabsMeasurements[position].right;
if (position < tabCount - 1) {
const nextTabLeft = this._tabsMeasurements[position + 1].left;
const nextTabRight = this._tabsMeasurements[position + 1].right;
const newLineLeft = (pageOffset * nextTabLeft + (1 - pageOffset) * lineLeft);
const newLineRight = (pageOffset * nextTabRight + (1 - pageOffset) * lineRight);
this.state._leftTabUnderline.setValue(newLineLeft);
this.state._widthTabUnderline.setValue(newLineRight - newLineLeft);
} else {
this.state._leftTabUnderline.setValue(lineLeft);
this.state._widthTabUnderline.setValue(lineRight - lineLeft);
}
},
renderTabOption(name, page) {
const isTabActive = this.props.activeTab === page;
const activeTextColor = this.props.activeTextColor || 'navy';
const inactiveTextColor = this.props.inactiveTextColor || 'black';
return <TouchableOpacity
key={name}
ref={'tab_' + page}
accessible={true}
accessibilityLabel={name}
accessibilityTraits='button'
style={styles.tab}
onPress={() => this.props.goToPage(page)}
onLayout={this.measureTab.bind(this, page)}
>
<View>
<Text style={{color: isTabActive ? activeTextColor : inactiveTextColor, }}>{name}</Text>
</View>
</TouchableOpacity>;
},
measureTab(page) {
const tabContainerhandle = React.findNodeHandle(this.refs.tabContainer);
this.refs['tab_' + page].measureLayout(tabContainerhandle, (ox, oy, width, height, pageX, pageY) => {
this._tabsMeasurements[page] = {left: ox, right: ox + width, width: width, height: height, };
this.updateView({value: this.props.scrollValue._value, });
});
},
render() {
const tabUnderlineStyle = {
position: 'absolute',
height: 4,
backgroundColor: this.props.underlineColor || 'navy',
bottom: 0,
};
this.props.scrollValue.addListener(this.updateView);
const dynamicTabUnderline = {
left: this.state._leftTabUnderline,
width: this.state._widthTabUnderline,
};
return <View
style={[styles.container, {backgroundColor: this.props.backgroundColor || null, }, ]}
onLayout={this.onContainerLayout}
>
<ScrollView
ref={(scrollView) => { this._scrollView = scrollView; }}
horizontal={true}
showsHorizontalScrollIndicator={false}
showsVerticalScrollIndicator={false}
styles={styles.scrollableContainer}
directionalLockEnabled={true}
scrollEventThrottle={16}
>
<View
style={styles.tabs}
ref={'tabContainer'}
onLayout={this.onTabContainerLayout}
>
{this.props.tabs.map((tab, i) => this.renderTabOption(tab, i))}
<Animated.View style={[tabUnderlineStyle, dynamicTabUnderline, ]} />
</View>
</ScrollView>
</View>;
},
onTabContainerLayout(e) {
this._tabContainerMeasurements = e.nativeEvent.layout;
},
onContainerLayout(e) {
this._containerMeasurements = e.nativeEvent.layout;
},
});
module.exports = ScrollableTabBar;
const styles = StyleSheet.create({
tab: {
height: TAB_HEIGHT - 1,
alignItems: 'center',
justifyContent: 'center',
paddingBottom: 30,
paddingLeft: 20,
paddingRight: 20,
},
container: {
height: TAB_HEIGHT,
borderWidth: 1,
borderTopWidth: 0,
borderLeftWidth: 0,
borderRightWidth: 0,
borderBottomColor: '#ccc',
},
tabs: {
height: TAB_HEIGHT - 1,
flexDirection: 'row',
justifyContent: 'space-around',
},
scrollableContainer: {
height: TAB_HEIGHT,
},
});