-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
NavBar.js
262 lines (248 loc) · 8.37 KB
/
NavBar.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import React from 'react';
import { HeaderBackButton } from 'react-navigation-stack';
import {
Platform, I18nManager, Image, StyleSheet, Text, TouchableOpacity, View,
} from 'react-native';
import { NavigationActions } from 'react-navigation';
import { DrawerActions } from 'react-navigation-drawer';
import _backButtonImage from '../images/back_chevron.png';
const hitSlop = {
top: 15,
bottom: 15,
left: 15,
right: 15,
};
export function BackButton(state) {
const textButtonStyle = [styles.barBackButtonText, state.backButtonTextStyle];
const style = [styles.backButton, state.leftButtonStyle];
const buttonImage = state.backButtonImage || _backButtonImage;
const tintColor = getValue(state.backButtonTintColor, state) || state.tintColor || state.navBarButtonColor || state.headerTintColor;
let onPress = state.onBack;
if (onPress) {
onPress = onPress.bind(null, state);
} else {
onPress = () => state.navigation.dispatch(NavigationActions.back());
}
// returning react-navigation's back button well styled for ios and android if rnrf4-supported customization
// is not required
if (!state.backButtonImage) {
return <HeaderBackButton onPress={onPress} title={state.backTitle} titleStyle={textButtonStyle} tintColor={tintColor} truncatedTitle={state.truncatedTitle} />;
}
const text = state.backTitle ? <Text style={textButtonStyle}>{state.backTitle}</Text> : null;
return (
<TouchableOpacity testID="backNavButton" style={styles.backButtonContainer} onPress={onPress}>
<View style={style}>
{buttonImage && !state.hideBackImage && <Image source={buttonImage} style={[styles.backButtonImage, state.barButtonIconStyle, state.leftButtonIconStyle, { tintColor }]} />}
{text}
</View>
</TouchableOpacity>
);
}
export function LeftButton(state) {
let onPress = state.onLeft;
let buttonImage = getValue(state.leftButtonImage, state);
let menuIcon = getValue(state.drawerIcon, state);
const style = [styles.leftButton, state.leftButtonStyle];
const leftButtonTextStyle = getValue(state.leftButtonTextStyle, state);
const leftButtonIconStyle = getValue(state.leftButtonIconStyle, state);
const leftButtonStyle = [styles.defaultImageStyle, leftButtonIconStyle];
const leftTitle = state.getLeftTitle ? state.getLeftTitle(state) : getValue(state.leftTitle, state);
const textColor = getValue(state.leftButtonTintColor, state);
const tintColor = textColor || state.tintColor || state.navBarButtonColor || state.headerTintColor;
const textStyle = [styles.barLeftButtonText, tintColor && { color: tintColor }, leftButtonTextStyle, textColor && { color: textColor }];
if (state.leftButton || state.left) {
const Button = state.leftButton || state.left;
return <Button {...state} key="leftNavBarBtn" testID="leftNavButton" style={[...style, ...leftButtonStyle]} textStyle={textStyle} />;
}
if (!onPress && !state.hideDrawerButton && (state.drawerImage || menuIcon) && state.drawerPosition !== 'right') {
buttonImage = state.drawerImage;
if (buttonImage || menuIcon) {
onPress = () => state.navigation.dispatch(DrawerActions.openDrawer());
}
if (!menuIcon) {
menuIcon = <Image source={buttonImage} style={[state.leftButtonIconStyle || styles.defaultImageStyle, { tintColor }]} />;
}
}
if (onPress && (leftTitle || buttonImage || menuIcon)) {
onPress = onPress.bind(null, state);
return (
<TouchableOpacity key="leftNavBarBtn" testID="leftNavButton" style={style} onPress={onPress} hitSlop={state.hitSlop || hitSlop}>
{leftTitle && <Text style={textStyle}>{leftTitle}</Text>}
{!leftTitle && (menuIcon || buttonImage) && (
<View
style={{
flex: 1,
justifyContent: 'center',
alignItems: 'flex-start',
}}
>
{menuIcon || <Image source={buttonImage} style={[state.leftButtonIconStyle || styles.defaultImageStyle, { tintColor }]} />}
</View>
)}
</TouchableOpacity>
);
}
if (!!state.onLeft ^ !!(leftTitle || buttonImage || menuIcon)) {
console.warn(`Both onLeft and leftTitle/leftButtonImage
must be specified for the scene: ${state.name}`);
}
return null;
}
function getValue(value, params) {
return value instanceof Function ? value(params) : value;
}
export function RightButton(state) {
if (!state) {
return null;
}
let onPress = state.onRight;
let buttonImage = getValue(state.rightButtonImage, state);
let menuIcon = state.drawerIcon;
const style = [styles.rightButton, state.rightButtonStyle];
const rightButtonTextStyle = getValue(state.rightButtonTextStyle, state);
const rightButtonIconStyle = getValue(state.rightButtonIconStyle, state);
const rightButtonStyle = [styles.defaultImageStyle, rightButtonIconStyle];
const rightTitle = state.getRightTitle ? state.getRightTitle(state) : getValue(state.rightTitle, state);
const textColor = getValue(state.rightButtonTintColor, state);
const tintColor = textColor || state.tintColor || state.navBarButtonColor || state.headerTintColor;
const textStyle = [styles.barRightButtonText, tintColor && { color: tintColor }, rightButtonTextStyle, textColor && { color: textColor }];
if (state.rightButton || state.right) {
const Button = state.rightButton || state.right;
return <Button {...state} key="rightNavBarBtn" testID="rightNavButton" style={style} textButtonStyle={textStyle} />;
}
if (!onPress && !state.hideDrawerButton && state.drawerImage && state.drawerPosition === 'right') {
buttonImage = state.drawerImage;
if (buttonImage || menuIcon) {
onPress = () => state.navigation.dispatch(DrawerActions.openDrawer());
}
if (!menuIcon) {
menuIcon = <Image source={buttonImage} style={[rightButtonStyle, { tintColor }]} />;
}
}
if (onPress && (rightTitle || buttonImage)) {
onPress = onPress.bind(null, state);
return (
<TouchableOpacity key="rightNavBarBtn" testID="rightNavButton" style={style} onPress={onPress} hitSlop={state.hitSlop || hitSlop}>
{rightTitle && <Text style={textStyle}>{rightTitle}</Text>}
{!rightTitle && buttonImage && (
<View
style={{
flex: 1,
justifyContent: 'center',
alignItems: 'flex-end',
}}
>
{menuIcon || <Image source={buttonImage} style={[state.rightButtonIconStyle || styles.defaultImageStyle, { tintColor }]} />}
</View>
)}
</TouchableOpacity>
);
}
if (!!state.onRight ^ !!(typeof rightTitle !== 'undefined' || typeof buttonImage !== 'undefined')) {
console.warn(`Both onRight and rightTitle/rightButtonImage
must be specified for the scene: ${state.routeName}`);
}
return null;
}
const styles = StyleSheet.create({
title: {
textAlign: 'center',
color: '#0A0A0A',
fontSize: 18,
width: 180,
alignSelf: 'center',
},
titleImage: {
width: 180,
alignSelf: 'center',
},
titleWrapper: {
marginTop: 10,
...Platform.select({
ios: {
top: 20,
},
android: {
top: 5,
},
windows: {
top: 5,
},
}),
},
header: {
backgroundColor: '#EFEFF2',
paddingTop: 0,
...Platform.select({
ios: {
height: 64,
},
android: {
height: 54,
},
windows: {
height: 54,
},
}),
borderBottomWidth: 0.5,
borderBottomColor: '#828287',
},
backButton: {
...Platform.select({
ios: {
top: 12,
},
android: {
top: 10,
},
windows: {
top: 8,
},
}),
left: 2,
paddingLeft: 8,
flexDirection: 'row',
transform: [{ scaleX: I18nManager.isRTL ? -1 : 1 }],
},
rightButton: {
right: 2,
paddingRight: 8,
},
leftButton: {
left: 2,
paddingLeft: 8,
},
barRightButtonText: {
color: 'rgb(0, 122, 255)',
textAlign: 'right',
fontSize: 17,
},
barBackButtonText: {
color: 'rgb(0, 122, 255)',
textAlign: 'left',
fontSize: 17,
paddingLeft: 6,
},
barLeftButtonText: {
color: 'rgb(0, 122, 255)',
textAlign: 'left',
fontSize: 17,
},
backButtonContainer: {
height: 50,
width: 70,
},
backButtonImage: {
...Platform.select({
android: {
marginTop: 5,
},
}),
width: 13,
height: 21,
},
defaultImageStyle: {
height: 24,
resizeMode: 'contain',
},
});