Skip to content

Commit 77b80d4

Browse files
author
Pavlo Aksonov
committed
remove state from this component for simplicity
1 parent 1c98c7b commit 77b80d4

File tree

5 files changed

+22
-58
lines changed

5 files changed

+22
-58
lines changed

Example/index.ios.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ var Tabs = require('react-native-tabs');
1616
class Example extends React.Component {
1717
constructor(props){
1818
super(props);
19-
this.state = {};
19+
this.state = {page:'second'};
2020
}
2121
render() {
2222
var self = this;
2323
return (
2424
<View style={styles.container}>
25-
<Tabs selected="second" style={{backgroundColor:'white'}}
25+
<Tabs selected={this.state.page} style={{backgroundColor:'white'}}
2626
selectedStyle={{color:'red'}} onSelect={el=>this.setState({page:el.props.name})}>
2727
<Text name="first">First</Text>
2828
<Text name="second" selectedIconStyle={{borderTopWidth:2,borderTopColor:'red'}}>Second</Text>
2929
<Text name="third">Third</Text>
3030
<Text name="fourth" selectedStyle={{color:'green'}}>Fourth</Text>
31-
<Text name="fifth" onSelect={()=>false}>Fifth</Text>
31+
<Text name="fifth">Fifth</Text>
3232
</Tabs>
3333
<Text style={styles.welcome}>
3434
Welcome to React Native

Example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
},
88
"dependencies": {
99
"react-native": "^0.20.0",
10-
"react-native-tabs": "^1.0.0"
10+
"react-native-tabs": "^1.0.1"
1111
}
1212
}

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ React Native platform-independent tabs. Could be used for bottom tab bars as wel
1212
Component just iterates over all its children and makes them touchable ('name' is only required attribute of each child).
1313
selectedStyle property represents style should be applied for selected tabs. This property could be set for all tabs or for individual tab.
1414
selectedIconStyle represents style applied for selected tab.
15-
The same, onSelect handler could be set globally for all tabs or/and for individual tab. If handler returns false, tab will not be changed.
15+
The same, onSelect handler could be set globally for all tabs or/and for individual tab.
1616
You can lock tab buttons (require user to use long press to actuate the button) by passing prop {locked: true}.
1717

1818
## Example
@@ -34,26 +34,26 @@ var Tabs = require('react-native-tabs');
3434
class Example extends React.Component {
3535
constructor(props){
3636
super(props);
37-
this.state = {};
37+
this.state = {page:'second'};
3838
}
3939
render() {
4040
var self = this;
4141
return (
4242
<View style={styles.container}>
43-
<Text style={styles.welcome}>
44-
Welcome to React Native!
45-
</Text>
46-
<Text style={styles.instructions}>
47-
Selected page: {this.state.page}
48-
</Text>
49-
<Tabs selected="second" style={{backgroundColor:'white'}}
43+
<Tabs selected={this.state.page} style={{backgroundColor:'white'}}
5044
selectedStyle={{color:'red'}} onSelect={el=>this.setState({page:el.props.name})}>
5145
<Text name="first">First</Text>
52-
<Text name="second">Second</Text>
46+
<Text name="second" selectedIconStyle={{borderTopWidth:2,borderTopColor:'red'}}>Second</Text>
5347
<Text name="third">Third</Text>
5448
<Text name="fourth" selectedStyle={{color:'green'}}>Fourth</Text>
55-
<Text name="fifth" onSelect={()=>false}>Fifth</Text>
49+
<Text name="fifth">Fifth</Text>
5650
</Tabs>
51+
<Text style={styles.welcome}>
52+
Welcome to React Native
53+
</Text>
54+
<Text style={styles.instructions}>
55+
Selected page: {this.state.page}
56+
</Text>
5757
</View>
5858
);
5959
}

index.js

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,12 @@ var {
1010
} = React;
1111

1212
class Tabs extends Component {
13-
14-
getSelectedElement(props, selected){
15-
let result = null;
16-
React.Children.forEach(props.children, el => {
17-
if (selected == el.props.name || result == null) {
18-
result = el;
19-
}
20-
});
21-
return result;
22-
}
23-
24-
25-
constructor(props){
26-
super(props);
27-
this.state = {selected : props.selected};
28-
this.onSelect = this.onSelect.bind(this);
29-
}
30-
31-
componentWillMount(){
32-
this.onSelect(this.getSelectedElement(this.props, this.props.selected));
33-
}
34-
35-
componentDidUpdate({selected}){
36-
// change selected state if selected is new value
37-
if (selected && selected != this.props.selected && selected != this.state.selected){
38-
this.onSelect(this.getSelectedElement(this.props, selected));
39-
}
40-
}
41-
4213
onSelect(el){
43-
if (el.props.onSelect){
44-
if (el.props.onSelect(el)===false){
45-
return;
46-
}
47-
}
48-
if (this.props.onSelect){
49-
if (this.props.onSelect(el)===false){
50-
return;
51-
}
14+
if (el.props.onSelect) {
15+
el.props.onSelect(el);
5216
}
53-
if (this.state.selected != el.props.name){
54-
this.setState({selected: el.props.name});
17+
if (this.props.onSelect) {
18+
this.props.onSelect(el);
5519
}
5620
}
5721

@@ -61,10 +25,10 @@ class Tabs extends Component {
6125
<View style={[styles.tabbarView, this.props.style]}>
6226
{this.props.children.map((el)=>
6327
<TouchableOpacity key={el.props.name+"touch"}
64-
style={[styles.iconView, this.props.iconStyle, el.props.name == this.state.selected ? this.props.selectedIconStyle || el.props.selectedIconStyle || {} : {} ]}
28+
style={[styles.iconView, this.props.iconStyle, el.props.name == this.props.selected ? this.props.selectedIconStyle || el.props.selectedIconStyle || {} : {} ]}
6529
onPress={()=>!self.props.locked && self.onSelect(el)}
6630
onLongPress={()=>self.props.locked && self.onSelect(el)}>
67-
{self.state.selected == el.props.name ? React.cloneElement(el, {style: {...el.props.style, ...this.props.selectedStyle, ...el.props.selectedStyle}}) : el}
31+
{self.props.selected == el.props.name ? React.cloneElement(el, {style: {...el.props.style, ...this.props.selectedStyle, ...el.props.selectedStyle}}) : el}
6832
</TouchableOpacity>
6933
)}
7034
</View>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-tabs",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "React Native platform-independent tabs. Could be used for bottom tab bars as well as sectioned views (with tab buttons)",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)