|
1 | | -'use strict'; |
2 | | - |
3 | | -var React = require('react-native'); |
4 | | -var { |
5 | | - StyleSheet, |
6 | | - Text, |
7 | | - TouchableOpacity, |
8 | | - View, |
9 | | - Animated, |
10 | | - } = React; |
11 | | - |
12 | | -var deviceWidth = require('Dimensions').get('window').width; |
13 | | - |
14 | | - |
15 | | -var underLineColor = '#3498DB' |
16 | | -var activeTabTextColor = 'rgba(0,0,0,9)' |
17 | | -var normalTabTextColor = 'rgba(0,0,0,0.4)' |
18 | | - |
19 | | -var styles = StyleSheet.create({ |
20 | | - tab: { |
21 | | - flex: 1, |
22 | | - alignItems: 'center', |
23 | | - justifyContent: 'center', |
24 | | - paddingBottom: 10, |
25 | | - paddingTop: 10 |
26 | | - }, |
27 | | - |
28 | | - tabs: { |
29 | | - height: 50 + 4, |
30 | | - flexDirection: 'row', |
31 | | - marginTop: 0, |
32 | | - borderWidth: 1, |
33 | | - borderTopWidth: 0, |
34 | | - borderLeftWidth: 0, |
35 | | - borderRightWidth: 0, |
36 | | - borderBottomColor: 'rgba(0,0,0,0.06)', |
37 | | - justifyContent: 'space-around' |
38 | | - }, |
39 | | -}); |
| 1 | +import React, { |
| 2 | + StyleSheet, |
| 3 | + Text, |
| 4 | + TouchableOpacity, |
| 5 | + View, |
| 6 | + Dimensions, |
| 7 | + Component, |
| 8 | + PropTypes, |
| 9 | + Animated |
| 10 | +} from 'react-native'; |
| 11 | + |
| 12 | + |
| 13 | +const { width } = Dimensions.get('window'); |
| 14 | +const underLineColor = '#3498DB'; |
| 15 | +const activeTabTextColor = 'rgba(0,0,0,9)'; |
| 16 | +const normalTabTextColor = 'rgba(0,0,0,0.4)'; |
| 17 | + |
| 18 | +class TabBar extends Component { |
| 19 | + static propTypes = { |
| 20 | + goToPage: PropTypes.func, |
| 21 | + activeTab: PropTypes.number, |
| 22 | + tabs: PropTypes.array, |
| 23 | + tabStyle: PropTypes.object, |
| 24 | + tabTextStyle: PropTypes.object, |
| 25 | + activeTabTextColor: PropTypes.string, |
| 26 | + normalTabTextColor: PropTypes.string |
| 27 | + }; |
| 28 | + |
| 29 | + |
| 30 | + static defaultProps = { |
| 31 | + activeTabTextColor, |
| 32 | + normalTabTextColor |
| 33 | + }; |
| 34 | + |
| 35 | + |
| 36 | + constructor(props) { |
| 37 | + super(props); |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + renderTabOption(name, page) { |
| 42 | + const isTabActive = this.props.activeTab === page; |
| 43 | + const textStyle = { |
| 44 | + color: isTabActive ? this.props.activeTabTextColor : this.props.normalTabTextColor, |
| 45 | + fontWeight: isTabActive ? 'bold' : 'normal' |
| 46 | + }; |
| 47 | + |
| 48 | + return ( |
| 49 | + <TouchableOpacity key={name} onPress={() => this.props.goToPage(page)}> |
| 50 | + <View style={[styles.tab, this.props.tabStyle]}> |
| 51 | + <Text style={[ textStyle, this.props.tabTextStyle ]}> |
| 52 | + { name } |
| 53 | + </Text> |
| 54 | + </View> |
| 55 | + </TouchableOpacity> |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + render() { |
| 61 | + var numberOfTabs = this.props.tabs.length; |
| 62 | + var tabUnderlineStyle = { |
| 63 | + position: 'absolute', |
| 64 | + width: width / numberOfTabs, |
| 65 | + height: 4, |
| 66 | + backgroundColor: underLineColor, |
| 67 | + bottom: 0 |
| 68 | + }; |
| 69 | + |
| 70 | + var left = this.props.scrollValue.interpolate({ |
| 71 | + inputRange: [0, 1], outputRange: [0, width / numberOfTabs] |
| 72 | + }); |
| 73 | + |
| 74 | + return ( |
| 75 | + <View style={[ styles.tabs, this.props.style]}> |
| 76 | + {this.props.tabs.map((tab, i) => this.renderTabOption(tab, i))} |
| 77 | + <Animated.View style={[tabUnderlineStyle, this.props.tabUnderlineStyle, {left}]}/> |
| 78 | + </View> |
| 79 | + ); |
| 80 | + } |
| 81 | +} |
| 82 | + |
40 | 83 |
|
| 84 | +const styles = StyleSheet.create({ |
| 85 | + tab: { |
| 86 | + flex: 1, |
| 87 | + alignItems: 'center', |
| 88 | + justifyContent: 'center', |
| 89 | + paddingBottom: 10, |
| 90 | + paddingTop: 10 |
| 91 | + }, |
41 | 92 |
|
42 | | -var DefaultTabBar = React.createClass({ |
43 | | - propTypes: { |
44 | | - goToPage: React.PropTypes.func, |
45 | | - activeTab: React.PropTypes.number, |
46 | | - tabs: React.PropTypes.array |
47 | | - }, |
48 | | - |
49 | | - renderTabOption(name, page) { |
50 | | - var isTabActive = this.props.activeTab === page; |
51 | | - |
52 | | - return ( |
53 | | - <TouchableOpacity key={name} onPress={() => this.props.goToPage(page)}> |
54 | | - <View style={[styles.tab]}> |
55 | | - <Text |
56 | | - style={{color: isTabActive ? activeTabTextColor : normalTabTextColor, fontWeight: isTabActive ? 'bold' : 'normal'}}>{name}</Text> |
57 | | - </View> |
58 | | - </TouchableOpacity> |
59 | | - ); |
60 | | - }, |
61 | | - |
62 | | - render() { |
63 | | - var numberOfTabs = this.props.tabs.length; |
64 | | - var tabUnderlineStyle = { |
65 | | - position: 'absolute', |
66 | | - width: deviceWidth / numberOfTabs, |
67 | | - height: 4, |
68 | | - backgroundColor: underLineColor, |
69 | | - bottom: 0, |
70 | | - }; |
71 | | - |
72 | | - var left = this.props.scrollValue.interpolate({ |
73 | | - inputRange: [0, 1], outputRange: [0, deviceWidth / numberOfTabs] |
74 | | - }); |
75 | | - |
76 | | - return ( |
77 | | - <View style={styles.tabs}> |
78 | | - {this.props.tabs.map((tab, i) => this.renderTabOption(tab, i))} |
79 | | - <Animated.View style={[tabUnderlineStyle, {left}]}/> |
80 | | - </View> |
81 | | - ); |
82 | | - }, |
| 93 | + tabs: { |
| 94 | + height: 50 + 4, |
| 95 | + flexDirection: 'row', |
| 96 | + marginTop: 0, |
| 97 | + borderWidth: 1, |
| 98 | + borderTopWidth: 0, |
| 99 | + borderLeftWidth: 0, |
| 100 | + borderRightWidth: 0, |
| 101 | + borderBottomColor: 'rgba(0,0,0,0.06)', |
| 102 | + justifyContent: 'space-around' |
| 103 | + } |
83 | 104 | }); |
84 | 105 |
|
85 | | -module.exports = DefaultTabBar; |
86 | 106 |
|
| 107 | +export default TabBar; |
0 commit comments