-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add support React Router v4 (#82) * Release 1.2.0
- Loading branch information
Showing
8 changed files
with
172 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
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
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { isReactChildren, createRoutesFromReactChildren } from './routeUtils'; | ||
|
||
/** | ||
* @description Creates and returns an array of routes from the given object which | ||
* may be a JSX route, a plain object route, or an array of either. | ||
* @param {Route} routes - React Router configuration. | ||
* @return {Array<String>} | ||
* | ||
* @example | ||
* import { routesCreater as createRoutes } from 'react-router-sitemap'; | ||
* import { routesParser as parseRoutes } from 'react-router-sitemap'; | ||
* | ||
* const routes = createRoutes(<Route path='/home'>); | ||
* const paths = parseRoutes(routes); // ['/home'] | ||
*/ | ||
const createRoutes = routes => { | ||
if (isReactChildren(routes)) { | ||
routes = createRoutesFromReactChildren(routes); | ||
} else if (routes && !Array.isArray(routes)) { | ||
routes = [routes]; | ||
} | ||
|
||
return routes; | ||
}; | ||
|
||
export default createRoutes; |
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import React from 'react'; | ||
|
||
/** | ||
* @description Use React method to test if is a valid React Element. | ||
* @param {Object} object - Which to test if is valid React Element. | ||
* @return {Boolean} | ||
* @ignore | ||
*/ | ||
const isValidChild = object => { | ||
return object === null || React.isValidElement(object); | ||
}; | ||
|
||
/** | ||
* @param {Object|array} | ||
* @return {Boolean} | ||
* @ignore | ||
*/ | ||
const isReactChildren = object => { | ||
return isValidChild(object) || (Array.isArray(object) && object.every(isValidChild)); | ||
}; | ||
|
||
/** | ||
* @description Creates and returns a routes object from the given ReactChildren. JSX | ||
* provides a convenient way to visualize how routes in the hierarchy are | ||
* nested. | ||
* @param {ReactChildren} children - ReactChildren in JSX | ||
* @return {Object} routes object | ||
* @ignore | ||
*/ | ||
const createRoutesFromReactChildren = children => { | ||
const routes = []; | ||
|
||
/** | ||
* @param {Object} element - ReactChild | ||
* @return {Object} route object | ||
* @ignore | ||
*/ | ||
const createRouteFromReactElement = element => { | ||
const type = element.type; | ||
const route = Object.assign({}, type.defaultProps, element.props); | ||
|
||
if (route.children) { | ||
const childRoutes = createRoutesFromReactChildren(route.children, route); | ||
|
||
if (childRoutes.length) { | ||
route.childRoutes = childRoutes; | ||
} | ||
delete route.children; | ||
} | ||
|
||
return route; | ||
}; | ||
|
||
React.Children.forEach(children, function (element) { | ||
if (React.isValidElement(element)) { | ||
// Component classes may have a static create* method. | ||
if (element.type.createRouteFromReactElement) { | ||
const route = element.type.createRouteFromReactElement(element); | ||
|
||
if (route) { | ||
routes.push(route); | ||
} | ||
} else { | ||
routes.push(createRouteFromReactElement(element)); | ||
} | ||
} | ||
}); | ||
|
||
return routes; | ||
}; | ||
|
||
export { | ||
isValidChild, | ||
isReactChildren, | ||
createRoutesFromReactChildren | ||
}; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,6 +1,6 @@ | ||
{ | ||
"name": "react-router-sitemap", | ||
"version": "1.1.5", | ||
"version": "1.2.0", | ||
"description": "Module to generate a sitemap for react-router configuration", | ||
"repository": { | ||
"type": "git", | ||
|
@@ -19,7 +19,7 @@ | |
"test": "karma start ./config/karma.config.js", | ||
"build": "npm run lint && npm run test && webpack --config ./config/build.config.js", | ||
"example": "cd example && node ./sitemap-builder.js", | ||
"documentation": "documentation build ./lib ./lib/routes-parser ./lib/paths-filter ./lib/params-applier ./lib/sitemap-builder -f md > api.md", | ||
"documentation": "documentation build ./lib ./lib/routes-creater ./lib/routes-parser ./lib/paths-filter ./lib/params-applier ./lib/sitemap-builder -f md > api.md", | ||
"prepublish": "npm run build" | ||
}, | ||
"author": "Igor Uvarov <[email protected]> (http://kuflash.ru)", | ||
|
@@ -58,7 +58,7 @@ | |
}, | ||
"peerDependencies": { | ||
"react": "^15.1.0 || ^16.0.0", | ||
"react-router": "^2.3.0 || ^3.2.1" | ||
"react-router": "^2.3.0 || ^3.2.1 || ^4.3.0" | ||
}, | ||
"dependencies": { | ||
"sitemap": "^1.6.0" | ||
|
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