Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 57 additions & 51 deletions src/private/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,57 +29,63 @@ function isExternalLink(external_link, href) {
return external_link;
}

function Link({
children,
preOnClick,
target,
linkTarget,
href,
download,
external_link,
disabled,
...otherProps
}) {
const updateLocation = e => {
const hasModifiers = e.metaKey || e.shiftKey || e.altKey || e.ctrlKey;
if (hasModifiers) {
return;
}
if (disabled) {
e.preventDefault();
return;
}
if (preOnClick) {
preOnClick();
}
if (href && !isExternalLink(external_link, href)) {
// prevent anchor from updating location
e.preventDefault();
window.history.pushState({}, '', href);
window.dispatchEvent(new CustomEvent('_dashprivate_pushstate'));
// scroll back to top
window.scrollTo(0, 0);
}
};

const linkIsExternal = href && isExternalLink(external_link, href);
/**
* ideally, we would use cloneElement however
* that doesn't work with dash's recursive
* renderTree implementation for some reason
*/
return (
<a
href={href}
target={linkIsExternal ? target || linkTarget : undefined}
download={download && linkIsExternal ? download : undefined}
{...otherProps}
onClick={e => updateLocation(e)}
>
{children}
</a>
);
}
const Link = React.forwardRef(
(
{
children,
preOnClick,
target,
linkTarget,
href,
download,
external_link,
disabled,
...otherProps
},
ref
) => {
const updateLocation = e => {
const hasModifiers = e.metaKey || e.shiftKey || e.altKey || e.ctrlKey;
if (hasModifiers) {
return;
}
if (disabled) {
e.preventDefault();
return;
}
if (preOnClick) {
preOnClick();
}
if (href && !isExternalLink(external_link, href)) {
// prevent anchor from updating location
e.preventDefault();
window.history.pushState({}, '', href);
window.dispatchEvent(new CustomEvent('_dashprivate_pushstate'));
// scroll back to top
window.scrollTo(0, 0);
}
};

const linkIsExternal = href && isExternalLink(external_link, href);
/**
* ideally, we would use cloneElement however
* that doesn't work with dash's recursive
* renderTree implementation for some reason
*/
return (
<a
href={href}
target={linkIsExternal ? target || linkTarget : undefined}
download={download && linkIsExternal ? download : undefined}
{...otherProps}
onClick={e => updateLocation(e)}
ref={ref}
>
{children}
</a>
);
}
);

Link.propTypes = {
/**
Expand Down
14 changes: 12 additions & 2 deletions src/private/__tests__/Link.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ describe('Link', () => {
const user = userEvent.setup();
const outerOnClick = jest.fn();
const link = render(
<div onClick={e => outerOnClick(e.defaultPrevented)}>
<div
onClick={e => {
outerOnClick(e.defaultPrevented);
e.preventDefault();
}}
>
<Link href="https://external.com">Clickable</Link>
</div>
);
Expand All @@ -86,7 +91,12 @@ describe('Link', () => {
const user = userEvent.setup();
const outerOnClick = jest.fn();
const link = render(
<div onClick={e => outerOnClick(e.defaultPrevented)}>
<div
onClick={e => {
outerOnClick(e.defaultPrevented);
e.preventDefault();
}}
>
<Link href="/external" external_link>
Clickable
</Link>
Expand Down