Skip to content

Commit 7fc7cd5

Browse files
authored
Fix warnings in test logs (#1083)
* Wrap Link in forwardRef * Intercept navigation clicks
1 parent 5bf1eaa commit 7fc7cd5

File tree

2 files changed

+69
-53
lines changed

2 files changed

+69
-53
lines changed

src/private/Link.js

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

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 => {
44-
const hasModifiers = e.metaKey || e.shiftKey || e.altKey || e.ctrlKey;
45-
if (hasModifiers) {
46-
return;
47-
}
48-
if (disabled) {
49-
e.preventDefault();
50-
return;
51-
}
52-
if (preOnClick) {
53-
preOnClick();
54-
}
55-
if (href && !isExternalLink(external_link, href)) {
56-
// prevent anchor from updating location
57-
e.preventDefault();
58-
window.history.pushState({}, '', href);
59-
window.dispatchEvent(new CustomEvent('_dashprivate_pushstate'));
60-
// scroll back to top
61-
window.scrollTo(0, 0);
62-
}
63-
};
64-
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-
);
82-
}
32+
const Link = React.forwardRef(
33+
(
34+
{
35+
children,
36+
preOnClick,
37+
target,
38+
linkTarget,
39+
href,
40+
download,
41+
external_link,
42+
disabled,
43+
...otherProps
44+
},
45+
ref
46+
) => {
47+
const updateLocation = e => {
48+
const hasModifiers = e.metaKey || e.shiftKey || e.altKey || e.ctrlKey;
49+
if (hasModifiers) {
50+
return;
51+
}
52+
if (disabled) {
53+
e.preventDefault();
54+
return;
55+
}
56+
if (preOnClick) {
57+
preOnClick();
58+
}
59+
if (href && !isExternalLink(external_link, href)) {
60+
// prevent anchor from updating location
61+
e.preventDefault();
62+
window.history.pushState({}, '', href);
63+
window.dispatchEvent(new CustomEvent('_dashprivate_pushstate'));
64+
// scroll back to top
65+
window.scrollTo(0, 0);
66+
}
67+
};
68+
69+
const linkIsExternal = href && isExternalLink(external_link, href);
70+
/**
71+
* ideally, we would use cloneElement however
72+
* that doesn't work with dash's recursive
73+
* renderTree implementation for some reason
74+
*/
75+
return (
76+
<a
77+
href={href}
78+
target={linkIsExternal ? target || linkTarget : undefined}
79+
download={download && linkIsExternal ? download : undefined}
80+
{...otherProps}
81+
onClick={e => updateLocation(e)}
82+
ref={ref}
83+
>
84+
{children}
85+
</a>
86+
);
87+
}
88+
);
8389

8490
Link.propTypes = {
8591
/**

src/private/__tests__/Link.test.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@ describe('Link', () => {
7070
const user = userEvent.setup();
7171
const outerOnClick = jest.fn();
7272
const link = render(
73-
<div onClick={e => outerOnClick(e.defaultPrevented)}>
73+
<div
74+
onClick={e => {
75+
outerOnClick(e.defaultPrevented);
76+
e.preventDefault();
77+
}}
78+
>
7479
<Link href="https://external.com">Clickable</Link>
7580
</div>
7681
);
@@ -86,7 +91,12 @@ describe('Link', () => {
8691
const user = userEvent.setup();
8792
const outerOnClick = jest.fn();
8893
const link = render(
89-
<div onClick={e => outerOnClick(e.defaultPrevented)}>
94+
<div
95+
onClick={e => {
96+
outerOnClick(e.defaultPrevented);
97+
e.preventDefault();
98+
}}
99+
>
90100
<Link href="/external" external_link>
91101
Clickable
92102
</Link>

0 commit comments

Comments
 (0)