TabBarBottom.js
5.77 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import * as React from 'react';
import { Animated, TouchableWithoutFeedback, StyleSheet, View, Platform, Keyboard } from 'react-native';
import TabBarIcon from './TabBarIcon';
import SafeAreaView from '../SafeAreaView';
import withOrientation from '../withOrientation';
const majorVersion = parseInt(Platform.Version, 10);
const isIos = Platform.OS === 'ios';
const useHorizontalTabs = majorVersion >= 11 && isIos;
class TabBarBottom extends React.PureComponent {
// See https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/UIKitUICatalog/UITabBar.html
static defaultProps = {
activeTintColor: '#3478f6', // Default active tint color in iOS 10
activeBackgroundColor: 'transparent',
inactiveTintColor: '#929292', // Default inactive tint color in iOS 10
inactiveBackgroundColor: 'transparent',
showLabel: true,
showIcon: true,
allowFontScaling: true
};
_renderLabel = scene => {
const {
position,
navigation,
activeTintColor,
inactiveTintColor,
labelStyle,
showLabel,
showIcon,
isLandscape,
allowFontScaling
} = this.props;
if (showLabel === false) {
return null;
}
const { index } = scene;
const { routes } = navigation.state;
// Prepend '-1', so there are always at least 2 items in inputRange
const inputRange = [-1, ...routes.map((x, i) => i)];
const outputRange = inputRange.map(inputIndex => inputIndex === index ? activeTintColor : inactiveTintColor);
const color = position.interpolate({
inputRange,
outputRange: outputRange
});
const tintColor = scene.focused ? activeTintColor : inactiveTintColor;
const label = this.props.getLabel({ ...scene, tintColor });
let marginLeft = 0;
if (isLandscape && showIcon && useHorizontalTabs) {
marginLeft = LABEL_LEFT_MARGIN;
}
let marginTop = 0;
if (!isLandscape && showIcon && useHorizontalTabs) {
marginTop = LABEL_TOP_MARGIN;
}
if (typeof label === 'string') {
return <Animated.Text style={[styles.label, { color, marginLeft, marginTop }, labelStyle]} allowFontScaling={allowFontScaling}>
{label}
</Animated.Text>;
}
if (typeof label === 'function') {
return label({ ...scene, tintColor });
}
return label;
};
_renderIcon = scene => {
const {
position,
navigation,
activeTintColor,
inactiveTintColor,
renderIcon,
showIcon,
showLabel
} = this.props;
if (showIcon === false) {
return null;
}
return <TabBarIcon position={position} navigation={navigation} activeTintColor={activeTintColor} inactiveTintColor={inactiveTintColor} renderIcon={renderIcon} scene={scene} style={showLabel && useHorizontalTabs ? {} : styles.icon} />;
};
_renderTestIDProps = scene => {
const testIDProps = this.props.getTestIDProps && this.props.getTestIDProps(scene);
return testIDProps;
};
render() {
const {
position,
navigation,
jumpToIndex,
getOnPress,
getTestIDProps,
activeBackgroundColor,
inactiveBackgroundColor,
style,
animateStyle,
tabStyle,
isLandscape
} = this.props;
const { routes } = navigation.state;
const previousScene = routes[navigation.state.index];
// Prepend '-1', so there are always at least 2 items in inputRange
const inputRange = [-1, ...routes.map((x, i) => i)];
const tabBarStyle = [styles.tabBar, isLandscape && useHorizontalTabs ? styles.tabBarLandscape : styles.tabBarPortrait, style];
return <Animated.View style={animateStyle}>
<SafeAreaView style={tabBarStyle} forceInset={{ bottom: 'always', top: 'never' }}>
{routes.map((route, index) => {
const focused = index === navigation.state.index;
const scene = { route, index, focused };
const onPress = getOnPress(previousScene, scene);
const outputRange = inputRange.map(inputIndex => inputIndex === index ? activeBackgroundColor : inactiveBackgroundColor);
const backgroundColor = position.interpolate({
inputRange,
outputRange: outputRange
});
const justifyContent = this.props.showIcon ? 'flex-end' : 'center';
const extraProps = this._renderTestIDProps(scene) || {};
const { testID, accessibilityLabel } = extraProps;
return <TouchableWithoutFeedback key={route.key} testID={testID} accessibilityLabel={accessibilityLabel} onPress={() => onPress ? onPress({ previousScene, scene, jumpToIndex }) : jumpToIndex(index)}>
<Animated.View style={[styles.tab, isLandscape && useHorizontalTabs && styles.tabLandscape, !isLandscape && useHorizontalTabs && styles.tabPortrait, { backgroundColor }, tabStyle]}>
{this._renderIcon(scene)}
{this._renderLabel(scene)}
</Animated.View>
</TouchableWithoutFeedback>;
})}
</SafeAreaView>
</Animated.View>;
}
}
const LABEL_LEFT_MARGIN = 20;
const LABEL_TOP_MARGIN = 15;
const styles = StyleSheet.create({
tabBar: {
backgroundColor: '#F7F7F7', // Default background color in iOS 10
borderTopWidth: StyleSheet.hairlineWidth,
borderTopColor: 'rgba(0, 0, 0, .3)',
flexDirection: 'row'
},
tabBarLandscape: {
height: 29
},
tabBarPortrait: {
height: 49
},
tab: {
flex: 1,
alignItems: isIos ? 'center' : 'stretch',
justifyContent: 'flex-end'
},
tabPortrait: {
justifyContent: 'flex-end',
flexDirection: 'column'
},
tabLandscape: {
justifyContent: 'center',
flexDirection: 'row'
},
icon: {
flexGrow: 1
},
label: {
textAlign: 'center',
fontSize: 10,
marginBottom: 1.5,
backgroundColor: 'transparent'
}
});
export default withOrientation(TabBarBottom);