forked from DickyT/react-native-tabbar-navigator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainTabBar.js
100 lines (94 loc) · 2.42 KB
/
MainTabBar.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
'use strict';
var React = require('react-native');
var {
Component,
StyleSheet,
Text,
View,
TabBarIOS
} = React;
var style = StyleSheet.create({
rootView: {
flex: 1
}
});
class MainTabBar extends Component {
constructor(props) {
super(props);
this.tabBarData = [];
this.state = {
selectedTab: 0
};
}
componentWillMount() {
this.configureTabBar();
}
configureTabBar() {
var defaultTabIndex = 1;
React.Children.map(this.props.initialConfig, function(eachChild, index) {
var eachTabBarData = {
id: index,
title: eachChild.props.title,
icon: eachChild.props.icon,
selectedIcon: eachChild.props.selectedIcon,
component: eachChild.props.children
};
this.tabBarData.push(eachTabBarData);
if (eachChild.props.defaultTab) {
defaultTabIndex = index;
}
}.bind(this));
this.setState({
selectedTab: defaultTabIndex
});
this.props.navComponent.currentTabIndex = defaultTabIndex;
this.props.navComponent.setState({
rootNavigatorTitle: this.tabBarData[defaultTabIndex].title
});
}
switchTab(tabId, tabTitle, currentTabIndex) {
this.props.navComponent.currentTabIndex = currentTabIndex;
this.props.navComponent.setState({
currentTabIndex: currentTabIndex,
rootNavigatorTitle: tabTitle
});
this.props.navComponent.forceUpdate();
this.setState({
selectedTab: tabId
});
}
renderTabBarItems() {
var items = [];
var self = this;
for (var i = 0; i < this.tabBarData.length; i++) {
var eachData = this.tabBarData[i];
var eachComponent = React.cloneElement(eachData.component, {
navigator: this.props.navigator,
navComponent: this.props.navComponent
});
items.push(
<TabBarIOS.Item
key={i}
title={eachData.title}
icon={eachData.icon}
selectedIcon={eachData.selectedIcon}
selected={self.state.selectedTab === eachData.id}
onPress={self.switchTab.bind(self, eachData.id, eachData.title, i)}>
{eachComponent}
</TabBarIOS.Item>
);
}
return items;
}
render() {
return (
<TabBarIOS
style={style.flexEnabled}
tintColor={this.props.tabTintColor}
barTintColor={this.props.tabBarTintColor}>
{this.renderTabBarItems()}
</TabBarIOS>
);
}
}
module.exports = MainTabBar;