forked from JunIce/react-native-scroll-customTabBar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlexibleTabBar.js
143 lines (122 loc) · 4.2 KB
/
FlexibleTabBar.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
140
141
142
143
import React,{Component} from 'react'
import {StyleSheet,
Text,
View,
Animated,
TouchableNativeFeedback,
TouchableOpacity,
Platform
} from 'react-native'
export default class CustomTabBar extends Component {
constructor(props: TabBarProps) {
super(props);
this.state = {
activeDefaultColor: '#08086b',
inactiveDefaultColor: '#666666'
}
}
_renderTab(name, page, isTabActive, onPressHandler) {
const { textStyle } = this.props;
const textColor = isTabActive ? this.props.activeColor : this.props.inactiveColor;
const fontWeight = isTabActive ? 'bold' : 'normal';
const Button = Platform.OS == 'ios' ? ButtonIos : ButtonAndroid;
return (<Button
style={{flex: 1}}
key={name}
accessible={true}
accessibilityLabel={name}
accessibilityTraits='button'
onPress={() => onPressHandler(page)}
>
<View style={styles.tab}>
<Text style={[{color: textColor, fontWeight } ]}>
{name}
</Text>
</View>
</Button>);
}
_renderUnderline() {
const containerWidth = this.props.containerWidth;
const numberOfTabs = this.props.tabs.length;
const underlineWidth = this.props.tabUnderlineDefaultWidth ? this.props.tabUnderlineDefaultWidth : containerWidth / (numberOfTabs * 2);
const scale = this.props.tabUnderlineScaleX ? this.props.tabUnderlineScaleX : 3;
const deLen = (containerWidth / numberOfTabs - underlineWidth ) / 2;
const tabUnderlineStyle = {
position: 'absolute',
width: underlineWidth,
height: 2,
borderRadius: 2,
backgroundColor: this.props.activeColor,
bottom: 0,
left: deLen
};
const translateX = this.props.scrollValue.interpolate({
inputRange: [0, 1],
outputRange: [0, containerWidth / numberOfTabs],
});
const scaleValue = (defaultScale) => {
let arr = new Array(numberOfTabs * 2);
return arr.fill(0).reduce(function(pre, cur, idx){
idx == 0 ? pre.inputRange.push(cur) : pre.inputRange.push(pre.inputRange[idx-1] + 0.5);
idx%2 ? pre.outputRange.push(defaultScale) : pre.outputRange.push(1)
return pre
}, {inputRange: [], outputRange: []})
}
const scaleX = this.props.scrollValue.interpolate(scaleValue(scale));
return(
<Animated.View
style={[
tabUnderlineStyle,
{
transform: [
{ translateX },
{ scaleX }
],
},
this.props.underlineStyle,
]}
/>
)
}
render() {
return (
<View style={[styles.tabs, {backgroundColor: this.props.backgroundColor}, this.props.style]}>
{this.props.tabs.map((name, page) => {
const isTabActive = this.props.activeTab === page;
return this._renderTab(name, page, isTabActive, this.props.goToPage)
})}
{
this._renderUnderline()
}
</View>
);
};
}
const ButtonAndroid = (props) => (
<TouchableNativeFeedback
delayPressIn={0}
background={TouchableNativeFeedback.SelectableBackground()}
{...props}
>
{props.children}
</TouchableNativeFeedback>);
const ButtonIos = (props) => (<TouchableOpacity {...props}>
{props.children}
</TouchableOpacity>);
const styles = StyleSheet.create({
tab: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
tabs: {
height: 50,
flexDirection: 'row',
justifyContent: 'space-around',
borderWidth: 1,
borderTopWidth: 0,
borderLeftWidth: 0,
borderRightWidth: 0,
borderColor: '#f4f4f4',
},
});