Skip to content

Commit d8cbf1d

Browse files
committed
flexible tabs and container
support landscape, containers less than full screen closes ptomasroos#4 closes ptomasroos#81
1 parent 4beeeb4 commit d8cbf1d

File tree

5 files changed

+44
-27
lines changed

5 files changed

+44
-27
lines changed

DefaultTabBar.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22

33
var React = require('react-native');
44
var {
5-
Dimensions,
65
StyleSheet,
76
Text,
87
TouchableOpacity,
98
View,
109
Animated,
1110
} = React;
1211

13-
var deviceWidth = Dimensions.get('window').width;
1412

1513
var styles = StyleSheet.create({
1614
tab: {
@@ -58,17 +56,18 @@ var DefaultTabBar = React.createClass({
5856
},
5957

6058
render() {
59+
var containerWidth = this.props.containerWidth;
6160
var numberOfTabs = this.props.tabs.length;
6261
var tabUnderlineStyle = {
6362
position: 'absolute',
64-
width: deviceWidth / numberOfTabs,
63+
width: containerWidth / numberOfTabs,
6564
height: 4,
6665
backgroundColor: this.props.underlineColor || "navy",
6766
bottom: 0,
6867
};
6968

7069
var left = this.props.scrollValue.interpolate({
71-
inputRange: [0, 1], outputRange: [0, deviceWidth / numberOfTabs]
70+
inputRange: [0, 1], outputRange: [0, containerWidth / numberOfTabs]
7271
});
7372

7473
return (

examples/FacebookTabsExample/FacebookTabBar.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ var {
66
Text,
77
View,
88
TouchableOpacity,
9-
Dimensions,
109
Animated,
1110
} = React;
1211

1312
var { Icon, } = require('react-native-icons');
14-
var deviceWidth = Dimensions.get('window').width;
1513

1614
var styles = StyleSheet.create({
1715
tab: {
@@ -81,17 +79,18 @@ var FacebookTabBar = React.createClass({
8179
},
8280

8381
render() {
82+
var containerWidth = this.props.containerWidth;
8483
var numberOfTabs = this.props.tabs.length;
8584
var tabUnderlineStyle = {
8685
position: 'absolute',
87-
width: deviceWidth / numberOfTabs,
86+
width: containerWidth / numberOfTabs,
8887
height: 3,
8988
backgroundColor: '#3b5998',
9089
bottom: 0,
9190
};
9291

9392
var left = this.props.scrollValue.interpolate({
94-
inputRange: [0, 1], outputRange: [0, deviceWidth / numberOfTabs]
93+
inputRange: [0, 1], outputRange: [0, containerWidth / numberOfTabs]
9594
});
9695

9796
return (

examples/FacebookTabsExample/FacebookTabsExample.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ var {
66
Text,
77
View,
88
ScrollView,
9-
Dimensions,
109
} = React;
1110

1211
var ScrollableTabView = require('react-native-scrollable-tab-view');
1312
var FacebookTabBar = require('./FacebookTabBar');
14-
var deviceWidth = Dimensions.get('window').width;
1513

1614
var FacebookTabsExample = React.createClass({
1715
render() {
@@ -57,7 +55,7 @@ var styles = StyleSheet.create({
5755
marginTop: 30,
5856
},
5957
tabView: {
60-
width: deviceWidth,
58+
flex: 1,
6159
padding: 10,
6260
backgroundColor: 'rgba(0,0,0,0.01)',
6361
},

examples/FacebookTabsExample/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
"dependencies": {
99
"react-native": "^0.16.0",
1010
"react-native-icons": "^0.7.0",
11-
"react-native-scrollable-tab-view": "brentvatne/react-native-scrollable-tab-view"
11+
"react-native-scrollable-tab-view": "../.."
1212
}
1313
}

index.js

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ var {
1010
StyleSheet,
1111
ViewPagerAndroid,
1212
PropTypes,
13+
InteractionManager,
1314
} = React;
1415

1516
var DefaultTabBar = require('./DefaultTabBar');
1617
var deviceWidth = Dimensions.get('window').width;
18+
var deviceHeight = Dimensions.get('window').height;
1719

1820
var ScrollableTabView = React.createClass({
1921
statics: {
@@ -39,6 +41,10 @@ var ScrollableTabView = React.createClass({
3941
return {
4042
currentPage: this.props.initialPage,
4143
scrollValue: new Animated.Value(this.props.initialPage),
44+
container: {
45+
width: deviceWidth,
46+
height: deviceHeight,
47+
}
4248
};
4349
},
4450

@@ -52,7 +58,7 @@ var ScrollableTabView = React.createClass({
5258
this.props.onChangeTab({ i: pageNumber, ref: this.props.children[pageNumber] });
5359

5460
if(Platform.OS === 'ios') {
55-
var offset = pageNumber * deviceWidth
61+
var offset = pageNumber * this.state.container.width;
5662
this.scrollView.scrollTo(0, offset);
5763
} else {
5864
this.scrollView.setPage(pageNumber);
@@ -78,26 +84,32 @@ var ScrollableTabView = React.createClass({
7884
horizontal
7985
pagingEnabled
8086
style={styles.scrollableContentIOS}
81-
contentOffset={{x:this.props.initialPage * deviceWidth}}
87+
contentOffset={{x:this.props.initialPage * this.state.container.width}}
8288
ref={(scrollView) => { this.scrollView = scrollView }}
8389
onScroll={(e) => {
8490
var offsetX = e.nativeEvent.contentOffset.x;
85-
this._updateScrollValue(offsetX / deviceWidth);
91+
this._updateScrollValue(offsetX / this.state.container.width);
8692
}}
8793
onMomentumScrollBegin={(e) => {
8894
var offsetX = e.nativeEvent.contentOffset.x;
89-
this._updateSelectedPage(parseInt(offsetX / deviceWidth));
95+
this._updateSelectedPage(parseInt(offsetX / this.state.container.width));
9096
}}
9197
onMomentumScrollEnd={(e) => {
9298
var offsetX = e.nativeEvent.contentOffset.x;
93-
this._updateSelectedPage(parseInt(offsetX / deviceWidth));
99+
this._updateSelectedPage(parseInt(offsetX / this.state.container.width));
94100
}}
95101
scrollEventThrottle={16}
96102
showsHorizontalScrollIndicator={false}
97103
scrollEnabled={!this.props.locked}
98104
directionalLockEnabled
99105
alwaysBounceVertical={false}>
100-
{this.props.children}
106+
{this.props.children.map((child,idx) => {
107+
return <View
108+
key={child.props.tabLabel + '_' + idx}
109+
style={{width: this.state.container.width}}>
110+
{child}
111+
</View>
112+
})}
101113
</ScrollView>
102114
);
103115
} else {
@@ -113,10 +125,10 @@ var ScrollableTabView = React.createClass({
113125
ref={(scrollView) => { this.scrollView = scrollView }}>
114126
{this.props.children.map((child,idx) => {
115127
return <View
116-
key={child.props.tabLabel + '_' + idx}
117-
style={{width: deviceWidth}}>
118-
{child}
119-
</View>
128+
key={child.props.tabLabel + '_' + idx}
129+
style={{width: this.state.container.width}}>
130+
{child}
131+
</View>
120132
})}
121133
</ViewPagerAndroid>
122134
);
@@ -136,11 +148,19 @@ var ScrollableTabView = React.createClass({
136148
this.state.scrollValue.setValue(value);
137149
},
138150

139-
render() {
140-
var translateX = this.state.scrollValue.interpolate({
141-
inputRange: [0, 1], outputRange: [0, -deviceWidth]
142-
});
151+
_handleLayout(e) {
152+
var {width, height} = e.nativeEvent.layout;
153+
var container = this.state.container;
154+
155+
if (width !== container.width || height !== container.height) {
156+
this.setState({container: e.nativeEvent.layout});
157+
InteractionManager.runAfterInteractions(() => {
158+
this.goToPage(this.state.currentPage);
159+
});
160+
}
161+
},
143162

163+
render() {
144164
var tabBarProps = {
145165
goToPage: this.goToPage,
146166
tabs: this.props.children.map((child) => child.props.tabLabel),
@@ -150,10 +170,11 @@ var ScrollableTabView = React.createClass({
150170
backgroundColor : this.props.tabBarBackgroundColor,
151171
activeTextColor : this.props.tabBarActiveTextColor,
152172
inactiveTextColor : this.props.tabBarInactiveTextColor,
173+
containerWidth: this.state.container.width,
153174
};
154175

155176
return (
156-
<View style={styles.container}>
177+
<View style={styles.container} onLayout={this._handleLayout}>
157178
{this.props.tabBarPosition === 'top' ? this.renderTabBar(tabBarProps) : null}
158179
{this.renderScrollableContent()}
159180
{this.props.tabBarPosition === 'bottom' ? this.renderTabBar(tabBarProps) : null}

0 commit comments

Comments
 (0)