Skip to content

Commit 178d42e

Browse files
committed
multi grouping issue fixed
1 parent b76355e commit 178d42e

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/package/components/appRouter.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import ErrorComponent from "./error";
1010
import LayoutComponent from "./layout";
1111
import NotFoundComponent from "./notFound";
1212
import PageWithLoader from "./pageWithLoader";
13+
import { replaceGroupPaths } from "../utils/helper";
1314

1415
type Module = { default: React.FC };
1516
const basePath = "/src/app";
@@ -38,14 +39,11 @@ const LoadingComponent =
3839
const recursiveRoutes = (
3940
routePath: string[],
4041
acc: RouteObject[],
41-
Component: Module,
42+
Component: Module
4243
) => {
4344
let path = routePath[0] === "index" ? "/" : routePath[0];
4445
path = path === "app" ? "/" : path;
45-
path = path
46-
.replace(/\:\.\.\.(\w+)/, ":$1/*")
47-
.replace(/\[(.*)\]/, "$1/*")
48-
.replace(/\(.*\)/, "");
46+
path = path.replace(/\:\.\.\.(\w+)/, ":$1/*").replace(/\[(.*)\]/, "$1/*");
4947

5048
let matchedIndex = acc.findIndex((r: RouteObject) => r.path === path);
5149
if (matchedIndex === -1) {
@@ -72,7 +70,7 @@ const recursiveRoutes = (
7270
recursiveRoutes(
7371
routePath.slice(1),
7472
acc[matchedIndex].children as RouteObject[],
75-
Component,
73+
Component
7674
);
7775
} else {
7876
const RouterComponent = () => (
@@ -111,7 +109,7 @@ const allRoutes = Object.entries(routes).reduce(
111109
recursiveRoutes(routePath, acc, Component);
112110
return acc;
113111
},
114-
[],
112+
[]
115113
);
116114

117115
const catchAllRoute = {
@@ -129,7 +127,7 @@ export const useAppRouter = () => {
129127
}
130128
}
131129
});
132-
return allRoutes satisfies RouteObject[];
130+
return replaceGroupPaths(allRoutes) satisfies RouteObject[];
133131
};
134132

135133
export const AppRouter = ({ router = createBrowserRouter }) => {

src/package/utils/helper.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { RouteObject } from "react-router-dom";
2+
3+
export const replaceGroupPaths = (routes: RouteObject[]) => {
4+
return routes.map((route) => {
5+
if (route.path && /^\(.+\)$/.test(route.path)) {
6+
route.path = "";
7+
}
8+
if (route.children) {
9+
route.children = replaceGroupPaths(route.children);
10+
}
11+
12+
return route;
13+
});
14+
};

0 commit comments

Comments
 (0)