Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support route wrappers #3189

Merged
merged 5 commits into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions examples/basic-spa/src/components/WrapperPage/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react';

const wrapperPage = (PageComponent) => {
console.log('custom route wrapper')
const WrappedPage = (props) => {
return <PageComponent {...props} />
};
return WrappedPage;
};

export default wrapperPage;
7 changes: 7 additions & 0 deletions examples/basic-spa/src/pages/Dashboard/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { Link, useSearchParams, withSearchParams } from 'ice';
import wrapperPage from '@/components/WrapperPage';

@withSearchParams
class Foo extends React.PureComponent {
Expand Down Expand Up @@ -31,4 +32,10 @@ const Dashboard = (props) => {
);
};


Dashboard.pageConfig = {
title: 'Dashboard Page',
wrappers: [wrapperPage]
};

export default Dashboard;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

跟这种方式 export default wrapperPage(Dashboard) 对比,唯一的优势就是不需要处理 pageConfig

9 changes: 8 additions & 1 deletion packages/plugin-router/src/runtime/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ import { IRouteWrapper, IDynamicImportComponent, RouteItemProps } from '../types
import { IRouterConfig } from '../types';

function wrapperRoute(component, routerWrappers) {
return (routerWrappers || []).reduce((acc, curr) => {
let mergedRouteWrappers = routerWrappers || [];
const wrappers = (component && component.pageConfig && Array.isArray(component.pageConfig.wrappers)) ? component.pageConfig.wrappers : [];
if (wrappers.length) {
mergedRouteWrappers = routerWrappers.concat(wrappers);
}

return (mergedRouteWrappers).reduce((acc, curr) => {
const compose = curr(acc);
if (acc.pageConfig) {
compose.pageConfig = acc.pageConfig;
Expand All @@ -39,6 +45,7 @@ function getRouteComponent(component, routerWrappers?: IRouteWrapper[]) {
function parseRoutes(routes: RouteItemProps[]) {
return routes.map((route) => {
const { children, component, routeWrappers, ...others } = route;

const parsedRoute: IRouterConfig = { ...others };
if (component) {
parsedRoute.component = getRouteComponent(component, children ? [] : routeWrappers);
Expand Down
4 changes: 4 additions & 0 deletions packages/plugin-router/src/types/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@ export interface RouteItemProps extends Omit<DefaultRouteProps, 'component'> {

component?: React.ComponentType<RouteComponentProps<any>> | React.ComponentType<any> | IDynamicImportComponent;

// built-in route wrappers
routeWrappers?: IRouteWrapper[];

// custom route wrappers
wrappers?: IRouteWrapper[];
};