Skip to content

Commit 33317f2

Browse files
committed
feat: 完成 * 的params 以数组形式返回
1 parent 02ea5e6 commit 33317f2

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

src/features/router/useRoute.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,34 @@ function usePrevious<T>(value: T): T | null {
1212
return ref.current;
1313
}
1414

15+
function getCatchAllParam(str: string | undefined) {
16+
if (!str) return null;
17+
// \[\.\.\.(\w+)\] 用来匹配形如 [...slug]
18+
// (\w+) 意味着捕获“字母、数字或下划线”组成的部分
19+
const match = str.match(/\[\.\.\.(\w+)\]/);
20+
return match ? match[1] : null;
21+
}
22+
23+
function getParams(
24+
params: Record<string, string> | undefined,
25+
id: string
26+
): Record<string, string | string[]> | undefined {
27+
if (!params?.['*']) return params;
28+
29+
const lastName = id.split('_').at(-1);
30+
const catchAllParam = getCatchAllParam(lastName);
31+
if (catchAllParam) {
32+
return { [catchAllParam]: params['*'].split('/') };
33+
}
34+
return params;
35+
}
36+
1537
/** - get route meta */
16-
export function useRoute<T = unknown, Q extends Record<string, string> = Record<string, string>>() {
38+
export function useRoute<
39+
T = unknown,
40+
Q extends Record<string, string> | null = Record<string, string>,
41+
P extends Record<string, string | string[]> = Record<string, string | string[]>
42+
>() {
1743
const matches = useMatches();
1844

1945
const routes = matches.at(-1) as Router.Route<T>;
@@ -32,11 +58,12 @@ export function useRoute<T = unknown, Q extends Record<string, string> = Record<
3258
fullPath,
3359
hash,
3460
matched: matches.slice(1) as Router.Route<T>[],
61+
params: getParams(routes.params as Record<string, string>, routes.id) as P,
3562
pathname,
3663
query,
3764
redirect: null,
3865
search
39-
} as Router.Route<T, Q>;
66+
} as Router.Route<T, Q, P>;
4067

4168
const previousRoute = usePrevious(route);
4269

src/types/app.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,10 @@ declare namespace App {
614614
refreshToken: string;
615615
tokenExpired: string;
616616
};
617-
route: Record<I18nRouteKey, string>;
617+
route: Record<I18nRouteKey, string> & {
618+
notFound: string;
619+
root: string;
620+
};
618621
system: {
619622
errorReason: string;
620623
reload: string;

src/types/elegant-router.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ declare module "@soybean-react/vite-plugin-react-router" {
4343
"(base)_home": "/home";
4444
"(base)_manage": "/manage";
4545
"(base)_manage_role": "/manage/role";
46+
"(base)_manage_role_[...slug]": "/manage/role/*";
4647
"(base)_manage_user": "/manage/user";
4748
"(base)_manage_user_[id]": "/manage/user/:id";
4849
"(base)_multi-menu": "/multi-menu";
@@ -146,6 +147,7 @@ declare module "@soybean-react/vite-plugin-react-router" {
146147
| "(base)_home"
147148
| "(base)"
148149
| "(base)_manage"
150+
| "(base)_manage_role_[...slug]"
149151
| "(base)_manage_role"
150152
| "(base)_manage_user_[id]"
151153
| "(base)_manage_user"

src/types/router.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,16 @@ declare namespace Router {
7474
url?: string | null;
7575
};
7676

77-
type Route<T = unknown, Q extends Record<string, string> = Record<string, string>> = UIMatch<T, RouteHandle> & {
77+
type Route<
78+
T = unknown,
79+
Q extends Record<string, string> | null = Record<string, string>,
80+
P extends Record<string, string | string[]> = Record<string, string | string[]>
81+
> = Omit<UIMatch<T, RouteHandle>, 'params'> & {
7882
error: Error | null;
7983
fullPath: string;
8084
hash: string;
8185
matched: UIMatch<T, RouteHandle>[];
86+
params: P;
8287
pathname: string;
8388
query: Q;
8489
redirect: Route<T, Q> | null;

0 commit comments

Comments
 (0)