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

Commit

Permalink
feat: make BaseLink grab the router instance from context
Browse files Browse the repository at this point in the history
  • Loading branch information
troch committed Feb 28, 2016
1 parent 076433a commit 76662e7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
17 changes: 10 additions & 7 deletions modules/BaseLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ 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);

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

isActive() {
return this.props.router.isActive(this.props.routeName, this.props.routeParams);
return this.router.isActive(this.props.routeName, this.props.routeParams);
}

clickHandler(evt) {
Expand All @@ -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(' ');

Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ describe('BaseLink component', () => {

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

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

0 comments on commit 76662e7

Please sign in to comment.