@rdyl/react-native-navigator-connect 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NavContext = void 0;
4
+ const react_1 = require("react");
5
+ exports.NavContext = (0, react_1.createContext)({});
package/dist/hooks.js CHANGED
@@ -3,17 +3,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useNavigator = void 0;
4
4
  const native_1 = require("@react-navigation/native");
5
5
  const react_1 = require("react");
6
+ const context_1 = require("./context");
6
7
  const useNavigator = () => {
8
+ const { findOption } = (0, react_1.useContext)(context_1.NavContext);
7
9
  const navigation = (0, native_1.useNavigation)();
8
10
  const route = (0, native_1.useRoute)();
9
11
  const option = (0, react_1.useMemo)(() => {
10
- if (!(global === null || global === void 0 ? void 0 : global.$findNavigatorOption))
11
- return;
12
- const opt = global.$findNavigatorOption(route.name);
12
+ const opt = findOption(route.name);
13
13
  if (opt) {
14
14
  return opt.option;
15
15
  }
16
- }, [route.name]);
16
+ }, [route.name, findOption]);
17
17
  const push = (0, react_1.useCallback)((name, params) => {
18
18
  navigation.dispatch(native_1.CommonActions.navigate({
19
19
  name,
@@ -24,10 +24,12 @@ const useNavigator = () => {
24
24
  if (navigation.canGoBack()) {
25
25
  navigation.dispatch(native_1.CommonActions.goBack());
26
26
  }
27
+ else {
28
+ throw Error();
29
+ }
27
30
  }, [navigation]);
28
31
  const redirect = (0, react_1.useCallback)((name, params) => {
29
- // @ts-ignore
30
- const opt = global.$findNavigatorOption(name);
32
+ const opt = findOption(name);
31
33
  if (opt) {
32
34
  if (opt.type === "tab") {
33
35
  navigation.dispatch(native_1.TabActions.jumpTo(name, params));
@@ -36,7 +38,7 @@ const useNavigator = () => {
36
38
  navigation.dispatch(native_1.StackActions.replace(name, params));
37
39
  }
38
40
  }
39
- }, [navigation]);
41
+ }, [navigation, findOption]);
40
42
  const onParams = (0, react_1.useCallback)((params = {}) => {
41
43
  navigation.dispatch(Object.assign(Object.assign({}, native_1.CommonActions.setParams(params)), { source: route.key }));
42
44
  }, [navigation, route]);
package/dist/index.js CHANGED
@@ -40,12 +40,13 @@ const react_1 = __importStar(require("react"));
40
40
  const native_1 = require("@react-navigation/native");
41
41
  const stack_1 = require("@react-navigation/stack");
42
42
  const bottom_tabs_1 = require("@react-navigation/bottom-tabs");
43
+ const context_1 = require("./context");
43
44
  const Tab = (0, bottom_tabs_1.createBottomTabNavigator)();
44
45
  const Stack = (0, stack_1.createStackNavigator)();
45
46
  const NavigatorConnect = ({ afterEach, stack = [], initial, tabs, }) => {
46
47
  const navigatorRef = (0, native_1.useNavigationContainerRef)();
47
48
  const [routeName, setRouteName] = (0, react_1.useState)("");
48
- const findNavigatorOption = (0, react_1.useCallback)((name) => {
49
+ const findOption = (0, react_1.useCallback)((name) => {
49
50
  const tab = ((tabs === null || tabs === void 0 ? void 0 : tabs.list) || []).find(ele => ele.name === name);
50
51
  if (tab) {
51
52
  return {
@@ -62,17 +63,14 @@ const NavigatorConnect = ({ afterEach, stack = [], initial, tabs, }) => {
62
63
  }
63
64
  return null;
64
65
  }, [tabs, stack]);
65
- const option = (0, react_1.useMemo)(() => {
66
- const opt = findNavigatorOption(routeName);
67
- if (opt) {
68
- return opt.option;
69
- }
70
- }, [findNavigatorOption, routeName]);
71
66
  (0, react_1.useEffect)(() => {
72
- if (option && afterEach) {
73
- afterEach(option);
67
+ if (afterEach) {
68
+ const opt = findOption(routeName);
69
+ if (opt) {
70
+ afterEach(opt.option);
71
+ }
74
72
  }
75
- }, [afterEach, option]);
73
+ }, [afterEach, routeName]);
76
74
  (0, react_1.useEffect)(() => {
77
75
  const callback = ({ data }) => {
78
76
  var _a;
@@ -84,25 +82,25 @@ const NavigatorConnect = ({ afterEach, stack = [], initial, tabs, }) => {
84
82
  navigatorRef.addListener("__unsafe_action__", callback);
85
83
  return () => navigatorRef.removeListener("__unsafe_action__", callback);
86
84
  }, [navigatorRef.isReady]);
87
- (0, react_1.useEffect)(() => {
88
- global.$findNavigatorOption = findNavigatorOption;
89
- }, [findNavigatorOption]);
90
- return (react_1.default.createElement(native_1.NavigationContainer, { ref: navigatorRef },
91
- react_1.default.createElement(Stack.Navigator, { initialRouteName: initial },
92
- !!tabs && (react_1.default.createElement(Stack.Screen, { key: "tabs", name: tabs.name, options: {
93
- headerShown: false,
94
- }, component: () => (react_1.default.createElement(Tab.Navigator, { initialRouteName: (tabs === null || tabs === void 0 ? void 0 : tabs.initial) || (tabs === null || tabs === void 0 ? void 0 : tabs.list[0].name) || "" }, tabs.list.map(ele => {
95
- const { component, name, meta: { title = "", tabBarIcon } = {}, } = ele;
96
- return (react_1.default.createElement(Tab.Screen, { options: {
97
- headerShown: false,
98
- tabBarLabel: title,
99
- tabBarIcon,
100
- tabBarActiveTintColor: "#333",
101
- }, key: name, name: name, component: component }));
102
- }))) })),
103
- stack.map(({ component, name }) => (react_1.default.createElement(Stack.Screen, { options: {
104
- headerShown: false,
105
- }, key: name, name: name, component: component }))))));
85
+ return (react_1.default.createElement(context_1.NavContext.Provider, { value: {
86
+ findOption,
87
+ } },
88
+ react_1.default.createElement(native_1.NavigationContainer, { ref: navigatorRef },
89
+ react_1.default.createElement(Stack.Navigator, { initialRouteName: initial },
90
+ !!tabs && (react_1.default.createElement(Stack.Screen, { key: "tabs", name: tabs.name, options: {
91
+ headerShown: false,
92
+ }, component: () => (react_1.default.createElement(Tab.Navigator, { initialRouteName: (tabs === null || tabs === void 0 ? void 0 : tabs.initial) || (tabs === null || tabs === void 0 ? void 0 : tabs.list[0].name) || "" }, tabs.list.map(ele => {
93
+ const { component, name, meta: { title = "", tabBarIcon } = {}, } = ele;
94
+ return (react_1.default.createElement(Tab.Screen, { options: {
95
+ headerShown: false,
96
+ tabBarLabel: title,
97
+ tabBarIcon,
98
+ tabBarActiveTintColor: "#333",
99
+ }, key: name, name: name, component: component }));
100
+ }))) })),
101
+ stack.map(({ component, name }) => (react_1.default.createElement(Stack.Screen, { options: {
102
+ headerShown: false,
103
+ }, key: name, name: name, component: component })))))));
106
104
  };
107
105
  __exportStar(require("./hooks"), exports);
108
106
  exports.default = NavigatorConnect;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rdyl/react-native-navigator-connect",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./types/index.d.ts",
6
6
  "license": "MIT",
@@ -0,0 +1,5 @@
1
+ export declare const NavContext: import("react").Context<NavContextValue>;
2
+ interface NavContextValue {
3
+ findOption(n: string): FindNavigatorOptionType;
4
+ }
5
+ export {};
package/types/type.d.ts CHANGED
@@ -11,7 +11,6 @@ interface NavigatorOptionMeta {
11
11
 
12
12
  interface NavigatorOption {
13
13
  name: string;
14
- path: string;
15
14
  meta?: NavigatorOptionMeta;
16
15
  isPublic?: boolean;
17
16
  component: React.ComponentType<any>;
@@ -33,5 +32,3 @@ type FindNavigatorOptionType = {
33
32
  type: "tab" | "stack";
34
33
  option: NavigatorOption;
35
34
  } | null;
36
-
37
- declare var $findNavigatorOption: (n: string) => FindNavigatorOptionType;