Created
October 19, 2017 11:10
-
-
Save randomnerd/ca4eff1c87e309645ff1e4f41f056a40 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, { Component } from 'react'; | |
| import { inject } from 'mobx-react'; | |
| @inject('router', 'common') | |
| export default class Link extends Component { | |
| render() { | |
| const { router, common, activeClassName, className, onClick, children, to, ...otherProps } = this.props; | |
| otherProps.href = router.createHref({ pathname: to }); | |
| let cls = className || ''; | |
| if (router.location.pathname === to && activeClassName) cls += (cls ? cls + ' ' : '') + activeClassName; | |
| otherProps.onClick = function navigate(e) { | |
| if (e.button !== 0 || e.ctrlKey || e.altKey || e.metaKey || e.shiftKey) { | |
| return; | |
| } | |
| e.preventDefault(); | |
| if (typeof onClick === 'function') { | |
| onClick(e); | |
| } | |
| router.push(to); | |
| window.scrollTo(0, 0); | |
| }; | |
| return <a children={children} className={cls} {...otherProps} />; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment