Skip to content

Commit

Permalink
fix: DRouter
Browse files Browse the repository at this point in the history
  • Loading branch information
zeromake committed Jan 28, 2019
1 parent 4e8e05e commit edb7405
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zreact-router",
"version": "0.3.0",
"version": "0.3.1",
"description": "a simple router support react,preact,zreact.",
"main": "dist/zreact-router.js",
"module": "dist/zreact-router-esm.js",
Expand Down
24 changes: 20 additions & 4 deletions src/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function Route(props: any) {
function isRoute(child: any): boolean {
const props = findProps(child);
const nodeType = findNodeType(child);
return nodeType === Route || nodeType === Redirect || props.default || props.path;
return nodeType === Route || nodeType === Redirect || (props && (props.default || props.path));
}

export class RouteImpl extends PureComponent<IRouteImplProps, any> {
Expand Down Expand Up @@ -120,7 +120,17 @@ export class RouteImpl extends PureComponent<IRouteImplProps, any> {

function handleRouteChildren(routeChildren) {
basepath = defaultBasePath;
const routes = Children.map(routeChildren, createRoute(basepath));
const createDRoute = createRoute(basepath);
const routes = [];
const newChildren = [];
Children.forEach(routeChildren, (route) => {
if (isRoute(route)) {
routes.push(createDRoute(route));
newChildren.push(null);
} else {
newChildren.push(route);
}
});
const match = pick(routes, pathname);
if (match) {
const {
Expand All @@ -145,13 +155,19 @@ export class RouteImpl extends PureComponent<IRouteImplProps, any> {
},
};
const child = Children.toArray(findChildren(element));
return [cloneElement(
const index = routeChildren.indexOf(element);
const clone = cloneElement(
element,
props,
child.length > 0 ? (
<DRouter primary={primary}>{child}</DRouter>
) : null,
)];
);
if (index !== -1) {
newChildren[index] = clone;
return newChildren.filter((i) => i);
}
return [clone];
}
return null;
}
Expand Down
1 change: 0 additions & 1 deletion src/tools.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
Children,
findChildren,
findProps,
findNodeType,
Expand Down

0 comments on commit edb7405

Please sign in to comment.