Skip to content

Commit adc0a2f

Browse files
committed
[added] IndexRoute
1 parent 2f0b8bf commit adc0a2f

File tree

4 files changed

+85
-8
lines changed

4 files changed

+85
-8
lines changed

modules/IndexRoute.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import React from 'react';
2+
import invariant from 'invariant';
3+
import { createRouteFromReactElement } from './RouteUtils';
4+
import { component, components, falsy } from './PropTypes';
5+
6+
var { bool, func } = React.PropTypes;
7+
8+
/**
9+
* An <IndexRoute> is used to specify its parent's <Route indexRoute> in
10+
* a JSX route config.
11+
*/
12+
var IndexRoute = React.createClass({
13+
14+
statics: {
15+
16+
createRouteFromReactElement(element, parentRoute) {
17+
if (parentRoute) {
18+
parentRoute.indexRoute = createRouteFromReactElement(element);
19+
} else {
20+
warning(
21+
false,
22+
'An <IndexRoute> does not make sense at the root of your route config'
23+
);
24+
}
25+
}
26+
27+
},
28+
29+
propTypes: {
30+
path: falsy,
31+
ignoreScrollBehavior: bool,
32+
component,
33+
components,
34+
getComponents: func
35+
},
36+
37+
render() {
38+
invariant(
39+
false,
40+
'<IndexRoute> elements are for router configuration only and should not be rendered'
41+
);
42+
}
43+
44+
});
45+
46+
export default IndexRoute;

modules/RouteUtils.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,23 @@ function checkPropTypes(componentName, propTypes, props) {
2222
}
2323
}
2424

25+
function createRoute(defaultProps, props) {
26+
return { ...defaultProps, ...props };
27+
}
28+
2529
export function createRouteFromReactElement(element) {
2630
var type = element.type;
27-
var route = Object.assign({}, type.defaultProps, element.props);
31+
var route = createRoute(type.defaultProps, element.props);
2832

2933
if (type.propTypes)
3034
checkPropTypes(type.displayName || type.name, type.propTypes, route);
3135

3236
if (route.children) {
33-
route.childRoutes = createRoutesFromReactChildren(route.children);
37+
var childRoutes = createRoutesFromReactChildren(route.children, route);
38+
39+
if (childRoutes.length)
40+
route.childRoutes = childRoutes;
41+
3442
delete route.children;
3543
}
3644

@@ -54,14 +62,17 @@ export function createRouteFromReactElement(element) {
5462
* Note: This method is automatically used when you provide <Route> children
5563
* to a <Router> component.
5664
*/
57-
export function createRoutesFromReactChildren(children) {
65+
export function createRoutesFromReactChildren(children, parentRoute) {
5866
var routes = [];
5967

6068
React.Children.forEach(children, function (element) {
6169
if (React.isValidElement(element)) {
6270
// Component classes may have a static create* method.
6371
if (element.type.createRouteFromReactElement) {
64-
routes.push(element.type.createRouteFromReactElement(element));
72+
var route = element.type.createRouteFromReactElement(element, parentRoute);
73+
74+
if (route)
75+
routes.push(route);
6576
} else {
6677
routes.push(createRouteFromReactElement(element));
6778
}

modules/__tests__/createRoutesFromReactChildren-test.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import expect from 'expect';
22
import React from 'react';
33
import { createRoutesFromReactChildren } from '../RouteUtils';
4+
import IndexRoute from '../IndexRoute';
45
import Route from '../Route';
56

67
describe('createRoutesFromReactChildren', function () {
@@ -28,10 +29,28 @@ describe('createRoutesFromReactChildren', function () {
2829
}
2930
});
3031

32+
it('works with index routes', function () {
33+
var routes = createRoutesFromReactChildren(
34+
<Route path="/" component={Parent}>
35+
<IndexRoute component={Hello} />
36+
</Route>
37+
);
38+
39+
expect(routes).toEqual([
40+
{
41+
path: '/',
42+
component: Parent,
43+
indexRoute: {
44+
component: Hello
45+
}
46+
}
47+
]);
48+
});
49+
3150
it('works with nested routes', function () {
3251
var routes = createRoutesFromReactChildren(
3352
<Route component={Parent}>
34-
<Route path="home" components={{ hello: Hello, goodbye: Goodbye }}/>
53+
<Route path="home" components={{ hello: Hello, goodbye: Goodbye }} />
3554
</Route>
3655
);
3756

@@ -50,9 +69,9 @@ describe('createRoutesFromReactChildren', function () {
5069

5170
it('works with falsy children', function () {
5271
var routes = createRoutesFromReactChildren([
53-
<Route path="/one" component={Parent}/>,
72+
<Route path="/one" component={Parent} />,
5473
null,
55-
<Route path="/two" component={Parent}/>,
74+
<Route path="/two" component={Parent} />,
5675
undefined
5776
]);
5877

@@ -71,7 +90,7 @@ describe('createRoutesFromReactChildren', function () {
7190
var routes = createRoutesFromReactChildren(
7291
<Route path="/one" component={Parent}>
7392
// This is a comment.
74-
<Route path="/two" component={Hello}/>
93+
<Route path="/two" component={Hello} />
7594
</Route>
7695
);
7796

modules/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export Router from './Router';
33
export Link from './Link';
44

55
/* components (configuration) */
6+
export IndexRoute from './IndexRoute';
67
export Redirect from './Redirect';
78
export Route from './Route';
89

0 commit comments

Comments
 (0)