UNPKG

4.07 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = useNavigationHelpers;
7
8var _routers = require("@react-navigation/routers");
9
10var React = _interopRequireWildcard(require("react"));
11
12var _NavigationContext = _interopRequireDefault(require("./NavigationContext"));
13
14var _types = require("./types");
15
16var _UnhandledActionContext = _interopRequireDefault(require("./UnhandledActionContext"));
17
18function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
20function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
21
22function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
23
24// This is to make TypeScript compiler happy
25// eslint-disable-next-line babel/no-unused-expressions
26_types.PrivateValueStore;
27
28/**
29 * Navigation object with helper methods to be used by a navigator.
30 * This object includes methods for common actions as well as methods the parent screen's navigation object.
31 */
32function useNavigationHelpers(_ref) {
33 let {
34 id: navigatorId,
35 onAction,
36 getState,
37 emitter,
38 router
39 } = _ref;
40 const onUnhandledAction = React.useContext(_UnhandledActionContext.default);
41 const parentNavigationHelpers = React.useContext(_NavigationContext.default);
42 return React.useMemo(() => {
43 const dispatch = op => {
44 const action = typeof op === 'function' ? op(getState()) : op;
45 const handled = onAction(action);
46
47 if (!handled) {
48 onUnhandledAction === null || onUnhandledAction === void 0 ? void 0 : onUnhandledAction(action);
49 }
50 };
51
52 const actions = { ...router.actionCreators,
53 ..._routers.CommonActions
54 };
55 const helpers = Object.keys(actions).reduce((acc, name) => {
56 // @ts-expect-error: name is a valid key, but TypeScript is dumb
57 acc[name] = function () {
58 return dispatch(actions[name](...arguments));
59 };
60
61 return acc;
62 }, {});
63 const navigationHelpers = { ...parentNavigationHelpers,
64 ...helpers,
65 dispatch,
66 emit: emitter.emit,
67 isFocused: parentNavigationHelpers ? parentNavigationHelpers.isFocused : () => true,
68 canGoBack: () => {
69 const state = getState();
70 return router.getStateForAction(state, _routers.CommonActions.goBack(), {
71 routeNames: state.routeNames,
72 routeParamList: {},
73 routeGetIdList: {}
74 }) !== null || (parentNavigationHelpers === null || parentNavigationHelpers === void 0 ? void 0 : parentNavigationHelpers.canGoBack()) || false;
75 },
76 getId: () => navigatorId,
77 getParent: id => {
78 if (id !== undefined) {
79 let current = navigationHelpers;
80
81 while (current && id !== current.getId()) {
82 current = current.getParent();
83 }
84
85 return current;
86 }
87
88 return parentNavigationHelpers;
89 },
90 getState
91 };
92 return navigationHelpers;
93 }, [navigatorId, emitter.emit, getState, onAction, onUnhandledAction, parentNavigationHelpers, router]);
94}
95//# sourceMappingURL=useNavigationHelpers.js.map
\No newline at end of file