router-kit 1.3.0 → 1.3.1
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/dist/context/RouterProvider.js +27 -27
- package/package.json +1 -1
|
@@ -17,18 +17,6 @@ const RouterProvider = ({ routes }) => {
|
|
|
17
17
|
const [path, setPath] = useState("");
|
|
18
18
|
const [fullPathWithParams, setFullPathWithParams] = useState("");
|
|
19
19
|
let page404 = null;
|
|
20
|
-
const paths = routes.flatMap((route) => {
|
|
21
|
-
const collectPaths = (r, parentPath = "/") => {
|
|
22
|
-
const fullPath = join(parentPath, `/${r.path}`);
|
|
23
|
-
const currentPaths = [fullPath];
|
|
24
|
-
if (r.children) {
|
|
25
|
-
const childPaths = r.children.flatMap((child) => collectPaths(child, fullPath));
|
|
26
|
-
return [...currentPaths, ...childPaths];
|
|
27
|
-
}
|
|
28
|
-
return currentPaths;
|
|
29
|
-
};
|
|
30
|
-
return collectPaths(route);
|
|
31
|
-
});
|
|
32
20
|
useEffect(() => {
|
|
33
21
|
setPath(window.location.pathname);
|
|
34
22
|
const patchHistory = (method) => {
|
|
@@ -57,22 +45,7 @@ const RouterProvider = ({ routes }) => {
|
|
|
57
45
|
}, []);
|
|
58
46
|
const pathValidation = (routeFullPath, currentPath) => {
|
|
59
47
|
const routePaths = routeFullPath.split("|");
|
|
60
|
-
const staticPaths = [];
|
|
61
|
-
const dynamicPaths = [];
|
|
62
48
|
for (const routePath of routePaths) {
|
|
63
|
-
if (routePath.includes(":")) {
|
|
64
|
-
dynamicPaths.push(routePath);
|
|
65
|
-
}
|
|
66
|
-
else {
|
|
67
|
-
staticPaths.push(routePath);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
for (const routePath of staticPaths) {
|
|
71
|
-
if (routePath === currentPath) {
|
|
72
|
-
return routePath;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
for (const routePath of dynamicPaths) {
|
|
76
49
|
const routeParts = routePath.split("/").filter(Boolean);
|
|
77
50
|
const pathParts = currentPath.split("/").filter(Boolean);
|
|
78
51
|
if (routeParts.length !== pathParts.length)
|
|
@@ -94,12 +67,39 @@ const RouterProvider = ({ routes }) => {
|
|
|
94
67
|
return false;
|
|
95
68
|
};
|
|
96
69
|
const getComponent = (routesList, currentPath, parentPath = "/") => {
|
|
70
|
+
const staticRoutes = [];
|
|
71
|
+
const dynamicRoutes = [];
|
|
97
72
|
for (const route of routesList) {
|
|
98
73
|
const is404 = route.path === "404" || route.path === "/404";
|
|
99
74
|
if (is404) {
|
|
100
75
|
page404 = route.component;
|
|
101
76
|
continue;
|
|
102
77
|
}
|
|
78
|
+
const pathArray = Array.isArray(route.path) ? route.path : [route.path];
|
|
79
|
+
const hasDynamicParams = pathArray.some((p) => p.includes(":"));
|
|
80
|
+
if (hasDynamicParams) {
|
|
81
|
+
dynamicRoutes.push(route);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
staticRoutes.push(route);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
for (const route of staticRoutes) {
|
|
88
|
+
const fullPath = join(parentPath, `/${route.path}`);
|
|
89
|
+
const matchedPath = pathValidation(fullPath, currentPath);
|
|
90
|
+
if (matchedPath) {
|
|
91
|
+
if (matchedPath !== fullPathWithParams) {
|
|
92
|
+
setFullPathWithParams(matchedPath);
|
|
93
|
+
}
|
|
94
|
+
return route.component;
|
|
95
|
+
}
|
|
96
|
+
if (route.children) {
|
|
97
|
+
const childMatch = getComponent(route.children, currentPath, fullPath);
|
|
98
|
+
if (childMatch)
|
|
99
|
+
return childMatch;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
for (const route of dynamicRoutes) {
|
|
103
103
|
const fullPath = join(parentPath, `/${route.path}`);
|
|
104
104
|
const matchedPath = pathValidation(fullPath, currentPath);
|
|
105
105
|
if (matchedPath) {
|
package/package.json
CHANGED