Link.js (1241B)
1 /** 2 * React Starter Kit (https://www.reactstarterkit.com/) 3 * 4 * Copyright © 2014-present Kriasoft, LLC. All rights reserved. 5 * 6 * This source code is licensed under the MIT license found in the 7 * LICENSE.txt file in the root directory of this source tree. 8 */ 9 10 import React, { PropTypes } from 'react'; 11 import history from '../../core/history'; 12 13 function isLeftClickEvent(event) { 14 return event.button === 0; 15 } 16 17 function isModifiedEvent(event) { 18 return !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey); 19 } 20 21 class Link extends React.Component { 22 static propTypes = { 23 to: PropTypes.string.isRequired, 24 children: PropTypes.node.isRequired, 25 onClick: PropTypes.func, 26 }; 27 28 static defaultProps = { 29 onClick: null, 30 }; 31 32 handleClick = (event) => { 33 if (this.props.onClick) { 34 this.props.onClick(event); 35 } 36 37 if (isModifiedEvent(event) || !isLeftClickEvent(event)) { 38 return; 39 } 40 41 if (event.defaultPrevented === true) { 42 return; 43 } 44 45 event.preventDefault(); 46 history.push(this.props.to); 47 }; 48 49 render() { 50 const { to, children, ...props } = this.props; 51 return <a href={to} {...props} onClick={this.handleClick}>{children}</a>; 52 } 53 } 54 55 export default Link;