Convert Permalink to TSX

virtualized-window
Justin 3 years ago
parent f0a9ed8ad4
commit 95a5ca2115

@ -1,41 +0,0 @@
import PropTypes from 'prop-types';
import React from 'react';
import { withRouter } from 'react-router-dom';
export default @withRouter
class Permalink extends React.PureComponent {
static propTypes = {
className: PropTypes.string,
href: PropTypes.string.isRequired,
to: PropTypes.string.isRequired,
children: PropTypes.node,
onInterceptClick: PropTypes.func,
history: PropTypes.object,
title: PropTypes.string,
dangerouslySetInnerHTML: PropTypes.object,
};
handleClick = e => {
if (this.props.onInterceptClick && this.props.onInterceptClick()) {
e.preventDefault();
return;
}
if (this.props.history && e.button === 0 && !(e.ctrlKey || e.metaKey)) {
e.preventDefault();
this.props.history.push(this.props.to);
}
}
render() {
const { href, children, className, title, dangerouslySetInnerHTML } = this.props;
return (
<a target='_blank' href={href} onClick={this.handleClick} dangerouslySetInnerHTML={dangerouslySetInnerHTML} title={title} className={`permalink${className ? ' ' + className : ''}`}>
{children}
</a>
);
}
}

@ -0,0 +1,37 @@
import * as React from 'react';
import { useHistory } from 'react-router-dom';
interface IPermaLink extends Pick<React.HTMLAttributes<HTMLAnchorElement>, 'dangerouslySetInnerHTML'> {
className?: string,
href: string,
title?: string,
to: string,
}
const Permalink: React.FC<IPermaLink> = (props) => {
const history = useHistory();
const { className, href, title, to, children, ...filteredProps } = props;
const handleClick = (event: React.MouseEvent<HTMLAnchorElement>) => {
if (event.button === 0 && !(event.ctrlKey || event.metaKey)) {
event.preventDefault();
history.push(to);
}
};
return (
<a
target='_blank'
href={href}
onClick={handleClick}
title={title}
className={`permalink${className ? ' ' + className : ''}`}
{...filteredProps}
>
{children}
</a>
);
};
export default Permalink;
Loading…
Cancel
Save