forked from vczero/react-native-tab-menu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tab.js
240 lines (226 loc) · 5.72 KB
/
tab.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
var React = require('react-native');
var {
StyleSheet,
Text,
View,
ScrollView,
TouchableOpacity,
} = React;
//设定内置的属性
//选中项,例如:_type_0_2 表示第一个Tab选中,并且第二个Tab中的第三项选中
var prefixType = '_type_';
//选中项样式,例如:_style_0_2 表示第一个Tab选中,并且第二个Tab中的第三项选中时的样式
var prefixStyle = '_style_';
//默认左侧选中的背景颜色
var defaultBackgroundColor = {backgroundColor:'#fff'};
var MenuList = React.createClass({
getInitialState: function(){
var data = this.props.data;
//左侧选择的index
var nSelected = this.props.nSelected;
//头部选择的index
var tabSelected = this.props.tabSelected;
var obj = {};
var kIndex = 0;
for(var k in data){
var childData = data[k];
var cIndex = 0;
for(var c in childData){
var type = prefixType + k + '_' + c;
var style = prefixStyle + k + '_' + c;
obj[type] = false;
obj[style] = {};
//设定默认选中项
if(nSelected === cIndex && tabSelected === kIndex){
obj[type] = true;
obj[style] = defaultBackgroundColor;
}
cIndex++;
}
kIndex++;
}
obj.tabSelected = tabSelected;
obj.nSelected = nSelected;
return obj;
},
render: function(){
var header = this.renderlHeader();
var left = this.renderLeft();
var right = this.renderRight();
return (
<View style={styles.container}>
<View style={[styles.row, styles.header]}>
{header}
</View>
<View style={[styles.row, styles.flex_1]}>
<ScrollView style={[styles.flex_1, styles.left_pannel]}>
{left}
</ScrollView>
<ScrollView style={[styles.flex_1, styles.right_pannel]}>
{right}
</ScrollView>
</View>
</View>
);
},
//渲染头部TabBar
renderlHeader: function(){
var data = this.props.data;
var tabSelected = this.state.tabSelected;
var header = [];
var tabIndex = 0;
for(var i in data){
var tabStyle = null;
if(tabIndex === tabSelected){
tabStyle=[styles.header_text, styles.active_blue];
}else{
tabStyle = [styles.header_text];
}
header.push(
<TouchableOpacity style={[styles.flex_1, styles.center]}
onPress={this.headerPress.bind(this, i)}>
<Text style={tabStyle}>{i}</Text>
</TouchableOpacity>
);
tabIndex ++;
}
return header;
},
//渲染左侧
renderLeft: function(){
var data = this.props.data;
var tabSelected = this.state.tabSelected;
var leftPannel = [];
var index = 0;
for(var i in data){
if(index === tabSelected){
for(var k in data[i]){
var style = this.state[prefixStyle + i + '_' + k];
leftPannel.push(
<Text onPress={this.leftPress.bind(this, i, k)}
style={[styles.left_row, style]}> {k}</Text>);
}
break;
}
index ++;
}
return leftPannel;
},
//渲染右边,二级菜单
renderRight: function(){
var data = this.props.data;
var tabSelected = this.state.tabSelected;
var nSelected = this.state.nSelected;
var index = 0;
var rightPannel = [];
for(var i in data){
if(tabSelected === index ){
for(var k in data[i]){
if(this.state[prefixType + i + '_' + k]){
for(var j in data[i][k]){
rightPannel.push(
<Text onPress={this.props.click.bind(this, data[i][k][j])} style={styles.left_row}>{data[i][k][j]}</Text>);
}
break;
}
}
}
index ++;
}
return rightPannel;
},
//点击左侧,展示右侧二级菜单
leftPress: function(tabIndex, nIndex){
var obj = {};
for(var k in this.state){
//将prefixType或者prefixStyle类型全部置false
if(k.indexOf(prefixType) > -1){
var obj = {};
obj[k] = false;
this.setState(obj);
}
if(k.indexOf(prefixStyle) > -1){
var obj = {};
obj[k] = {};
this.setState(obj);
}
}
obj[prefixType + tabIndex + '_' + nIndex] = true;
obj[prefixStyle + tabIndex + '_' + nIndex] = defaultBackgroundColor;
this.setState(obj);
},
//头部点击事件即Tab切换事件
headerPress: function(title){
var data = this.props.data;
var index = 0;
for(var i in data){
if(i === title){
this.setState({
tabSelected: index,
});
var obj = {};
var n = 0;
for(var k in data[i]){
if(n !== 0){
obj[prefixType + i + '_' + k] = false;
obj[prefixStyle + i + '_' + k] = {};
}else{
obj[prefixType + i + '_' + k] = true;
obj[prefixStyle + i + '_' + k] = defaultBackgroundColor;
}
n ++;
}
this.setState(obj);
}
index ++;
}
}
});
var styles = StyleSheet.create({
container:{
height:240,
flex:1,
borderTopWidth:1,
borderBottomWidth:1,
borderColor:'#ddd'
},
row:{
flexDirection: 'row'
},
flex_1:{
flex:1
},
header:{
height:35,
borderBottomWidth:1,
borderColor:'#DFDFDF',
backgroundColor:'#F5F5F5'
},
header_text:{
color:'#7B7B7B',
fontSize:15
},
center:{
justifyContent:'center',
alignItems:'center'
},
left_pannel:{
backgroundColor:'#F2F2F2',
},
left_row:{
height:30,
lineHeight:20,
fontSize:14,
color:'#7C7C7C',
},
right_pannel:{
marginLeft:10
},
active_blue:{
color: '#00B7EB'
},
active_fff:{
backgroundColor:'#fff'
}
});
module.exports = MenuList;