Skip to content

Commit

Permalink
feat(platform): route's data support pass params
Browse files Browse the repository at this point in the history
  • Loading branch information
xiejay97 committed Jan 10, 2023
1 parent 3e8b93c commit d04f94f
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions packages/platform/src/app/Routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const RouteStateContext = React.createContext<RouteStateContextData>({
export type CanActivateFn = (route: RouteItem) => true | React.ReactElement;

export interface RouteData {
title?: string | ((params: any) => string);
title?: string;
acl?:
| {
control: Control | Control[];
Expand All @@ -68,6 +68,15 @@ export interface NonIndexRouteItem extends Omit<NonIndexRouteObject, 'children'>
}
export type RouteItem = IndexRouteItem | NonIndexRouteItem;

export interface IndexRouteItemInput extends IndexRouteObject {
data?: RouteData | ((params: any) => RouteData);
}
export interface NonIndexRouteItemInput extends Omit<NonIndexRouteObject, 'children'> {
children?: NonIndexRouteItemInput[];
data?: RouteData | ((params: any) => RouteData);
}
export type RouteItemInput = IndexRouteItemInput | NonIndexRouteItemInput;

// I have a great implementation of route caching, but considering the synchronization of data between pages (like modifying list or detail page data), I ended up not introducing route caching.
export const AppRoutes = React.memo(() => {
const ACLGuard = useACLGuard();
Expand Down Expand Up @@ -184,17 +193,24 @@ export const AppRoutes = React.memo(() => {
{
path: '/exception/:status',
element: <AppExceptionRoute />,
data: {
title: (params) => params.status,
},
data: (params) => ({
title: params.status,
}),
},
{
path: '*',
element: <Navigate to="/exception/404" replace />,
},
] as RouteItem[],
] as RouteItemInput[],
location
);
) as any as RouteMatch<string, RouteItem>[] | null;
if (matches) {
matches.forEach((matche) => {
if (isFunction(matche.route.data)) {
matche.route.data = matche.route.data(matche.params);
}
});
}

const element: React.ReactNode = (() => {
if (!matches) {
Expand Down

0 comments on commit d04f94f

Please sign in to comment.