From 76662e78a20b79356a699e0f9c766315b7376245 Mon Sep 17 00:00:00 2001 From: Thomas Roch Date: Sun, 28 Feb 2016 13:55:42 +0000 Subject: [PATCH] feat: make BaseLink grab the router instance from context --- modules/BaseLink.js | 17 ++++++++++------- test/main.js | 4 ++-- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/modules/BaseLink.js b/modules/BaseLink.js index bcb8c11..d280832 100644 --- a/modules/BaseLink.js +++ b/modules/BaseLink.js @@ -4,6 +4,7 @@ class BaseLink extends Component { constructor(props, context) { super(props, context); + this.router = context.router; this.isActive = this.isActive.bind(this); this.clickHandler = this.clickHandler.bind(this); @@ -11,7 +12,7 @@ class BaseLink extends Component { } isActive() { - return this.props.router.isActive(this.props.routeName, this.props.routeParams); + return this.router.isActive(this.props.routeName, this.props.routeParams); } clickHandler(evt) { @@ -23,19 +24,19 @@ class BaseLink extends Component { } } - let comboKey = evt.metaKey || evt.altKey || evt.ctrlKey || evt.shiftKey; + const comboKey = evt.metaKey || evt.altKey || evt.ctrlKey || evt.shiftKey; if (evt.button === 0 && !comboKey) { evt.preventDefault(); - this.props.router.navigate(this.props.routeName, this.props.routeParams, this.props.routeOptions); + this.router.navigate(this.props.routeName, this.props.routeParams, this.props.routeOptions); } } render() { - const { router, routeName, routeParams, className, activeClassName, children } = this.props; + const { routeName, routeParams, className, activeClassName, children } = this.props; const active = this.isActive(); - const href = router.buildUrl(routeName, routeParams); + const href = this.router.buildUrl(routeName, routeParams); const linkclassName = (className ? className.split(' ') : []) .concat(active ? [activeClassName] : []).join(' '); @@ -45,9 +46,11 @@ class BaseLink extends Component { } } +BaseLink.contextTypes = { + router: PropTypes.object.isRequired +}; + BaseLink.propTypes = { - // route: PropTypes.object.isRequired, - router: PropTypes.object.isRequired, routeName: PropTypes.string.isRequired, routeParams: PropTypes.object, routeOptions: PropTypes.object, diff --git a/test/main.js b/test/main.js index 605ec1d..5df31f0 100644 --- a/test/main.js +++ b/test/main.js @@ -68,7 +68,7 @@ describe('BaseLink component', () => { it('should render an hyperlink element', () => { router.addNode('home', '/home'); - const output = mount(); + const output = mount(); expect(output.find('a')).to.have.attr('href', '/home') expect(output.find('a')).not.to.have.className('active'); }); @@ -76,7 +76,7 @@ describe('BaseLink component', () => { it('should have an active class if associated route is active', () => { router.setOption('defaultRoute', 'home'); router.start(); - const output = mount(); + const output = mount(); expect(output.find('a')).to.have.className('active'); }); });