Skip to content

Commit 26c632a

Browse files
committed
Merge pull request #2425 from taion/dynamic-indexRoute
isActive for dynamic index routes
2 parents fbc109c + 31b1a79 commit 26c632a

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

modules/__tests__/isActive-test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,45 @@ describe('isActive', function () {
297297
})
298298
})
299299
})
300+
301+
describe('dynamic routes', function () {
302+
const routes = {
303+
path: '/',
304+
childRoutes: [
305+
{ path: 'foo' }
306+
],
307+
getIndexRoute(location, callback) {
308+
setTimeout(() => callback(null, {}))
309+
}
310+
}
311+
312+
describe('when not on index route', function () {
313+
it('does not show index as active', function (done) {
314+
render((
315+
<Router history={createHistory('/foo')} routes={routes} />
316+
), node, function () {
317+
expect(this.history.isActive('/')).toBe(true)
318+
expect(this.history.isActive('/', null, true)).toBe(false)
319+
expect(this.history.isActive('/foo')).toBe(true)
320+
done()
321+
})
322+
})
323+
})
324+
325+
describe('when on index route', function () {
326+
it('shows index as active', function (done) {
327+
render((
328+
<Router history={createHistory('/')} routes={routes} />
329+
), node, function () {
330+
// Need to wait for async match to complete.
331+
setTimeout(() => {
332+
expect(this.history.isActive('/')).toBe(true)
333+
expect(this.history.isActive('/', null, true)).toBe(true)
334+
expect(this.history.isActive('/foo')).toBe(false)
335+
done()
336+
})
337+
})
338+
})
339+
})
340+
})
300341
})

modules/isActive.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,12 @@ function getMatchingRoute(pathname, activeRoutes, activeParams) {
6666
* Returns true if the given pathname matches the active routes
6767
* and params.
6868
*/
69-
function routeIsActive(pathname, activeRoutes, activeParams, indexOnly) {
70-
let route = getMatchingRoute(pathname, activeRoutes, activeParams)
71-
72-
if (route == null)
73-
return false
74-
75-
if (indexOnly)
76-
return activeRoutes.length > 1 && activeRoutes[activeRoutes.length - 1] === route.indexRoute
69+
function routeIsActive(pathname, location, routes, params, indexOnly) {
70+
if (indexOnly) {
71+
return location.pathname.replace(/\/*$/) === pathname.replace(/\/*$/)
72+
}
7773

78-
return true
74+
return getMatchingRoute(pathname, routes, params) != null
7975
}
8076

8177
/**
@@ -100,7 +96,7 @@ function isActive(pathname, query, indexOnly, location, routes, params) {
10096
if (location == null)
10197
return false
10298

103-
if (!routeIsActive(pathname, routes, params, indexOnly))
99+
if (!routeIsActive(pathname, location, routes, params, indexOnly))
104100
return false
105101

106102
return queryIsActive(query, location.query)

0 commit comments

Comments
 (0)