forked from aksonov/react-native-tabs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (92 loc) · 2.86 KB
/
index.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
101
102
103
'use strict';
import React, {
Component
} from 'react';
import {
StyleSheet,
View,
Text,
TouchableOpacity,
Keyboard,
Platform,
} from 'react-native';
type State = {
keyboardUp: boolean,
}
class Tabs extends Component {
state: State = {};
onSelect(el){
if (el.props.onSelect) {
el.props.onSelect(el);
} else if (this.props.onSelect) {
this.props.onSelect(el);
}
}
componentWillMount(){
if (Platform.OS==='android') {
this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', this.keyboardWillShow);
this.keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', this.keyboardWillHide);
}
}
componentWillUnmount(){
if (Platform.OS==='android') {
this.keyboardDidShowListener.remove();
this.keyboardDidHideListener.remove();
}
}
keyboardWillShow = (e) => {
this.setState({ keyboardUp: true });
};
keyboardWillHide = (e) => {
this.setState({ keyboardUp: false });
};
render(){
const self = this;
let selected = this.props.selected
if (!selected){
React.Children.forEach(this.props.children.filter(c=>c), el=>{
if (!selected || el.props.initial){
selected = el.props.name || el.key;
}
});
}
return (
<View style={[styles.tabbarView, this.props.style, this.state.keyboardUp && styles.hidden]}>
{React.Children.map(this.props.children.filter(c=>c),(el)=>
<TouchableOpacity key={el.props.name+"touch"}
testID={el.props.testID}
style={[styles.iconView, this.props.iconStyle, (el.props.name || el.key) == selected ? this.props.selectedIconStyle || el.props.selectedIconStyle || {} : {} ]}
onPress={()=>!self.props.locked && self.onSelect(el)}
onLongPress={()=>self.onSelect(el)}
activeOpacity={el.props.pressOpacity}>
{selected == (el.props.name || el.key) ? React.cloneElement(el, {selected: true, style: [el.props.style, this.props.selectedStyle, el.props.selectedStyle]}) : el}
</TouchableOpacity>
)}
</View>
);
}
}
var styles = StyleSheet.create({
tabbarView: {
position:'absolute',
bottom:0,
right:0,
left:0,
height:50,
opacity:1,
backgroundColor:'transparent',
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center'
},
iconView: {
flex: 1,
height: 50,
justifyContent: 'center',
alignItems: 'center',
},
hidden: {
height: 0,
},
});
export default Tabs;