Skip to content
This repository was archived by the owner on May 23, 2018. It is now read-only.

Commit 76662e7

Browse files
committed
feat: make BaseLink grab the router instance from context
1 parent 076433a commit 76662e7

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

modules/BaseLink.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ class BaseLink extends Component {
44
constructor(props, context) {
55
super(props, context);
66

7+
this.router = context.router;
78
this.isActive = this.isActive.bind(this);
89
this.clickHandler = this.clickHandler.bind(this);
910

1011
this.state = { active: this.isActive() };
1112
}
1213

1314
isActive() {
14-
return this.props.router.isActive(this.props.routeName, this.props.routeParams);
15+
return this.router.isActive(this.props.routeName, this.props.routeParams);
1516
}
1617

1718
clickHandler(evt) {
@@ -23,19 +24,19 @@ class BaseLink extends Component {
2324
}
2425
}
2526

26-
let comboKey = evt.metaKey || evt.altKey || evt.ctrlKey || evt.shiftKey;
27+
const comboKey = evt.metaKey || evt.altKey || evt.ctrlKey || evt.shiftKey;
2728

2829
if (evt.button === 0 && !comboKey) {
2930
evt.preventDefault();
30-
this.props.router.navigate(this.props.routeName, this.props.routeParams, this.props.routeOptions);
31+
this.router.navigate(this.props.routeName, this.props.routeParams, this.props.routeOptions);
3132
}
3233
}
3334

3435
render() {
35-
const { router, routeName, routeParams, className, activeClassName, children } = this.props;
36+
const { routeName, routeParams, className, activeClassName, children } = this.props;
3637

3738
const active = this.isActive();
38-
const href = router.buildUrl(routeName, routeParams);
39+
const href = this.router.buildUrl(routeName, routeParams);
3940
const linkclassName = (className ? className.split(' ') : [])
4041
.concat(active ? [activeClassName] : []).join(' ');
4142

@@ -45,9 +46,11 @@ class BaseLink extends Component {
4546
}
4647
}
4748

49+
BaseLink.contextTypes = {
50+
router: PropTypes.object.isRequired
51+
};
52+
4853
BaseLink.propTypes = {
49-
// route: PropTypes.object.isRequired,
50-
router: PropTypes.object.isRequired,
5154
routeName: PropTypes.string.isRequired,
5255
routeParams: PropTypes.object,
5356
routeOptions: PropTypes.object,

test/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ describe('BaseLink component', () => {
6868

6969
it('should render an hyperlink element', () => {
7070
router.addNode('home', '/home');
71-
const output = mount(<BaseLink router={ router } routeName={ 'home' } />);
71+
const output = mount(<RouterProvider router={ router }><BaseLink routeName={ 'home' } /></RouterProvider>);
7272
expect(output.find('a')).to.have.attr('href', '/home')
7373
expect(output.find('a')).not.to.have.className('active');
7474
});
7575

7676
it('should have an active class if associated route is active', () => {
7777
router.setOption('defaultRoute', 'home');
7878
router.start();
79-
const output = mount(<BaseLink router={ router } routeName={ 'home' } />);
79+
const output = mount(<RouterProvider router={ router }><BaseLink routeName={ 'home' } /></RouterProvider>);
8080
expect(output.find('a')).to.have.className('active');
8181
});
8282
});

0 commit comments

Comments
 (0)