-
Notifications
You must be signed in to change notification settings - Fork 15
/
CNavigation.js
356 lines (319 loc) · 10.1 KB
/
CNavigation.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/** react 组建的引用 */
import React, {Component} from "react";
import {
StyleSheet, Dimensions, Text, View, Image, Platform, StatusBar, NetInfo,TouchableWithoutFeedback
} from "react-native";
/** 全局样式的引用 */
import {Layout} from "../styles/layout";
import {Size} from "../styles/size";
/** 第三方依赖库的引用 */
import {SafeAreaView, withNavigation} from 'react-navigation';
import PropTypes from 'prop-types';
/** 自定义组建的引用 */
import HOCTouchable from './HOC/CTouchable';
import CStatusBar from './CStatusBar';
import {Util} from "../utils/util";
/** 自定义工具方法的引用 */
/** 一些常量的声明 */
const CTouchableWithoutFeedback = HOCTouchable(TouchableWithoutFeedback)
const {width, height} = Dimensions.get('window');//屏幕宽高
const
LEFT_ICON_BLACK = require('../images/common/navig_img_back_black.png'),
LEFT_ICON_WHITE = require('../images/common/navig_img_back_white.png'),
RIGHT_ICON = require('../images/common/common_img_arrow.png'),
WHITE_COLOR = Layout.color.white_bg,
BLACK_COLOR = Layout.color.black;
@withNavigation
class LeftButtonItem extends Component {
static propTypes = {
theme: PropTypes.oneOf(['dark', 'light', 'variable']),
leftButton: PropTypes.shape({
isShowTitle: PropTypes.bool,
title: PropTypes.string,
isShowIcon: PropTypes.bool,
iconStyle: PropTypes.object,
handle: PropTypes.func,
titleStyle: PropTypes.object,
}),
}
static defaultProps = {
theme: 'dark',
}
constructor(props) {
super(props);
this.defaultLeftButton = {
isShowTitle: false,
title: '左',
isShowIcon: false,
handle: null,
iconStyle: {
width: 23,
height: 23,
}, // 默认icon样式
titleStyle: {
fontSize: 16,
}, // 默认文案的样式
};
this.state = {};
}
componentDidMount() {
}
_navigate = () => {
const {handle} = Object.assign(this.defaultLeftButton, this.props.leftButton)
/** 执行特定的事件 */
if (handle && handle instanceof Function) {
handle()
}
/** 左边按钮默认返回上一页 */
else {
this.props.navigation.pop()
}
}
render() {
const {isShowTitle, title, titleStyle, iconStyle, isShowIcon} = Object.assign(this.defaultLeftButton, this.props.leftButton)
const {theme} = this.props
return (
<CTouchableWithoutFeedback handle={this._navigate}>
<View style={[styles.btn]}>
{
isShowTitle ? <Text
style={[titleStyle,
{color: (theme === 'light' || theme === 'variable') ? WHITE_COLOR : BLACK_COLOR}
]}
numberOfLines={1}> {title} </Text>
: isShowIcon ?
<Image source={theme === 'dark' ? LEFT_ICON_BLACK : LEFT_ICON_WHITE} style={iconStyle}/> : null
}
</View>
</CTouchableWithoutFeedback>
);
}
}
@withNavigation
class RightButtonItem extends Component {
static propTypes = {
theme: PropTypes.oneOf(['dark', 'light', 'variable']),
rightButton: PropTypes.shape({
isShowTitle: PropTypes.bool,
title: PropTypes.string,
isShowIcon: PropTypes.bool,
iconStyle: PropTypes.object,
handle: PropTypes.func,
titleStyle: PropTypes.object,
}),
}
static defaultProps = {
theme: 'dark',
}
constructor(props) {
super(props);
this.defaultRightButton = {
isShowTitle: false,
title: '右',
isShowIcon: false,
handle: null,
iconStyle: {
width: 23,
height: 23,
}, // 默认icon样式
titleStyle: {
color: '#000'
}, // 默认文案的样式
};
this.state = {};
}
_navigate = () => {
const {handle} = Object.assign(this.defaultRightButton, this.props.rightButton)
/** 执行特定的事件 */
if (handle && handle instanceof Function) {
handle()
}
}
render() {
const {isShowTitle, title, titleStyle, iconStyle, isShowIcon} = Object.assign(this.defaultRightButton, this.props.rightButton)
const {theme} = this.props
return (
<CTouchableWithoutFeedback handle={this._navigate}>
<View style={[styles.btn, {justifyContent: 'flex-end',}]}>
{
isShowTitle ? <Text
style={[titleStyle, {color: (theme === 'light' || theme === 'variable') ? WHITE_COLOR : (titleStyle.color ? titleStyle.color : BLACK_COLOR)}]}
numberOfLines={1}> {title} </Text>
: isShowIcon ?
<Image source={theme === 'dark' ? RIGHT_ICON : RIGHT_ICON} style={iconStyle}/> : null
}
</View>
</CTouchableWithoutFeedback>
);
}
}
@withNavigation
class TitleItem extends Component {
static propTypes = {
centerTitle: PropTypes.shape({
title: PropTypes.string,
handle: PropTypes.func,
titleStyle: PropTypes.object,
})
}
constructor(props) {
super(props);
this.defaultcenterTitle = {
handle: null,
title: '',
titleStyle: {
color: '#000',
}, // 导航文字样式
};
this.state = {};
}
render() {
const {title, titleStyle, handle} = Object.assign(this.defaultcenterTitle, this.props.centerTitle)
const {theme} = this.props
return (
<CTouchableWithoutFeedback onPress={handle}>
<View>
<Text
style={[
titleStyle, {
color: (theme === 'light' || theme === 'variable') ? WHITE_COLOR : BLACK_COLOR
}]}
numberOfLines={1}>
{title ? title : null}
</Text>
</View>
</CTouchableWithoutFeedback>
);
}
}
@withNavigation
export default class CNavigation extends Component {
static propTypes = {
getRef: PropTypes.func,
LeftOrRight: PropTypes.string,
commonBackgroundColor: PropTypes.string,
navBackgroundColor: PropTypes.string,
isPaddingTop: PropTypes.bool,
isSafeAreaTop: PropTypes.bool,
isSafeAreaBottom: PropTypes.bool,
isNavContent: PropTypes.bool,
theme: PropTypes.oneOf(['dark', 'light', 'variable']),
barStyle: PropTypes.string,
}
static defaultProps = {
getRef: null,
commonBackgroundColor: Layout.color.white_bg, // 视图的背景颜色
navBackgroundColor: Layout.color.white_bg, // 导航的背景颜色
LeftOrRight: 'all',
isPaddingTop: true, // 默认有paddingTop
isSafeAreaTop: true, // 是否设置安全区域
isSafeAreaBottom: true, // 是否设置安全区域
isNavContent: true, // 是否需要导航
theme: 'dark', // 导航的主题颜色
barStyle: 'dark-content',
}
constructor(props) {
super(props);
this.state = {
theme: props.theme,
barStyle: props.barStyle,
navBackgroundColor: props.navBackgroundColor,
};
props.getRef instanceof Function && props.getRef(this)
}
componentWillMount() {
NetInfo.isConnected.addEventListener('connectionChange', this._handleFirstConnectivityChange);
}
_handleFirstConnectivityChange = (isConnected) => {
// window.console.log(`-------- 当前联网状态为 '${isConnected}-------`);
NetInfo.isConnected.removeEventListener('connectionChange', this._handleFirstConnectivityChange);
}
componentDidMount() {
this._fadeInBottomLine()
}
shouldComponentUpdate(nextProps) {
/** 针对不需要导航的页面阻止该组件没必要的render */
return true
}
componentWillUnmount() {
}
/** 根据滚动动态修改导航以及状态栏的样式 */
_fadeInBottomLine = (p) => {
const {theme} = this.props
let _navBackgroundColor, _barStyle, _theme
if (theme === 'variable') {
_navBackgroundColor = p ? (p >= 0.5 ? `rgba(255,255,255,${p})` : 'transparent') : 'transparent'
_barStyle = (p >= 0.5) ? 'dark-content' : 'light-content'
_theme = (p >= 0.5) ? 'dark' : 'light'
this.setState({
navBackgroundColor: _navBackgroundColor,
barStyle: _barStyle,
theme: _theme,
})
}
}
render() {
const {LeftOrRight, commonBackgroundColor, isPaddingTop, isSafeAreaTop, isNavContent, isSafeAreaBottom} = this.props
const {barStyle, navBackgroundColor, theme} = this.state
return (
<SafeAreaView
ref={ref => this._CNavInstance = ref}
forceInset={{top: isSafeAreaTop ? 'always' : 'never', bottom: isSafeAreaBottom ? 'always' : 'never'}}
style={[{
flex: 1, backgroundColor: commonBackgroundColor,
position: 'relative',
}]}>
<CStatusBar ref={ref => this._statusBarInstance = ref}
barStyle={barStyle}
/>
<View style={[styles.container, {paddingTop: isPaddingTop ? 44: 0}]}>
{
isNavContent ? <View style={[
styles.navContainer,
{height: (!isSafeAreaTop && Util.isIPhoneX()) ? 88 : (Util.isAndroid() ? (Size.screen.statusBarHeight + 44) : (!isSafeAreaTop ? 64 : 44))},
{paddingTop: (!isSafeAreaTop && Util.isIPhoneX()) ? 24 : (Util.isAndroid() ? StatusBar.currentHeight : (!isSafeAreaTop ? 20 : 0)) },
{backgroundColor: navBackgroundColor},
]}>
<View style={styles.buttonWrapper}>
{
LeftOrRight === 'left' || 'all' ? <LeftButtonItem {...this.props} theme={theme}/> : <View/>
}
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
<TitleItem {...this.props} theme={theme}/>
</View>
{
LeftOrRight === 'right' || 'all' ? <RightButtonItem {...this.props} theme={theme}/> : <View/>
}
</View>
</View> : null
}
{this.props.children}
</View>
</SafeAreaView>
);
}
}
const styles = StyleSheet.create({
container: {
position: 'relative',
flex: 1,
},
navContainer: {
position: 'absolute',
width: width,
zIndex: 10000,
paddingHorizontal: Layout.gap.gap_edge,
top: 0,
},
buttonWrapper: {
flex: 1,
flexDirection: 'row',
alignItems: 'center',
},
btn: {
flexDirection: 'row',
alignItems: 'center',
width: 60,
height: 44,
}
});