Skip to content

Commit 6e64304

Browse files
committed
make IndexRoute work within nested path-less routes
1 parent 45a9869 commit 6e64304

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

modules/__tests__/IndexRoute-test.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,41 @@ describe('An <IndexRoute>', function () {
6060
done()
6161
})
6262
})
63-
})
6463

64+
it('renders when its parents combined pathes match', function (done) {
65+
render((
66+
<Router history={createHistory('/path/test')}>
67+
<Route path="/path" component={Parent}>
68+
<IndexRoute component={Child}/>
69+
<Route path="test" component={Parent}>
70+
<IndexRoute component={Child}/>
71+
</Route>
72+
</Route>
73+
</Router>
74+
), node, function () {
75+
expect(node.textContent).toEqual('parent parent child')
76+
done()
77+
})
78+
})
79+
80+
it('renders when its parents combined pathes match, and its direct parent is path less', function (done) {
81+
render((
82+
<Router history={createHistory('/')}>
83+
<Route path="/" component={Parent}>
84+
<Route component={Parent}>
85+
<Route component={Parent}>
86+
<Route component={Parent}>
87+
<Route path="deep" component={Parent}/>
88+
<IndexRoute component={Child}/>
89+
</Route>
90+
</Route>
91+
</Route>
92+
</Route>
93+
</Router>
94+
), node, function () {
95+
expect(node.textContent).toEqual('parent parent parent parent child')
96+
done()
97+
})
98+
})
99+
})
65100
})

modules/matchRoutes.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@ function getIndexRoute(route, location, callback) {
2121
route.getIndexRoute(location, function (error, indexRoute) {
2222
callback(error, !error && createRoutes(indexRoute)[0])
2323
})
24+
} else if (route.childRoutes) {
25+
const pathless = route.childRoutes.filter(function (obj) {
26+
return !obj.hasOwnProperty('path')
27+
})
28+
29+
loopAsync(pathless.length, function (index, next, done) {
30+
getIndexRoute(pathless[index], location, function (error, indexRoute) {
31+
if (error || indexRoute) {
32+
const routes = [ pathless[index] ].concat( Array.isArray(indexRoute) ? indexRoute : [ indexRoute ] )
33+
done(error, routes)
34+
} else {
35+
next()
36+
}
37+
})
38+
}, function (err, routes) {
39+
callback(null, routes)
40+
})
2441
} else {
2542
callback()
2643
}
@@ -65,7 +82,9 @@ function matchRouteDeep(basename, route, location, callback) {
6582
if (error) {
6683
callback(error)
6784
} else {
68-
if (indexRoute)
85+
if (Array.isArray(indexRoute))
86+
match.routes.push(...indexRoute)
87+
else if (indexRoute)
6988
match.routes.push(indexRoute)
7089

7190
callback(null, match)

0 commit comments

Comments
 (0)