-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dimitri Kopriwa
committed
Jan 14, 2019
1 parent
f89d570
commit 4ca5007
Showing
15 changed files
with
422 additions
and
53 deletions.
There are no files selected for viewing
Empty file.
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 +1,11 @@ | ||
Refer to the utility documentation in question for configuration step. | ||
The react-router `<Route />` components expect a few props described [here](https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/api/Route.md). | ||
|
||
It is more appropriate to store route configuration as an array than an object but it is more appropriate to target path in an object. | ||
|
||
Your **route configuration** `routes.js` must be of type `Array` and can include `childRoutes` of type `Array`. | ||
|
||
It will return an object construct of `{ [route.name]: route }`. | ||
|
||
You can use [getPages](#get-pages) utility to have the `routeList` configuration as an object that can be traversed. | ||
|
||
|
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,69 @@ | ||
<!-- Generated by documentation.js. Update this documentation by updating the source code. --> | ||
|
||
## getPages | ||
|
||
Looping through an array and find an entry require filtering. Find recursively a (`childRoutes`) won't look nice in your code. | ||
|
||
it turn your array of `routes` into a `pages` object that can be used to access any `path`. | ||
|
||
### Parameters | ||
|
||
- `routeConfig` **[Array][1]** a list of route configuration | ||
- `default` **[object][2]** pages - an object pages to expand (optional, default `{}`) | ||
|
||
### Examples | ||
|
||
```javascript | ||
`childRoutes` are flattened in parent `[parent][route.name]` and are kept in `[parent].childRoutes` for faster accessibility. | ||
|
||
// This is how you would access a specific page in your routes configuration array: | ||
|
||
const routes = [ | ||
{ | ||
name: 'dashboard', | ||
redirect: true, | ||
from: '/dashboard', | ||
to: '/', | ||
description: 'Dashboard', | ||
childRoutes: [ | ||
{ | ||
name: 'users', | ||
path: '/users', | ||
description: 'List users', | ||
}, | ||
], | ||
} | ||
]; | ||
const page = routes.filter((route) => route.name === 'dashboard')[0] | ||
|
||
// This is how you do with `getPages`: | ||
|
||
const { getPages } = require('@yeutech-lab/react-router-dom-utils'); | ||
const routes = [ | ||
{ | ||
name: 'dashboard', | ||
redirect: true, | ||
from: '/dashboard', | ||
to: '/', | ||
description: 'Dashboard', | ||
childRoutes: [ | ||
{ | ||
name: 'users', | ||
path: '/users', | ||
description: 'List users', | ||
}, | ||
], | ||
} | ||
]; | ||
const pages = getPages(routes); | ||
<ul> | ||
<li>{pages.dashboard.description}: {pages.dashboard.from}</li> | ||
<li>{pages.dashboard.users.description}: {pages.dashboard.users.path}</li> | ||
</ul> | ||
``` | ||
|
||
Returns **[object][2]** pages - a pages object | ||
|
||
[1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array | ||
|
||
[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object |
This file was deleted.
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<!-- Generated by documentation.js. Update this documentation by updating the source code. --> | ||
|
||
## makeRoutes | ||
|
||
This utility will check for routes configurations | ||
|
||
### Parameters | ||
|
||
- `routesConfig` **[Array][1]** list of route configuration | ||
|
||
### Examples | ||
|
||
```javascript | ||
const routeList = makeRoutes([{ name: 'home', path: '/home', component: HomePage }]) | ||
// return [<Route name="home" path="/home" component={HomePage} />] | ||
``` | ||
|
||
Returns **[Array][1]** routeList - list of <Route /> and <Redirect /> | ||
|
||
[1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array |
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
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,74 @@ | ||
/** | ||
* @public | ||
* @name getPages | ||
* @description | ||
* | ||
* Looping through an array and find an entry require filtering. Find recursively a (`childRoutes`) won't look nice in your code. | ||
* | ||
* it turn your array of `routes` into a `pages` object that can be used to access any `path`. | ||
* | ||
* @param {Array} routeConfig - a list of route configuration | ||
* @param {object} [default={}] pages - an object pages to expand | ||
* @return {object} pages - a pages object | ||
* @example | ||
* `childRoutes` are flattened in parent `[parent][route.name]` and are kept in `[parent].childRoutes` for faster accessibility. | ||
* | ||
* // This is how you would access a specific page in your routes configuration array: | ||
* | ||
* const routes = [ | ||
* { | ||
* name: 'dashboard', | ||
* redirect: true, | ||
* from: '/dashboard', | ||
* to: '/', | ||
* description: 'Dashboard', | ||
* childRoutes: [ | ||
* { | ||
* name: 'users', | ||
* path: '/users', | ||
* description: 'List users', | ||
* }, | ||
* ], | ||
* } | ||
* ]; | ||
* const page = routes.filter((route) => route.name === 'dashboard')[0] | ||
* | ||
* // This is how you do with `getPages`: | ||
* | ||
* const { getPages } = require('@yeutech-lab/react-router-dom-utils'); | ||
* const routes = [ | ||
* { | ||
* name: 'dashboard', | ||
* redirect: true, | ||
* from: '/dashboard', | ||
* to: '/', | ||
* description: 'Dashboard', | ||
* childRoutes: [ | ||
* { | ||
* name: 'users', | ||
* path: '/users', | ||
* description: 'List users', | ||
* }, | ||
* ], | ||
* } | ||
* ]; | ||
* const pages = getPages(routes); | ||
* <ul> | ||
* <li>{pages.dashboard.description}: {pages.dashboard.from}</li> | ||
* <li>{pages.dashboard.users.description}: {pages.dashboard.users.path}</li> | ||
* </ul> | ||
* | ||
* | ||
*/ | ||
export default function getPages(routeConfig, pages = {}) { | ||
const copy = [...routeConfig]; | ||
function addRoutes(routes, parent) { | ||
const innerCopy = [...routes]; | ||
innerCopy.forEach((route) => { | ||
parent[route.name] = route; // eslint-disable-line no-param-reassign | ||
route.childRoutes && addRoutes(route.childRoutes, route); // eslint-disable-line no-unused-expressions | ||
}); | ||
} | ||
addRoutes(copy, pages); | ||
return pages; | ||
} |
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 +1,3 @@ | ||
export { default as Routes } from './Routes'; | ||
export { default as makeRoutes } from './makeRoutes'; | ||
export { default as getPages } from './getPages'; |
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,31 @@ | ||
import { Redirect, Route } from 'react-router-dom'; | ||
import React from 'react'; | ||
|
||
/** | ||
* @public | ||
* @description This utility will check for routes configurations | ||
* @param {Array} routesConfig - list of route configuration | ||
* @returns {Array} routeList - list of <Route /> and <Redirect /> | ||
* @example | ||
* const routeList = makeRoutes([{ name: 'home', path: '/home', component: HomePage }]) | ||
* // return [<Route name="home" path="/home" component={HomePage} />] | ||
*/ | ||
export default function makeRoutes(routesConfig) { | ||
const routeList = []; | ||
function recursive(routes) { | ||
routes.forEach((route) => { | ||
if (routeList.filter((l) => l.key === route.name).length === 0) { | ||
if (route.from) { | ||
routeList.push(<Redirect key={route.name} {...route} />); | ||
} else { | ||
routeList.push(<Route key={route.name} {...route} />); | ||
} | ||
if (route.childRoutes) { | ||
recursive(route.childRoutes); | ||
} | ||
} | ||
}); | ||
} | ||
recursive(routesConfig); | ||
return routeList; | ||
} |
Oops, something went wrong.