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