-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support fragments in makeRouteConfig
- Loading branch information
Showing
2 changed files
with
91 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,60 @@ | ||
import React from 'react'; | ||
|
||
/** | ||
* Create a route configuration from JSX configuration elements. | ||
*/ | ||
export default function makeRouteConfig(node) { | ||
return React.Children.toArray(node) | ||
.filter(React.isValidElement) | ||
.map(({ type: Type, props: { children, ...props } }) => { | ||
if (__DEV__) { | ||
if (Type.prototype.constructor !== Type) { | ||
// Unwrap proxies from react-proxy. This isn't strictly necessary. | ||
// eslint-disable-next-line no-param-reassign | ||
Type = Type.prototype.constructor; | ||
} else if ( | ||
// eslint-disable-next-line no-underscore-dangle | ||
Type.__reactstandin__getCurrent | ||
) { | ||
// Unwrap proxies from react-stand-in. | ||
// eslint-disable-next-line no-param-reassign | ||
Type = Object.getPrototypeOf(Type); | ||
} | ||
function buildRouteConfig(node, routeConfig) { | ||
React.Children.forEach(node, child => { | ||
if (!React.isValidElement(child)) { | ||
return; | ||
} | ||
|
||
let Type = child.type; | ||
const { children, ...props } = child.props; | ||
|
||
if (Type === React.Fragment) { | ||
buildRouteConfig(children, routeConfig); | ||
return; | ||
} | ||
|
||
if (__DEV__) { | ||
if (Type.prototype.constructor !== Type) { | ||
// Unwrap proxies from react-proxy. This isn't strictly necessary. | ||
// eslint-disable-next-line no-param-reassign | ||
Type = Type.prototype.constructor; | ||
} else if ( | ||
// eslint-disable-next-line no-underscore-dangle | ||
Type.__reactstandin__getCurrent | ||
) { | ||
// Unwrap proxies from react-stand-in. | ||
// eslint-disable-next-line no-param-reassign | ||
Type = Object.getPrototypeOf(Type); | ||
} | ||
} | ||
|
||
const route = new Type(props); | ||
const route = new Type(props); | ||
|
||
if (children) { | ||
if (React.isValidElement(children) || Array.isArray(children)) { | ||
route.children = makeRouteConfig(children); | ||
} else { | ||
const routeGroups = {}; | ||
Object.entries(children).forEach(([groupName, groupRoutes]) => { | ||
routeGroups[groupName] = makeRouteConfig(groupRoutes); | ||
}); | ||
if (children) { | ||
if (React.isValidElement(children) || Array.isArray(children)) { | ||
// eslint-disable-next-line no-use-before-define | ||
route.children = makeRouteConfig(children); | ||
} else { | ||
const routeGroups = {}; | ||
Object.entries(children).forEach(([groupName, groupRoutes]) => { | ||
// eslint-disable-next-line no-use-before-define | ||
routeGroups[groupName] = makeRouteConfig(groupRoutes); | ||
}); | ||
|
||
route.children = routeGroups; | ||
} | ||
route.children = routeGroups; | ||
} | ||
} | ||
|
||
return route; | ||
}); | ||
routeConfig.push(route); | ||
}); | ||
|
||
return routeConfig; | ||
} | ||
|
||
/** | ||
* Create a route configuration from JSX configuration elements. | ||
*/ | ||
export default function makeRouteConfig(node) { | ||
return buildRouteConfig(node, []); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters