@rdyl/react-native-navigator-connect 0.1.3
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.
- package/README.md +10 -0
- package/dist/hooks.js +48 -0
- package/dist/index.js +130 -0
- package/package.json +36 -0
- package/types/hooks.d.ts +11 -0
- package/types/index.d.ts +9 -0
- package/types/type.d.ts +33 -0
package/README.md
ADDED
package/dist/hooks.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useNavigator = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const native_1 = require("@react-navigation/native");
|
|
6
|
+
const useNavigator = () => {
|
|
7
|
+
const navigation = (0, native_1.useNavigation)();
|
|
8
|
+
const route = (0, native_1.useRoute)();
|
|
9
|
+
const option = (0, react_1.useMemo)(() => {
|
|
10
|
+
// @ts-ignore
|
|
11
|
+
const opt = global === null || global === void 0 ? void 0 : global.$findNavigatorOption(route.name);
|
|
12
|
+
if (opt) {
|
|
13
|
+
return opt.option;
|
|
14
|
+
}
|
|
15
|
+
}, [route.name]);
|
|
16
|
+
const push = (0, react_1.useCallback)((name, params) => {
|
|
17
|
+
navigation.dispatch(native_1.CommonActions.navigate({
|
|
18
|
+
name,
|
|
19
|
+
params,
|
|
20
|
+
}));
|
|
21
|
+
}, [navigation]);
|
|
22
|
+
const back = (0, react_1.useCallback)(() => {
|
|
23
|
+
if (navigation.canGoBack()) {
|
|
24
|
+
navigation.dispatch(native_1.CommonActions.goBack());
|
|
25
|
+
}
|
|
26
|
+
}, [navigation]);
|
|
27
|
+
const redirect = (0, react_1.useCallback)((name, params) => {
|
|
28
|
+
// @ts-ignore
|
|
29
|
+
const opt = global.$findNavigatorOption(name);
|
|
30
|
+
if (opt) {
|
|
31
|
+
if (opt.type === "tab") {
|
|
32
|
+
navigation.dispatch(native_1.TabActions.jumpTo(name, params));
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
navigation.dispatch(native_1.StackActions.replace(name, params));
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}, [navigation]);
|
|
39
|
+
const setParams = (0, react_1.useCallback)((params = {}) => {
|
|
40
|
+
navigation.dispatch(Object.assign(Object.assign({}, native_1.CommonActions.setParams(params)), { source: route.key }));
|
|
41
|
+
}, [navigation, route]);
|
|
42
|
+
return Object.assign(Object.assign({}, route), { option,
|
|
43
|
+
push,
|
|
44
|
+
back,
|
|
45
|
+
redirect,
|
|
46
|
+
setParams });
|
|
47
|
+
};
|
|
48
|
+
exports.useNavigator = useNavigator;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
36
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
const react_1 = __importStar(require("react"));
|
|
40
|
+
const native_1 = require("@react-navigation/native");
|
|
41
|
+
const stack_1 = require("@react-navigation/stack");
|
|
42
|
+
const bottom_tabs_1 = require("@react-navigation/bottom-tabs");
|
|
43
|
+
const Tab = (0, bottom_tabs_1.createBottomTabNavigator)();
|
|
44
|
+
const Stack = (0, stack_1.createStackNavigator)();
|
|
45
|
+
const NavigatorConnect = ({ afterEach, stack = {}, tabs = {}, }) => {
|
|
46
|
+
const navigatorRef = (0, native_1.useNavigationContainerRef)();
|
|
47
|
+
const [routeName, setRouteName] = (0, react_1.useState)("");
|
|
48
|
+
const findNavigatorOption = (0, react_1.useCallback)((name) => {
|
|
49
|
+
const tab = ((tabs === null || tabs === void 0 ? void 0 : tabs.items) || []).find(ele => ele.name === name);
|
|
50
|
+
if (tab) {
|
|
51
|
+
return {
|
|
52
|
+
type: "tab",
|
|
53
|
+
option: tab,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
const item = ((stack === null || stack === void 0 ? void 0 : stack.items) || []).find(ele => ele.name === name);
|
|
57
|
+
if (item) {
|
|
58
|
+
return {
|
|
59
|
+
type: "stack",
|
|
60
|
+
option: item,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
return null;
|
|
64
|
+
}, []);
|
|
65
|
+
const option = (0, react_1.useMemo)(() => {
|
|
66
|
+
const opt = findNavigatorOption(routeName);
|
|
67
|
+
if (opt) {
|
|
68
|
+
return opt.option;
|
|
69
|
+
}
|
|
70
|
+
}, [findNavigatorOption, routeName]);
|
|
71
|
+
const initialTabName = (0, react_1.useMemo)(() => {
|
|
72
|
+
const { initial, items = [] } = tabs;
|
|
73
|
+
if (initial) {
|
|
74
|
+
return initial;
|
|
75
|
+
}
|
|
76
|
+
if (items[0]) {
|
|
77
|
+
return items[0].name;
|
|
78
|
+
}
|
|
79
|
+
}, [tabs]);
|
|
80
|
+
const initialStackName = (0, react_1.useMemo)(() => {
|
|
81
|
+
const { initial, items = [] } = stack;
|
|
82
|
+
if (initial) {
|
|
83
|
+
return initial;
|
|
84
|
+
}
|
|
85
|
+
if (items[0]) {
|
|
86
|
+
return items[0].name;
|
|
87
|
+
}
|
|
88
|
+
}, [tabs]);
|
|
89
|
+
const TabsNavigator = (0, react_1.useMemo)(() => {
|
|
90
|
+
return () => (react_1.default.createElement(Tab.Navigator, { initialRouteName: initialTabName }, ((tabs === null || tabs === void 0 ? void 0 : tabs.items) || []).map(ele => {
|
|
91
|
+
const { component, name, meta: { title = "", tabBarIcon } = {}, } = ele;
|
|
92
|
+
return (react_1.default.createElement(Tab.Screen, { options: {
|
|
93
|
+
headerShown: false,
|
|
94
|
+
tabBarLabel: title,
|
|
95
|
+
tabBarIcon,
|
|
96
|
+
tabBarActiveTintColor: "#333",
|
|
97
|
+
}, key: name, name: name, component: component }));
|
|
98
|
+
})));
|
|
99
|
+
}, [tabs]);
|
|
100
|
+
(0, react_1.useEffect)(() => {
|
|
101
|
+
if (option && afterEach) {
|
|
102
|
+
afterEach(option);
|
|
103
|
+
}
|
|
104
|
+
}, [afterEach, option]);
|
|
105
|
+
(0, react_1.useEffect)(() => {
|
|
106
|
+
const callback = ({ data }) => {
|
|
107
|
+
var _a;
|
|
108
|
+
if (data.action.payload) {
|
|
109
|
+
const { name } = (_a = data.action) === null || _a === void 0 ? void 0 : _a.payload;
|
|
110
|
+
setRouteName(name);
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
navigatorRef.addListener("__unsafe_action__", callback);
|
|
114
|
+
return () => navigatorRef.removeListener("__unsafe_action__", callback);
|
|
115
|
+
}, [navigatorRef.isReady]);
|
|
116
|
+
(0, react_1.useEffect)(() => {
|
|
117
|
+
// @ts-ignore
|
|
118
|
+
global.$findNavigatorOption = findNavigatorOption;
|
|
119
|
+
}, [findNavigatorOption]);
|
|
120
|
+
return (react_1.default.createElement(native_1.NavigationContainer, { ref: navigatorRef },
|
|
121
|
+
react_1.default.createElement(Stack.Navigator, { initialRouteName: initialStackName },
|
|
122
|
+
react_1.default.createElement(Stack.Screen, { key: "tabs", name: "tabs", options: {
|
|
123
|
+
headerShown: false,
|
|
124
|
+
}, component: TabsNavigator }),
|
|
125
|
+
((stack === null || stack === void 0 ? void 0 : stack.items) || []).map(({ component, name }) => (react_1.default.createElement(Stack.Screen, { options: {
|
|
126
|
+
headerShown: false,
|
|
127
|
+
}, key: name, name: name, component: component }))))));
|
|
128
|
+
};
|
|
129
|
+
__exportStar(require("./hooks"), exports);
|
|
130
|
+
exports.default = NavigatorConnect;
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rdyl/react-native-navigator-connect",
|
|
3
|
+
"version": "0.1.3",
|
|
4
|
+
"main": "./dist/index.js",
|
|
5
|
+
"types": "./types/index.d.ts",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist",
|
|
12
|
+
"types",
|
|
13
|
+
"README.md"
|
|
14
|
+
],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsc && copyfiles -u 1 src/**/*.d.ts types"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@types/node": "^22.10.1",
|
|
20
|
+
"@types/react": "^18.3.12",
|
|
21
|
+
"@types/react-native": "^0.73.0",
|
|
22
|
+
"copyfiles": "^2.4.1",
|
|
23
|
+
"react": "^18.3.1",
|
|
24
|
+
"typescript": "^5.6.3"
|
|
25
|
+
},
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"react": ">=18.0.0",
|
|
28
|
+
"react-native": ">=0.73.0"
|
|
29
|
+
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@react-navigation/bottom-tabs": "^7.0.3",
|
|
33
|
+
"@react-navigation/native": "^7.0.2",
|
|
34
|
+
"@react-navigation/stack": "^7.0.2"
|
|
35
|
+
}
|
|
36
|
+
}
|
package/types/hooks.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const useNavigator: () => {
|
|
2
|
+
option: any;
|
|
3
|
+
push: (name: string, params?: QueryParams) => void;
|
|
4
|
+
back: () => void;
|
|
5
|
+
redirect: (name: string, params?: QueryParams) => void;
|
|
6
|
+
setParams: (params?: QueryParams) => void;
|
|
7
|
+
key: string;
|
|
8
|
+
name: string;
|
|
9
|
+
path?: string;
|
|
10
|
+
params?: Readonly<object | undefined>;
|
|
11
|
+
};
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { FC } from "react";
|
|
2
|
+
declare const NavigatorConnect: FC<NavigatorConnectProps>;
|
|
3
|
+
export * from "./hooks";
|
|
4
|
+
export interface NavigatorConnectProps {
|
|
5
|
+
stack?: NavigatorStack;
|
|
6
|
+
tabs?: NavigatorTabs;
|
|
7
|
+
afterEach?(to: NavigatorOption): void;
|
|
8
|
+
}
|
|
9
|
+
export default NavigatorConnect;
|
package/types/type.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** @Navigator */
|
|
2
|
+
interface NavigatorOptionMeta {
|
|
3
|
+
title?: string;
|
|
4
|
+
tabBarIcon?: (props: {
|
|
5
|
+
focused: boolean;
|
|
6
|
+
color: string;
|
|
7
|
+
size: number;
|
|
8
|
+
}) => React.ReactNode;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface NavigatorOption {
|
|
12
|
+
name: string;
|
|
13
|
+
path: string;
|
|
14
|
+
meta?: NavigatorOptionMeta;
|
|
15
|
+
isPublic?: boolean;
|
|
16
|
+
component: React.ComponentType<any>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface NavigatorStack {
|
|
20
|
+
items?: NavigatorOption[];
|
|
21
|
+
initial?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
interface NavigatorTabs extends NavigatorStack {
|
|
25
|
+
name?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
type FindNavigatorOptionType = {
|
|
29
|
+
type: "tab" | "stack";
|
|
30
|
+
option: NavigatorOption;
|
|
31
|
+
} | null;
|
|
32
|
+
|
|
33
|
+
type QueryParams = Record<string, unknown>;
|