UNPKG

3.12 kBJavaScriptView Raw
1import React, {
2 Component,
3 ViewStylePropTypes,
4 PropTypes,
5} from 'react';
6import {
7 View,
8 Text,
9 StyleSheet,
10 Animated,
11} from 'react-native';
12import Touch from '../Touch';
13
14const styles = StyleSheet.create({
15 activeTextStyle: {
16 color: '#82A0FA',
17 },
18 tabDeafultText: {
19 color: '#888',
20 },
21 tabBackground: {
22 flex: 1,
23 justifyContent: 'center',
24 alignItems: 'center',
25 },
26 defaultTabBarStyle: {
27 // flex: 1,
28 height: 50,
29 flexDirection: 'row',
30 justifyContent: 'space-around',
31 borderWidth: 1,
32 borderTopWidth: 0,
33 borderLeftWidth: 0,
34 borderRightWidth: 0,
35 borderColor: '#ccc',
36 },
37 hideUnderline: {
38 backgroundColor: 'blue',
39 height: 2,
40 opacity: 0,
41 },
42 showUnderline: {
43 backgroundColor: 'blue',
44 height: 2,
45 },
46});
47class TabBar extends Component {
48 static propTypes = {
49 tabs: PropTypes.arrayOf(PropTypes.any),
50 goToScene: PropTypes.func,
51 renderTab: PropTypes.func,
52 activeTextStyle: Text.propTypes.style,
53 defaultTextStyle: Text.propTypes.style,
54 tabBarStyle: ViewStylePropTypes || (View.propTypes && View.propTypes.style),
55 tabUnderLineStyle: ViewStylePropTypes || (View.propTypes && View.propTypes.style), // 下划线背景
56 onChangeTab: PropTypes.func, // onPress事件
57 activeTab: PropTypes.number,
58 }
59 static defaultProps = {
60 tabs: [],
61 goToScene: null,
62 renderTab: () => {},
63 activeTextStyle: styles.activeTextStyle,
64 tabBarPosition: null,
65 onChangeTab: null,
66 defaultTextStyle: styles.tabDeafultText,
67 tabUnderLineStyle: null,
68 tabBarStyle: null,
69 activeTab: null,
70 }
71 constructor(props) {
72 super(props);
73 this.state = {
74 widthValue: new Animated.Value(0),
75 };
76 }
77
78 renderTab = (tabLabel, key, onPressHandler) => {
79 const {
80 activeTextStyle,
81 defaultTextStyle,
82 onChangeTab,
83 tabUnderLineStyle,
84 activeTab,
85 } = this.props;
86
87 const isTabActive = activeTab === key;
88
89 const textColor = isTabActive ? activeTextStyle : defaultTextStyle;
90
91 const underline = isTabActive ? (<View style={[styles.showUnderline, tabUnderLineStyle]} />) : <View style={styles.hideUnderline} />;
92
93 return (<Touch
94 style={[{ flex: 1 }]}
95 key={tabLabel}
96 accessible
97 accessibilityLabel={tabLabel}
98 accessibilityTraits="button"
99 onPress={() => onPressHandler(key)}
100 >
101 <View style={{ flex: 1, flexDirection: 'column', alignItems: 'center' }}>
102 <View style={styles.tabBackground}>
103 <Text style={textColor}>{tabLabel}</Text>
104 </View>
105 {underline}
106 </View>
107 </Touch>);
108 }
109
110 render() {
111 const {
112 tabs,
113 goToScene,
114 tabUnderLineStyle,
115 tabBarStyle,
116 } = this.props;
117 return (
118 <View style={[styles.defaultTabBarStyle, tabBarStyle]}>
119 { tabs.map((tabLabel, key) => {
120 const name = tabLabel;
121 const renderTab = this.props.renderTab || this.renderTab;
122 const Tab = this.renderTab(name, key, goToScene);
123 return Tab;
124 })}
125 </View>
126 );
127 }
128}
129export default TabBar;