2017-05-03 02:04:16 +02:00
|
|
|
import React from 'react';
|
2017-04-21 20:05:35 +02:00
|
|
|
import PropTypes from 'prop-types';
|
2016-12-02 15:05:50 +01:00
|
|
|
|
2017-06-23 19:36:54 +02:00
|
|
|
export default class Permalink extends React.PureComponent {
|
2016-12-02 15:05:50 +01:00
|
|
|
|
2017-05-12 14:44:10 +02:00
|
|
|
static contextTypes = {
|
2017-05-20 17:31:47 +02:00
|
|
|
router: PropTypes.object,
|
2017-05-12 14:44:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
className: PropTypes.string,
|
|
|
|
href: PropTypes.string.isRequired,
|
|
|
|
to: PropTypes.string.isRequired,
|
2017-05-20 17:31:47 +02:00
|
|
|
children: PropTypes.node,
|
2018-03-08 08:57:21 +01:00
|
|
|
onInterceptClick: PropTypes.func,
|
2017-05-12 14:44:10 +02:00
|
|
|
};
|
|
|
|
|
2018-03-08 08:57:21 +01:00
|
|
|
handleClick = e => {
|
|
|
|
if (this.props.onInterceptClick && this.props.onInterceptClick()) {
|
|
|
|
e.preventDefault();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-11 15:27:59 +02:00
|
|
|
if (this.context.router && e.button === 0 && !(e.ctrlKey || e.metaKey)) {
|
2016-12-02 15:05:50 +01:00
|
|
|
e.preventDefault();
|
2017-06-20 20:40:03 +02:00
|
|
|
this.context.router.history.push(this.props.to);
|
2016-12-02 15:05:50 +01:00
|
|
|
}
|
2017-04-21 20:05:35 +02:00
|
|
|
}
|
2016-12-02 15:05:50 +01:00
|
|
|
|
|
|
|
render () {
|
2018-03-08 08:57:21 +01:00
|
|
|
const { href, children, className, onInterceptClick, ...other } = this.props;
|
2016-12-02 15:05:50 +01:00
|
|
|
|
2017-05-03 02:04:16 +02:00
|
|
|
return (
|
2017-07-11 15:27:59 +02:00
|
|
|
<a target='_blank' href={href} onClick={this.handleClick} {...other} className={`permalink${className ? ' ' + className : ''}`}>
|
2017-05-03 02:04:16 +02:00
|
|
|
{children}
|
|
|
|
</a>
|
|
|
|
);
|
2016-12-02 15:05:50 +01:00
|
|
|
}
|
|
|
|
|
2017-04-21 20:05:35 +02:00
|
|
|
}
|