Skip to content

Commit f5a0ee1

Browse files
committed
use button for scrollable tab bar
1 parent df3c3b7 commit f5a0ee1

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

ScrollableTabBar.js

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ const {
55
Animated,
66
StyleSheet,
77
ScrollView,
8-
TouchableOpacity,
98
Text,
109
Platform,
11-
findNodeHandle,
1210
Dimensions,
1311
} = ReactNative;
12+
const Button = require('./Button');
1413

1514
const WINDOW_WIDTH = Dimensions.get('window').width;
1615

@@ -74,7 +73,10 @@ const ScrollableTabBar = React.createClass({
7473
},
7574

7675
necessarilyMeasurementsCompleted(position) {
77-
return this._tabsMeasurements[position] && this._tabsMeasurements[position + 1];
76+
return this._tabsMeasurements[position] &&
77+
this._tabsMeasurements[position + 1] &&
78+
this._tabContainerMeasurements &&
79+
this._containerMeasurements;
7880
},
7981

8082
updateTabPanel(position, pageOffset) {
@@ -125,29 +127,26 @@ const ScrollableTabBar = React.createClass({
125127
const textColor = isTabActive ? activeTextColor : inactiveTextColor;
126128
const fontWeight = isTabActive ? 'bold' : 'normal';
127129

128-
return <TouchableOpacity
130+
return <Button
129131
key={`${name}_${page}`}
130-
ref={'tab_' + page}
131132
accessible={true}
132133
accessibilityLabel={name}
133134
accessibilityTraits='button'
134-
style={[styles.tab, this.props.tabStyle]}
135135
onPress={() => this.props.goToPage(page)}
136136
onLayout={this.measureTab.bind(this, page)}
137137
>
138-
<Text style={[{color: textColor, fontWeight, }, textStyle, ]}>
139-
{name}
140-
</Text>
141-
</TouchableOpacity>;
138+
<View style={[styles.tab, this.props.tabStyle]}>
139+
<Text style={[{color: textColor, fontWeight, }, textStyle, ]}>
140+
{name}
141+
</Text>
142+
</View>
143+
</Button>;
142144
},
143145

144-
measureTab(page) {
145-
const tabContainerhandle = findNodeHandle(this.refs.tabContainer);
146-
this.refs['tab_' + page].measureLayout(tabContainerhandle, (ox, oy, width, height, pageX, pageY) => {
147-
this._tabsMeasurements[page] = {left: ox, right: ox + width, width: width, height: height, };
148-
149-
this.updateView({value: this.props.scrollValue._value, });
150-
});
146+
measureTab(page, event) {
147+
const { x, width, height, } = event.nativeEvent.layout;
148+
this._tabsMeasurements[page] = {left: x, right: x + width, width, height, };
149+
this.updateView({value: this.props.scrollValue._value, });
151150
},
152151

153152
render() {
@@ -194,10 +193,12 @@ const ScrollableTabBar = React.createClass({
194193
width = WINDOW_WIDTH;
195194
}
196195
this.setState({ _containerWidth: width, });
196+
this.updateView({value: this.props.scrollValue._value, });
197197
},
198198

199199
onContainerLayout(e) {
200200
this._containerMeasurements = e.nativeEvent.layout;
201+
this.updateView({value: this.props.scrollValue._value, });
201202
},
202203
});
203204

0 commit comments

Comments
 (0)