Skip to content

Commit 8ff4783

Browse files
authored
Functional Link component (#1079)
* Make Link component functional * Clean up unused plugin
1 parent 60f8549 commit 8ff4783

File tree

2 files changed

+34
-52
lines changed

2 files changed

+34
-52
lines changed

.babelrc

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
{
22
"presets": ["@babel/preset-env", "@babel/preset-react"],
3-
"plugins": ["@babel/plugin-transform-runtime"],
4-
"env": {
5-
"test": {
6-
"plugins": ["@babel/plugin-transform-class-properties"]
7-
}
8-
}
3+
"plugins": ["@babel/plugin-transform-runtime"]
94
}

src/private/Link.js

Lines changed: 33 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -29,69 +29,56 @@ function isExternalLink(external_link, href) {
2929
return external_link;
3030
}
3131

32-
class Link extends Component {
33-
/**
34-
* This component can be used either as a dash-core-components style link or
35-
* as a styled HTML anchor
36-
*/
37-
constructor(props) {
38-
super(props);
39-
this.updateLocation = this.updateLocation.bind(this);
40-
}
41-
42-
updateLocation(e) {
32+
function Link({
33+
children,
34+
preOnClick,
35+
target,
36+
linkTarget,
37+
href,
38+
download,
39+
external_link,
40+
disabled,
41+
...otherProps
42+
}) {
43+
const updateLocation = e => {
4344
const hasModifiers = e.metaKey || e.shiftKey || e.altKey || e.ctrlKey;
4445
if (hasModifiers) {
4546
return;
4647
}
47-
if (this.props.disabled) {
48+
if (disabled) {
4849
e.preventDefault();
4950
return;
5051
}
51-
if (this.props.preOnClick) {
52-
this.props.preOnClick();
52+
if (preOnClick) {
53+
preOnClick();
5354
}
54-
const {external_link, href} = this.props;
5555
if (href && !isExternalLink(external_link, href)) {
5656
// prevent anchor from updating location
5757
e.preventDefault();
58-
const {href} = this.props;
5958
window.history.pushState({}, '', href);
6059
window.dispatchEvent(new CustomEvent('_dashprivate_pushstate'));
6160
// scroll back to top
6261
window.scrollTo(0, 0);
6362
}
64-
}
63+
};
6564

66-
render() {
67-
const {
68-
children,
69-
preOnClick,
70-
target,
71-
linkTarget,
72-
href,
73-
download,
74-
external_link,
75-
...otherProps
76-
} = this.props;
77-
const linkIsExternal = href && isExternalLink(external_link, href);
78-
/**
79-
* ideally, we would use cloneElement however
80-
* that doesn't work with dash's recursive
81-
* renderTree implementation for some reason
82-
*/
83-
return (
84-
<a
85-
href={href}
86-
target={linkIsExternal ? target || linkTarget : undefined}
87-
download={download && linkIsExternal ? download : undefined}
88-
{...otherProps}
89-
onClick={e => this.updateLocation(e)}
90-
>
91-
{children}
92-
</a>
93-
);
94-
}
65+
const linkIsExternal = href && isExternalLink(external_link, href);
66+
/**
67+
* ideally, we would use cloneElement however
68+
* that doesn't work with dash's recursive
69+
* renderTree implementation for some reason
70+
*/
71+
return (
72+
<a
73+
href={href}
74+
target={linkIsExternal ? target || linkTarget : undefined}
75+
download={download && linkIsExternal ? download : undefined}
76+
{...otherProps}
77+
onClick={e => updateLocation(e)}
78+
>
79+
{children}
80+
</a>
81+
);
9582
}
9683

9784
Link.propTypes = {

0 commit comments

Comments
 (0)