forked from allmaxgit/react-navigation-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
src.js
104 lines (98 loc) · 2.88 KB
/
src.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
/**
* Created by Bardiaswift
*
* @flow
*/
/* eslint-disable no-underscore-dangle, camelcase */
import type { ElementRef } from 'react';
import { NavigationActions } from 'react-navigation';
import type {
NavigationScreenProp,
NavigationState,
NavigationParams,
NavigationNavigateAction,
} from 'react-navigation/src/TypeDefinition';
type Navigator = {
_navigation: NavigationScreenProp<NavigationState>,
subs: ?{ remove: () => void }
};
const navigators: { [routeName: string]: ?Navigator } = {};
export function setNavigatior(routeName: string, navigator: ElementRef<*>) {
if (navigator) {
const { subs } = navigator;
if (subs) {
subs.remove();
}
navigators[routeName] = navigator;
}
}
export const makeNavigation = (navigationRouteName: string) => ({
navigate: (
routeName: string,
params?: NavigationParams,
action?: NavigationNavigateAction,
): boolean => {
const navigator = navigators[navigationRouteName];
if (navigator) {
return navigator._navigation.navigate(routeName, params, action);
}
return false;
},
setParams: (params: NavigationParams): boolean => {
const navigator = navigators[navigationRouteName];
if (navigator) {
const { _navigation } = navigator;
const action = NavigationActions.setParams({
key: _navigation.state.routes[0].key,
params,
});
_navigation.dispatch(action);
}
return false;
},
goBack: () => {
const navigator = navigators[navigationRouteName];
if (navigator) {
navigator._navigation.goBack();
}
},
reset: (routeName?: string | string[], params?: Object) => {
const navigator = navigators[navigationRouteName];
if (navigator) {
const { _navigation } = navigator;
let action: NavigationNavigateAction;
if (routeName) {
const actions: NavigationNavigateAction[] = [];
let index: number;
if (Array.isArray(routeName)) {
index = routeName.length - 1;
routeName.forEach((rn, i) => {
const payload: {
routeName: string,
params?: ?NavigationParams,
action?: ?NavigationNavigateAction,
} = { routeName: rn };
if (i === index && params) {
payload.params = params;
}
actions.push(NavigationActions.navigate(payload));
});
} else {
actions.push(NavigationActions.navigate({ routeName, params }));
index = 0;
}
action = NavigationActions.reset({ actions, index });
} else {
action = NavigationActions.back({ key: _navigation.state.routes[1].key });
}
_navigation.dispatch(action);
}
},
getCanNavigateBack: () => {
const navigator = navigators[navigationRouteName];
if (navigator) {
return navigator._navigation.state.index > 0;
}
return false;
},
});