Skip to content

Commit 5b1225c

Browse files
author
Pavlo Aksonov
committed
improve TabBar, support initial prop for Routes
1 parent 6dd16fc commit 5b1225c

File tree

4 files changed

+26
-56
lines changed

4 files changed

+26
-56
lines changed

Example/Example.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export default class Example extends React.Component {
9090
<Route name="tab1_2" component={TabView} title="Tab #1_2" />
9191
</Router>
9292
</Route>
93-
<Route name="tab2" schema="tab" title="Tab #2" hideNavBar={true}>
93+
<Route name="tab2" schema="tab" title="Tab #2" hideNavBar={true} initial={true}>
9494
<Router onPop={()=>{console.log("onPop is called!"); return true} }>
9595
<Route name="tab2_1" component={TabView} title="Tab #2_1" />
9696
<Route name="tab2_2" component={TabView} title="Tab #2_2" />

Example/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
"start": "node_modules/react-native/packager/packager.sh"
77
},
88
"dependencies": {
9-
"react-native": "^0.19.0",
9+
"react-native": "^0.20.0",
1010
"react-native-button": "^1.2.1",
11-
"react-native-router-flux": "^2.2.7",
11+
"react-native-router-flux": "^2.3.0",
1212
"react-native-modalbox": "^1.3.0",
1313
"react-redux": "^4.4.0",
1414
"redux": "^3.3.1"

TabBar.js

Lines changed: 21 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,67 +4,37 @@ import Tabs from 'react-native-tabs';
44

55
import Actions from './Actions';
66
export default class TabBar extends React.Component {
7-
constructor(props){
8-
super(props);
9-
}
107
onSelect(el){
118
if (!Actions[el.props.name]){
129
throw new Error("No action is defined for name="+el.props.name+" actions:"+JSON.stringify(Object.keys(Actions)));
1310
}
14-
if (el.props.selected && Actions[el.props.defaultRoute]) {
15-
Actions[el.props.defaultRoute]({hideTabBar: el.props.hideTabBar});
16-
} else {
17-
Actions[el.props.name]({hideTabBar: el.props.hideTabBar});
18-
}
19-
InteractionManager.runAfterInteractions(() =>
20-
this.setState({hideTabBar: el.props.hideTabBar}));
21-
return {selected: true};
22-
}
23-
getChildrenState(selectedRoute){
24-
var self = this;
25-
let selected = false;
26-
var children = [];
27-
React.Children.forEach(this.props.children, function(el, index){
28-
const schema = self.props.router && self.props.router.schemas[el.props.schema] ? self.props.router.schemas[el.props.schema] : {};
29-
let props = {...schema, ...el.props};
30-
if (!el.props.name)
31-
console.error("No name is defined for element");
32-
if (selectedRoute){
33-
if (selectedRoute == el.props.name){
34-
props.selected = true;
35-
} else {
36-
props.selected = false;
37-
}
38-
}
39-
40-
var Icon = props.icon || console.error("No icon class is defined for "+el.name);
41-
children.push(<Icon key={el.props.name} {...props}/>);
42-
if (props.selected || index === 0){
43-
selected = el;
44-
}
45-
});
46-
return {children, hideTabBar: selected.props.hideTabBar};
47-
}
48-
componentWillMount(){
49-
if (!this.props.children){
50-
return;
51-
}
52-
this.state = this.getChildrenState(this.props.selected);
53-
54-
}
11+
Actions[el.props.name]({hideTabBar: el.props.hideTabBar});
5512

56-
componentWillReceiveProps({selected}){
57-
//console.log("TABBAR "+selected);
58-
InteractionManager.runAfterInteractions(() =>
59-
this.setState(this.getChildrenState(selected)));
6013
}
6114
render(){
62-
if (this.state.hideTabBar){
15+
if (this.props.hideTabBar){
6316
return <View/>
6417
}
18+
// choose initial route
19+
let selected = this.props.selected
20+
if (!selected){
21+
React.Children.forEach(this.props.children, el=>{
22+
if (!selected || el.props.initial){
23+
selected = el.props.name;
24+
}
25+
});
26+
}
6527
return (
66-
<Tabs style={[{backgroundColor:'white'}, this.props.tabBarStyle]} onSelect={this.onSelect.bind(this)} {...this.props}>
67-
{this.state.children}
28+
<Tabs style={[{backgroundColor:'white'}, this.props.tabBarStyle]} onSelect={this.onSelect.bind(this)} {...this.props} selected={selected}>
29+
{React.Children.map(this.props.children, el=>{
30+
const schema = this.props.router && this.props.router.schemas[el.props.schema] ? this.props.router.schemas[el.props.schema] : {};
31+
let props = {...schema, ...el.props};
32+
if (!el.props.name)
33+
console.error("No name is defined for element");
34+
35+
var Icon = props.icon || console.error("No icon class is defined for "+el.name);
36+
return <Icon key={el.props.name} {...props}/>;
37+
})}
6838
</Tabs>
6939
);
7040
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-router-flux",
3-
"version": "2.2.7",
3+
"version": "2.3.0",
44
"description": "React Native Router using Flux architecture",
55
"repository": {
66
"type": "git",
@@ -36,6 +36,6 @@
3636
"dependencies": {
3737
"@exponent/react-native-action-sheet": "^0.1.2",
3838
"@exponent/react-native-navigator": "^0.3.5",
39-
"react-native-tabs": "^0.1.8"
39+
"react-native-tabs": "^1.0.2"
4040
}
4141
}

0 commit comments

Comments
 (0)