Skip to content

Commit 19c7086

Browse files
committed
[added] Handle undefined query values in isActive
1 parent 265ca88 commit 19c7086

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

docs/API.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,11 +537,11 @@ Stringifies the query into the pathname, using the router's config.
537537
Creates a URL, using the router's config. For example, it will add `#/` in front of the `pathname` for hash history.
538538

539539
##### `isActive(pathname, query, indexOnly)`
540-
Returns `true` or `false` depending on if the current path is active. Will be true for every route in the route branch matched by the `pathname` (child route is active, therefore parent is too).
540+
Returns `true` or `false` depending on if the current path is active. Will be true for every route in the route branch matched by the `pathname` (child route is active, therefore parent is too), unless `indexOnly` is specified, in which case it will only match the exact path.
541541

542542
###### arguments
543543
- `pathname` - the full URL with or without the query.
544-
- `query` - an object that will be stringified by the router.
544+
- `query` - if specified, an object containing key/value pairs that must be active in the current query - explicit `undefined` values require the corresponding key to be missing or `undefined` in the current query
545545
- `indexOnly` - a boolean (default: `false`).
546546

547547
#### Examples

modules/__tests__/isActive-test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,5 +270,31 @@ describe('isActive', function () {
270270
})
271271
})
272272
})
273+
274+
describe('with a query with explicit undefined values', function () {
275+
it('matches missing query keys', function (done) {
276+
render((
277+
<Router history={createHistory('/home?foo=1')}>
278+
<Route path="/" />
279+
<Route path="/home" />
280+
</Router>
281+
), node, function () {
282+
expect(this.history.isActive('/home', { foo: 1, bar: undefined })).toBe(true)
283+
done()
284+
})
285+
})
286+
287+
it('does not match a present query key', function (done) {
288+
render((
289+
<Router history={createHistory('/home?foo=1&bar=')}>
290+
<Route path="/" />
291+
<Route path="/home" />
292+
</Router>
293+
), node, function () {
294+
expect(this.history.isActive('/home', { foo: 1, bar: undefined })).toBe(false)
295+
done()
296+
})
297+
})
298+
})
273299
})
274300
})

modules/isActive.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,21 @@ function deepEqual(a, b) {
1414
}
1515

1616
if (typeof a === 'object') {
17-
for (let p in a)
18-
if (a.hasOwnProperty(p) && (!b.hasOwnProperty(p) || !deepEqual(a[p], b[p])))
17+
for (let p in a) {
18+
if (!a.hasOwnProperty(p)) {
19+
continue
20+
}
21+
22+
if (a[p] === undefined) {
23+
if (b[p] !== undefined) {
24+
return false
25+
}
26+
} else if (!b.hasOwnProperty(p)) {
1927
return false
28+
} else if (!deepEqual(a[p], b[p])) {
29+
return false
30+
}
31+
}
2032

2133
return true
2234
}

0 commit comments

Comments
 (0)