-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
129 lines (118 loc) · 2.57 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
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
/**
* Created by WittBulter on 16/9/5.
*/
import React, {Component} from 'react'
import {StyleSheet, View, Text, Image, TouchableHighlight, Dimensions} from 'react-native'
import Item from './item'
export default class Tabbar extends Component {
static propTypes = {
...View.propTypes,
style: View.propTypes.style,
activeColor: React.PropTypes.string,
}
static Item = Item
// 构造
constructor (props){
super(props)
// 初始状态
this.state = {
content: this.props.children,
contentActive: 0,
textActive: this.props.activeColor? this.props.activeColor: '#FE985B'
}
this.children = '2'
}
/**
*
* @param children {Array} children pages footer
*/
footerBar (children){
/**
*
* @param done {function} item callback
* @param index {number} onpress index
*/
const pressHandle = (done, index) =>{
this.setState({
contentActive: index
})
done&& done()
}
return children.map((item, index) =>{
const active = this.state.contentActive== index
const selected = item.props.selectedIcon? item.props.selectedIcon: item.props.icon
return (
<TouchableHighlight key={`tabbar-item${index}`}
style={styles.footerButton}
onPress={() => pressHandle(item.props.onPress, index)}
underlayColor={'transparent'}
>
<View style={styles.box}>
<Image source={active? selected: item.props.icon}
style={styles.icon}
/>
<Text style={[styles.text, active&& {color: this.state.textActive}]}
>
{item.props.text}
</Text>
</View>
</TouchableHighlight>
)
})
}
render (){
console.log(this.state.content);
return (
<View style={styles.body}>
<View style={styles.content}>
{this.state.content[this.state.contentActive].props.children}
</View>
<View style={styles.footer}
{...this.props.style}
>
{this.footerBar(this.props.children)}
</View>
</View>
)
}
}
const {width} = Dimensions.get('window')
const styles = StyleSheet.create({
body: {
flex: 1,
width: width,
overflow: 'hidden',
},
content: {
flex: 1,
},
box: {
flex: 1,
alignItems: 'center',
},
footer: {
width: width,
flexDirection: 'row',
backgroundColor: '#fff',
borderTopWidth: 1,
borderTopColor: '#E5E5E5'
},
footerButton: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
paddingTop: 5,
paddingBottom: 5,
},
icon: {
width: 22,
height: 22,
alignItems: 'center',
},
text: {
fontSize: 12,
color: '#9B9DB0',
paddingTop: 3,
textAlign: 'center'
},
})