Skip to content

Commit

Permalink
feat(core): 新增router
Browse files Browse the repository at this point in the history
  • Loading branch information
stbui committed Sep 18, 2019
1 parent d3ee537 commit 961629b
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 0 deletions.
29 changes: 29 additions & 0 deletions packages/core/src/router/CreateRouter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React, { createElement } from 'react';
import { Route } from 'react-router';

/**
<CreateRouter basePath="/user" resource="user">
<Create />
</CreateRouter>;
*/

export const CreateRouter = ({ basePath, resource, children }) => {
if (!children) {
return null;
}

return (
<Route
path={`${basePath}/create`}
render={props =>
createElement(children, {
basePath,
...resource,
...props,
})
}
/>
);
};

export default CreateRouter;
30 changes: 30 additions & 0 deletions packages/core/src/router/EditRouter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React, { createElement } from 'react';
import { Route } from 'react-router';

/**
<EditRouter basePath="/user" resource="user">
<Show />
</EditRouter>;
*/

export const EditRouter = ({ basePath, resource, children }) => {
if (!children) {
return null;
}

return (
<Route
path={`${basePath}/:id`}
render={props =>
createElement(children, {
basePath,
id: decodeURIComponent(props.match.params.id),
...resource,
...props,
})
}
/>
);
};

export default EditRouter;
29 changes: 29 additions & 0 deletions packages/core/src/router/ListRouter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React, { createElement } from 'react';
import { Route } from 'react-router';

/**
<ListRouter basePath="/user" resource="user">
<Create />
</ListRouter>;
*/

export const ListRouter = ({ basePath, resource, children }) => {
if (!children) {
return null;
}

return (
<Route
path={basePath}
render={props =>
createElement(children, {
basePath,
...resource,
...props,
})
}
/>
);
};

export default ListRouter;
30 changes: 30 additions & 0 deletions packages/core/src/router/ShowRouter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React, { createElement } from 'react';
import { Route } from 'react-router';

/**
<ShowRouter basePath="/user" resource="user">
<Show />
</ShowRouter>;
*/

export const ShowRouter = ({ basePath, resource, children }) => {
if (!children) {
return null;
}

return (
<Route
path={`${basePath}/:id/show`}
render={props =>
createElement(children, {
basePath,
id: decodeURIComponent(props.match.params.id),
...resource,
...props,
})
}
/>
);
};

export default ShowRouter;
4 changes: 4 additions & 0 deletions packages/core/src/router/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './CreateRouter';
export * from './EditRouter';
export * from './ListRouter';
export * from './ShowRouter';

0 comments on commit 961629b

Please sign in to comment.