-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Router.js
157 lines (131 loc) · 3.77 KB
/
Router.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
/**
* Copyright (c) 2015-present, Pavel Aksonov
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import React, {
Component,
PropTypes,
} from 'react';
import { BackAndroid } from 'react-native';
import NavigationExperimental from 'react-native-experimental-navigation';
import Actions, { ActionMap } from './Actions';
import getInitialState from './State';
import Reducer, { findElement } from './Reducer';
import DefaultRenderer from './DefaultRenderer';
import Scene from './Scene';
import * as ActionConst from './ActionConst';
const {
RootContainer: NavigationRootContainer,
} = NavigationExperimental;
const propTypes = {
dispatch: PropTypes.func,
backAndroidHandler: PropTypes.func,
onBackAndroid: PropTypes.func,
onExitApp: PropTypes.func,
};
class Router extends Component {
constructor(props) {
super(props);
this.state = {};
this.renderNavigation = this.renderNavigation.bind(this);
this.handleProps = this.handleProps.bind(this);
this.handleBackAndroid = this.handleBackAndroid.bind(this);
}
componentDidMount() {
this.handleProps(this.props);
BackAndroid.addEventListener('hardwareBackPress', this.handleBackAndroid);
}
componentWillReceiveProps(props) {
this.handleProps(props);
}
componentWillUnmount() {
BackAndroid.removeEventListener('hardwareBackPress', this.handleBackAndroid);
}
handleBackAndroid() {
const {
backAndroidHandler,
onBackAndroid,
onExitApp,
} = this.props;
// optional for customizing handler
if (backAndroidHandler) {
return backAndroidHandler();
}
try {
Actions.pop();
if (onBackAndroid) {
onBackAndroid();
}
return true;
} catch (err) {
if (onExitApp) {
return onExitApp();
}
return false;
}
}
handleProps(props) {
let scenesMap;
if (props.scenes) {
scenesMap = props.scenes;
} else {
let scenes = props.children;
if (Array.isArray(props.children) || props.children.props.component) {
scenes = (
<Scene
key="__root"
hideNav
{...this.props}
>
{props.children}
</Scene>
);
}
scenesMap = Actions.create(scenes, props.wrapBy);
}
// eslint-disable-next-line no-unused-vars
const { children, styles, scenes, reducer, createReducer, ...parentProps } = props;
scenesMap.rootProps = parentProps;
const initialState = getInitialState(scenesMap);
const reducerCreator = props.createReducer || Reducer;
const routerReducer = props.reducer || (
reducerCreator({
initialState,
scenes: scenesMap,
}));
this.setState({ reducer: routerReducer });
}
renderNavigation(navigationState, onNavigate) {
if (!navigationState) {
return null;
}
Actions.get = key => findElement(navigationState, key, ActionConst.REFRESH);
Actions.callback = props => {
const constAction = (props.type && ActionMap[props.type] ? ActionMap[props.type] : null);
if (this.props.dispatch) {
if (constAction) {
this.props.dispatch({ ...props, type: constAction });
} else {
this.props.dispatch(props);
}
}
return (constAction ? onNavigate({ ...props, type: constAction }) : onNavigate(props));
};
return <DefaultRenderer onNavigate={onNavigate} navigationState={navigationState} />;
}
render() {
if (!this.state.reducer) return null;
return (
<NavigationRootContainer
reducer={this.state.reducer}
renderNavigation={this.renderNavigation}
/>
);
}
}
Router.propTypes = propTypes;
export default Router;