forked from LiuC520/react-native-scrollable-tab-view-forked
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DefaultTabBar.js
139 lines (128 loc) · 3.51 KB
/
DefaultTabBar.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
const React = require('react');
const { ViewPropTypes, TouchableOpacity } = (ReactNative = require('react-native'))
const PropTypes = require('prop-types');
const createReactClass = require('create-react-class');
const {
StyleSheet,
Text,
View,
Animated,
Dimensions,
} = ReactNative;
// const Button = require('./Button');
const DefaultTabBar = createReactClass({
propTypes: {
goToPage: PropTypes.func,
activeTab: PropTypes.number,
tabs: PropTypes.array,
backgroundColor: PropTypes.string,
activeTextColor: PropTypes.string,
inactiveTextColor: PropTypes.string,
textStyle: Text.propTypes.style,
tabStyle: ViewPropTypes.style,
renderTab: PropTypes.func,
underlineStyle: ViewPropTypes.style,
width:PropTypes.number,
},
getDefaultProps() {
return {
activeTextColor: 'navy',
inactiveTextColor: 'black',
backgroundColor: null,
width:Dimensions.get('window').width
};
},
renderTab(child, page, isTabActive, onPressHandler) {
const { activeTextColor, inactiveTextColor, textStyle } = this.props;
const textColor = isTabActive ? activeTextColor : inactiveTextColor;
const fontWeight = isTabActive ? 'bold' : 'normal';
return (
<TouchableOpacity
style={{ flex: 1 }}
key={child.tabLabel}
accessible
activeOpacity={1}
// accessibilityLabel={child.tabLabel}
accessibilityTraits='button'
onPress={() => onPressHandler(page)}
>
<View style={[styles.tab, this.props.tabStyle]}>
<Text style={[{ color: textColor, fontWeight }, textStyle]}>{child.tabLabel}</Text>
</View>
</TouchableOpacity>
)
},
/**
* 刘成修改源码:
* 增加tab可以固定宽度
*/
render() {
const width = this.props.width;
const containerWidth = this.props.containerWidth || width;
const numberOfTabs = this.props.tabs.length;
const eachWidth = containerWidth / numberOfTabs
const tabUnderlineStyle = {
position: 'absolute',
width: eachWidth,
height: 4,
backgroundColor: 'navy',
bottom: 0,
};
let left = 0;
if(this.props.underlineStyle && this.props.underlineStyle.width){
left = eachWidth / 2 - this.props.underlineStyle.width / 2;
}
const translateX = this.props.scrollValue.interpolate({
inputRange: [0, 1],
outputRange: [0, eachWidth],
});
return (
<View style={[styles.tabs,
{
backgroundColor: this.props.backgroundColor,
paddingRight: width - containerWidth
}, this.props.style]}
>
{this.props.tabs.map((child, page) => {
const isTabActive = this.props.activeTab === page;
const renderTab = this.props.renderTab || this.renderTab;
return renderTab(child, page, isTabActive, this.props.goToPage);
})}
<Animated.View
ref={sl=>this.animatedLine = sl}
style={[
tabUnderlineStyle,
{
left,
},
{
transform: [
{ translateX },
],
},
this.props.underlineStyle,
]}
/>
</View>
);
},
});
const styles = StyleSheet.create({
tab: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
// paddingBottom: 10,
},
tabs: {
height: 40,
flexDirection: 'row',
justifyContent: 'space-around',
borderWidth: 0.5,
borderTopWidth: 0,
borderLeftWidth: 0,
borderRightWidth: 0,
borderColor: '#E6E6E6',
},
});
module.exports = DefaultTabBar;