forked from ptomasroos/react-native-scrollable-tab-view
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
210 lines (189 loc) · 5.8 KB
/
index.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
203
204
205
206
207
208
209
210
'use strict';
var React = require('react-native');
var {
Dimensions,
View,
Animated,
ScrollView,
Platform,
StyleSheet,
ViewPagerAndroid,
PropTypes,
InteractionManager,
} = React;
var DefaultTabBar = require('./DefaultTabBar');
var deviceWidth = Dimensions.get('window').width;
var deviceHeight = Dimensions.get('window').height;
var ScrollableTabView = React.createClass({
statics: {
DefaultTabBar,
},
propTypes: {
tabBarPosition: PropTypes.oneOf(['top', 'bottom']),
initialPage: PropTypes.number,
onChangeTab: PropTypes.func,
onScroll: PropTypes.func,
renderTabBar: PropTypes.any,
style: View.propTypes.style,
},
getDefaultProps() {
return {
tabBarPosition: 'top',
initialPage: 0,
onChangeTab: () => {},
onScroll: () => {}
}
},
getInitialState() {
return {
currentPage: this.props.initialPage,
scrollValue: new Animated.Value(this.props.initialPage),
container: {
width: deviceWidth,
height: deviceHeight,
}
};
},
componentWillReceiveProps(props) {
if (props.initialPage && props.initialPage !== this.state.currentPage) {
this.goToPage(props.initialPage);
}
},
goToPage(pageNumber) {
this.props.onChangeTab({ i: pageNumber, ref: this._children()[pageNumber] });
if(Platform.OS === 'ios') {
var offset = pageNumber * this.state.container.width;
this.scrollView.scrollTo(0, offset);
} else {
this.scrollView.setPage(pageNumber);
}
this.setState({currentPage: pageNumber});
},
renderTabBar(props) {
if (this.props.renderTabBar === false) {
return null;
} else if (this.props.renderTabBar) {
return React.cloneElement(this.props.renderTabBar(), props);
} else {
return <DefaultTabBar {...props} />;
}
},
renderScrollableContent() {
if (Platform.OS === 'ios') {
return (
<ScrollView
horizontal
pagingEnabled
style={styles.scrollableContentIOS}
contentContainerStyle={styles.scrollableContentContainerIOS}
contentOffset={{x:this.props.initialPage * this.state.container.width}}
ref={(scrollView) => { this.scrollView = scrollView }}
onScroll={(e) => {
var offsetX = e.nativeEvent.contentOffset.x;
this._updateScrollValue(offsetX / this.state.container.width);
}}
onMomentumScrollBegin={(e) => {
var offsetX = e.nativeEvent.contentOffset.x;
this._updateSelectedPage(parseInt(offsetX / this.state.container.width));
}}
onMomentumScrollEnd={(e) => {
var offsetX = e.nativeEvent.contentOffset.x;
this._updateSelectedPage(parseInt(offsetX / this.state.container.width));
}}
scrollEventThrottle={16}
showsHorizontalScrollIndicator={false}
scrollEnabled={!this.props.locked}
directionalLockEnabled
alwaysBounceVertical={false}>
{this._children().map((child,idx) => {
return <View
key={child.props.tabLabel + '_' + idx}
style={{width: this.state.container.width}}>
{child}
</View>
})}
</ScrollView>
);
} else {
return (
<ViewPagerAndroid
style={styles.scrollableContentAndroid}
initialPage={this.props.initialPage}
onPageSelected={this._updateSelectedPage}
onPageScroll={(e) => {
const {offset, position} = e.nativeEvent;
this._updateScrollValue(position + offset);
}}
ref={(scrollView) => { this.scrollView = scrollView }}>
{this._children().map((child,idx) => {
return <View
key={child.props.tabLabel + '_' + idx}
style={{width: this.state.container.width}}>
{child}
</View>
})}
</ViewPagerAndroid>
);
}
},
_updateSelectedPage(currentPage) {
if (typeof currentPage === 'object') {
currentPage = currentPage.nativeEvent.position;
}
this.setState({currentPage}, () => {
this.props.onChangeTab({ i: currentPage });
});
},
_updateScrollValue(value) {
this.state.scrollValue.setValue(value);
this.props.onScroll(value);
},
_handleLayout(e) {
var {width, height} = e.nativeEvent.layout;
var container = this.state.container;
if (width !== container.width || height !== container.height) {
this.setState({container: e.nativeEvent.layout});
InteractionManager.runAfterInteractions(() => {
this.goToPage(this.state.currentPage);
});
}
},
_children() {
return React.Children.map(this.props.children, (child) => child);
},
render() {
var tabBarProps = {
goToPage: this.goToPage,
tabs: this._children().map((child) => child.props.tabLabel),
activeTab: this.state.currentPage,
scrollValue: this.state.scrollValue,
underlineColor : this.props.tabBarUnderlineColor,
backgroundColor : this.props.tabBarBackgroundColor,
activeTextColor : this.props.tabBarActiveTextColor,
inactiveTextColor : this.props.tabBarInactiveTextColor,
containerWidth: this.state.container.width,
};
return (
<View style={[styles.container, this.props.style]} onLayout={this._handleLayout}>
{this.props.tabBarPosition === 'top' ? this.renderTabBar(tabBarProps) : null}
{this.renderScrollableContent()}
{this.props.tabBarPosition === 'bottom' ? this.renderTabBar(tabBarProps) : null}
</View>
);
}
});
module.exports = ScrollableTabView;
var styles = StyleSheet.create({
container: {
flex: 1,
},
scrollableContentContainerIOS: {
flex: 1,
},
scrollableContentIOS: {
flexDirection: 'column',
},
scrollableContentAndroid: {
flex: 1,
},
});