Skip to content

Commit 8889da6

Browse files
committed
Merge remote-tracking branch 'anyx/render-tab-name-fix-and-examples'
2 parents c5f5781 + f37d69e commit 8889da6

File tree

4 files changed

+74
-36
lines changed

4 files changed

+74
-36
lines changed

DefaultTabBar.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const DefaultTabBar = React.createClass({
2020
inactiveTextColor: React.PropTypes.string,
2121
textStyle: Text.propTypes.style,
2222
tabStyle: View.propTypes.style,
23-
renderTabName: React.PropTypes.func,
23+
renderTab: React.PropTypes.func,
2424
},
2525

2626
getDefaultProps() {
@@ -30,37 +30,39 @@ const DefaultTabBar = React.createClass({
3030
underlineColor: 'navy',
3131
backgroundColor: null,
3232
underlineHeight: 4,
33-
renderTabName: this.renderTabName,
3433
};
3534
},
3635

3736
renderTabOption(name, page) {
3837
const isTabActive = this.props.activeTab === page;
3938

39+
let onPressHandler = this.props.goToPage;
40+
let renderTab = this.props.renderTab ? this.props.renderTab : this.renderTab;
41+
42+
return renderTab(name, page, isTabActive, onPressHandler);
43+
},
44+
45+
renderTab(name, page, isTabActive, onPressHandler) {
46+
const { activeTextColor, inactiveTextColor, textStyle, } = this.props;
47+
const textColor = isTabActive ? activeTextColor : inactiveTextColor;
48+
const fontWeight = isTabActive ? 'bold' : 'normal';
49+
4050
return <Button
4151
style={{flex: 1, }}
4252
key={name}
4353
accessible={true}
4454
accessibilityLabel={name}
4555
accessibilityTraits='button'
46-
onPress={() => this.props.goToPage(page)}
56+
onPress={() => onPressHandler(page)}
4757
>
48-
{this.renderTabName(name, page, isTabActive)}
58+
<View style={[styles.tab, this.props.tabStyle, ]}>
59+
<Text style={[{color: textColor, fontWeight, }, textStyle, ]}>
60+
{name}
61+
</Text>
62+
</View>
4963
</Button>;
5064
},
5165

52-
renderTabName(name, page, isTabActive) {
53-
const { activeTextColor, inactiveTextColor, textStyle, } = this.props;
54-
const textColor = isTabActive ? activeTextColor : inactiveTextColor;
55-
const fontWeight = isTabActive ? 'bold' : 'normal';
56-
57-
return <View style={[styles.tab, this.props.tabStyle, ]}>
58-
<Text style={[{color: textColor, fontWeight, }, textStyle, ]}>
59-
{name}
60-
</Text>
61-
</View>;
62-
},
63-
6466
render() {
6567
const containerWidth = this.props.containerWidth;
6668
const numberOfTabs = this.props.tabs.length;

ScrollableTabBar.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const ScrollableTabBar = React.createClass({
2828
tabStyle: View.propTypes.style,
2929
tabsContainerStyle: View.propTypes.style,
3030
textStyle: Text.propTypes.style,
31-
renderTabName: React.PropTypes.func,
31+
renderTab: React.PropTypes.func,
3232
},
3333

3434
getDefaultProps() {
@@ -42,7 +42,6 @@ const ScrollableTabBar = React.createClass({
4242
style: {},
4343
tabStyle: {},
4444
tabsContainerStyle: {},
45-
renderTabName: this.renderTabName,
4645
};
4746
},
4847

@@ -127,30 +126,34 @@ const ScrollableTabBar = React.createClass({
127126
renderTabOption(name, page) {
128127
const isTabActive = this.props.activeTab === page;
129128

129+
let onPressHandler = this.props.goToPage;
130+
let onLayoutHandler = this.measureTab.bind(this, page);
131+
let renderTab = this.props.renderTab ? this.props.renderTab : this.renderTab;
132+
133+
return renderTab(name, page, isTabActive, onPressHandler, onLayoutHandler);
134+
},
135+
136+
renderTab(name, page, isTabActive, onPressHandler, onLayoutHandler) {
137+
const { activeTextColor, inactiveTextColor, textStyle, } = this.props;
138+
const textColor = isTabActive ? activeTextColor : inactiveTextColor;
139+
const fontWeight = isTabActive ? 'bold' : 'normal';
140+
130141
return <Button
131142
key={`${name}_${page}`}
132143
accessible={true}
133144
accessibilityLabel={name}
134145
accessibilityTraits='button'
135-
onPress={() => this.props.goToPage(page)}
136-
onLayout={this.measureTab.bind(this, page)}
146+
onPress={() => onPressHandler(page)}
147+
onLayout={onLayoutHandler}
137148
>
138-
{this.renderTabName(name, page, isTabActive)}
149+
<View style={[styles.tab, this.props.tabStyle, ]}>
150+
<Text style={[{color: textColor, fontWeight, }, textStyle, ]}>
151+
{name}
152+
</Text>
153+
</View>
139154
</Button>;
140155
},
141156

142-
renderTabName(name, page, isTabActive) {
143-
const { activeTextColor, inactiveTextColor, textStyle, } = this.props;
144-
const textColor = isTabActive ? activeTextColor : inactiveTextColor;
145-
const fontWeight = isTabActive ? 'bold' : 'normal';
146-
147-
return <View style={[styles.tab, this.props.tabStyle, ]}>
148-
<Text style={[{color: textColor, fontWeight, }, textStyle, ]}>
149-
{name}
150-
</Text>
151-
</View>;
152-
},
153-
154157
measureTab(page, event) {
155158
const { x, width, height, } = event.nativeEvent.layout;
156159
this._tabsMeasurements[page] = {left: x, right: x + width, width, height, };

examples/FacebookTabsExample/DynamicExample.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
import React from 'react';
22
import {
33
Text,
4+
StyleSheet,
5+
View,
6+
TouchableHighlight,
47
} from 'react-native';
58
import TimerMixin from 'react-timer-mixin';
6-
9+
import Icon from 'react-native-vector-icons/Ionicons';
710
import ScrollableTabView, { DefaultTabBar, } from 'react-native-scrollable-tab-view';
811

12+
const styles = StyleSheet.create({
13+
tab: {
14+
flex: 1,
15+
flexDirection: 'row',
16+
alignItems: 'center',
17+
justifyContent: 'center',
18+
paddingBottom: 10,
19+
},
20+
icon: {
21+
marginRight: 3,
22+
},
23+
});
24+
925
const Child = React.createClass({
1026
onEnter() {
1127
console.log('enter: ' + this.props.i); // eslint-disable-line no-console
@@ -43,10 +59,27 @@ export default React.createClass({
4359
this.children[from].onLeave();
4460
},
4561

62+
renderTab(name, page, isTabActive, onPressHandler, onLayoutHandler) {
63+
return <TouchableHighlight
64+
key={`${name}_${page}`}
65+
onPress={() => onPressHandler(page)}
66+
onLayout={onLayoutHandler}
67+
style={{flex: 1, }}
68+
underlayColor="#aaaaaa"
69+
>
70+
<View style={[styles.tab, this.props.tabStyle, ]}>
71+
<Icon name="md-star" color="red" size={16} style={styles.icon}/>
72+
<Text style={[{color: 'blue', }, ]}>
73+
{name}
74+
</Text>
75+
</View>
76+
</TouchableHighlight>;
77+
},
78+
4679
render() {
4780
return <ScrollableTabView
4881
style={{marginTop: 20, }}
49-
renderTabBar={() => <DefaultTabBar />}
82+
renderTabBar={() => <DefaultTabBar renderTab={this.renderTab}/>}
5083
onChangeTab={this.handleChangeTab}
5184
>
5285
{this.state.tabs.map((tab, i) => {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"lint": "eslint -c .eslintrc .",
7+
"lint": "eslint -c .eslintrc . --ignore-path .gitignore",
88
"test": "echo \"Error: no test specified\" && exit 1"
99
},
1010
"repository": {

0 commit comments

Comments
 (0)