react-ark-tools
Version:
Boilerplate and tooling for JavaScript application development with React
57 lines (44 loc) • 1.3 kB
JavaScript
/**
* React App SDK (https://github.com/kriasoft/react-app)
*
* Copyright © 2015-present Kriasoft, LLC. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE.txt file in the root directory of this source tree.
*/
import React, { PropTypes } from 'react';
import history from '../../core/history';
class Link extends React.Component {
static propTypes = {
to: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).isRequired,
onClick: PropTypes.func,
};
handleClick = (event) => {
if (this.props.onClick) {
this.props.onClick(event);
}
if (event.button !== 0 /* left click */) {
return;
}
if (event.metaKey || event.altKey || event.ctrlKey || event.shiftKey) {
return;
}
if (event.defaultPrevented === true) {
return;
}
event.preventDefault();
if (this.props.to) {
history.push(this.props.to);
} else {
history.push({
pathname: event.currentTarget.pathname,
search: event.currentTarget.search,
});
}
};
render() {
const { to, ...props } = this.props; // eslint-disable-line no-use-before-define
return <a href={history.createHref(to)} {...props} onClick={this.handleClick} />;
}
}
export default Link;