Skip to content
This repository was archived by the owner on Sep 29, 2023. It is now read-only.

Commit 2a72f15

Browse files
committed
Check that params match for link to be active
1 parent cf95178 commit 2a72f15

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
},
3535
"license": "MIT",
3636
"dependencies": {
37+
"deep-equal": "^1.0.1",
3738
"history": "^4.6.1",
3839
"hoist-non-react-statics": "^1.2.0",
3940
"path-to-regexp": "^1.7.0"

src/components/Link/__tests__/testLink.js

+23-5
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,19 @@ describe('<Link>', () => {
1313
context = {
1414
router: {
1515
linkMiddleware: [],
16-
location: {},
16+
location: {
17+
pathname: '/u/x/'
18+
},
1719
urls: {
1820
makeLocation({urlName, params, query}) {
1921
expect(urlName).toEqual('user:profile');
2022
expect(params).toEqual({username: 'x'});
2123
expect(query).toEqual({a: 'b'});
2224
return {pathname: "/u/x/", search: '?a=b'};
2325
},
24-
match() {
25-
return {};
26+
match(pathname, urlName) {
27+
expect(urlName).toEqual('user:profile');
28+
return {params: {username: pathname.split("/")[2]}};
2629
}
2730
},
2831
navigate,
@@ -74,11 +77,26 @@ describe('<Link>', () => {
7477
});
7578

7679
it('should have active styles when appropriate', () => {
77-
const wrapper = mount(
80+
context.router.location.pathname = '/u/a/';
81+
let wrapper = mount(
7882
<Link activeClassName="another" className="testclass" urlName="user:profile" params={{username: 'x'}} query={{a: 'b'}} />,
7983
{context}
8084
);
81-
const a = wrapper.find('a');
85+
let a = wrapper.find('a');
86+
expect(a.length).toEqual(1);
87+
expect(a.props()).toEqual({
88+
children: undefined,
89+
href: "/u/x/?a=b",
90+
onClick: jasmine.any(Function),
91+
className: 'testclass'
92+
});
93+
94+
context.router.location.pathname = '/u/x/';
95+
wrapper = mount(
96+
<Link activeClassName="another" className="testclass" urlName="user:profile" params={{username: 'x'}} query={{a: 'b'}} />,
97+
{context}
98+
);
99+
a = wrapper.find('a');
82100
expect(a.length).toEqual(1);
83101
expect(a.props()).toEqual({
84102
children: undefined,

src/components/Link/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* @flow */
22
import React from 'react';
3+
import deepEqual from 'deep-equal';
34
import withRouter from '../decorators/withRouter';
45
import {RouterContextPropType} from '../Router';
56
import type Urls from '../../urls';
@@ -83,7 +84,7 @@ class Link extends React.PureComponent {
8384
};
8485

8586
isActive = () => {
86-
const {urls, location, urlName, exact, strict, isActive} = this.props;
87+
const {urls, location, urlName, params, exact, strict, isActive} = this.props;
8788

8889
if (!urlName) {
8990
return false;
@@ -95,6 +96,9 @@ class Link extends React.PureComponent {
9596
return isActive(match, location);
9697
}
9798

99+
if (match && params) {
100+
return deepEqual(match.params, params);
101+
}
98102
return !!match;
99103
};
100104

yarn.lock

+4
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,10 @@ decamelize@^1.0.0, decamelize@^1.1.1:
12231223
version "1.2.0"
12241224
resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290"
12251225

1226+
deep-equal@^1.0.1:
1227+
version "1.0.1"
1228+
resolved "https://registry.yarnpkg.com/deep-equal/-/deep-equal-1.0.1.tgz#f5d260292b660e084eff4cdbc9f08ad3247448b5"
1229+
12261230
deep-extend@~0.4.0:
12271231
version "0.4.2"
12281232
resolved "https://registry.yarnpkg.com/deep-extend/-/deep-extend-0.4.2.tgz#48b699c27e334bf89f10892be432f6e4c7d34a7f"

0 commit comments

Comments
 (0)