SectionTabBar.js
3.5 KB
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
import React from 'react';
import ReactNative from 'react-native';
const {
StyleSheet,
Text,
View,
Animated,
TouchableOpacity,
Dimensions,
} = ReactNative;
export default class SectionTabBar extends React.Component {
static propTypes = {
containerWidth: React.PropTypes.number,
goToPage: React.PropTypes.func,
activeTab: React.PropTypes.number,
tabs: React.PropTypes.array,
underlineColor: React.PropTypes.string,
underlineHeight: React.PropTypes.number,
backgroundColor: React.PropTypes.string,
activeTextColor: React.PropTypes.string,
inactiveTextColor: React.PropTypes.string,
textStyle: Text.propTypes.style,
tabStyle: View.propTypes.style,
};
static defaultProps = {
containerWidth: Dimensions.get('window').width,
activeTab: 0,
activeTextColor: 'black',
inactiveTextColor: '#b0b0b0',
underlineColor: 'black',
backgroundColor: 'white',
underlineHeight: 1.5,
};
constructor(props) {
super(props);
let activeTab = this.props.activeTab ? this.props.activeTab : 0;
this.state = {
activeTab: activeTab,
};
}
componentWillReceiveProps(nextProps) {
this.setState({
activeTab: nextProps.activeTab
});
}
renderTabOption(name, page) {
const isTabActive = this.state.activeTab === page;
const { activeTextColor, inactiveTextColor, textStyle, } = this.props;
const textColor = isTabActive ? activeTextColor : inactiveTextColor;
const fontWeight = isTabActive ? 'bold' : 'normal';
return (
<TouchableOpacity
key={name}
style={styles.tab}
onPress={() => {
this.props.goToPage(page);
this.setState({
activeTab: page
});
}}
>
<Text style={[{color: textColor, fontWeight, fontSize: 14}, textStyle, ]}>
{name}
</Text>
</TouchableOpacity>
);
}
render() {
const containerWidth = this.props.containerWidth;
const numberOfTabs = this.props.tabs.length;
const tabUnderlineStyle = {
position: 'absolute',
width: containerWidth / numberOfTabs,
height: this.props.underlineHeight,
backgroundColor: 'white',
bottom: 3.5,
alignItems: 'center',
};
const innerLineStyle = {
width: 100,
height: this.props.underlineHeight,
backgroundColor: this.props.underlineColor,
};
const left = this.props.scrollValue.interpolate({
inputRange: [0, 1, ], outputRange: [0, containerWidth / numberOfTabs, ],
});
return (
<View style={[styles.tabs, {backgroundColor: this.props.backgroundColor, }, this.props.style, ]}>
{this.props.tabs.map((tab, i) => this.renderTabOption(tab, i))}
<Animated.View style={[tabUnderlineStyle, {left}]} >
<View style={innerLineStyle}/>
</Animated.View>
</View>
);
}
}
let styles = StyleSheet.create({
tabs: {
height: 41,
flexDirection: 'row',
justifyContent: 'space-around',
},
tab: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
});