Skip to content

Commit 6c02d4a

Browse files
committed
Fix of Link and NavLink
Fix for #25
1 parent f0fd135 commit 6c02d4a

File tree

4 files changed

+9
-18
lines changed

4 files changed

+9
-18
lines changed

__tests__/shared/components/GenericLink.jsx

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global document, window */
1+
/* global window */
22

33
import GenericLink from 'components/GenericLink';
44
import PT from 'prop-types';
@@ -83,7 +83,6 @@ test('onClick(..) works when rendered as custom <Link>', () => {
8383
expect(clickHandler).toHaveBeenCalled();
8484

8585
const domain = 'https://some.domain.com';
86-
document.location.assign = jest.fn();
8786
doc = renderDom((
8887
<GenericLink
8988
className="LINK"
@@ -96,6 +95,5 @@ test('onClick(..) works when rendered as custom <Link>', () => {
9695
link = findInDomByClass(doc, 'LINK');
9796
simulate.click(link);
9897

99-
expect(document.location.assign).toHaveBeenCalledWith(domain);
10098
expect(window.scroll).toHaveBeenCalledTimes(1);
10199
});

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,5 @@
121121
"mkDistDir:prod": "mkdir -p dist/prod/shared/utils && mkdir -p dist/prod/client",
122122
"test": "npm run lint && npm run jest"
123123
},
124-
"version": "0.7.0"
124+
"version": "0.7.1"
125125
}

src/shared/components/GenericLink.jsx

+6-13
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* - User explicitely opts to use <a>.
77
*/
88

9-
/* global document, window */
9+
/* global window */
1010

1111
import _ from 'lodash';
1212
import PT from 'prop-types';
@@ -25,9 +25,12 @@ export default function GenericLink(props) {
2525
routerLinkType,
2626
to,
2727
} = props;
28+
29+
const url = new Url(to);
30+
2831
/* Renders the link as <a> element if either opted explicitely, or the link
2932
* should open a new tab, or it is an anchor reference. */
30-
if (enforceA || openNewTab || to.startsWith('#')) {
33+
if (enforceA || openNewTab || to.startsWith('#') || url.host) {
3134
return (
3235
<a
3336
className={className}
@@ -56,17 +59,7 @@ export default function GenericLink(props) {
5659
onClick: (e) => {
5760
/* If a custom onClick(..) handler was provided we execute it. */
5861
if (onClick) onClick(e);
59-
60-
/* If the URL leads to a different domain we make transition using JS,
61-
* preventing event processing by React Router. */
62-
const url = new Url(to);
63-
if (url.origin !== document.location.origin) {
64-
document.location.assign(to);
65-
e.preventDefault();
66-
67-
/* Scroll to the top-left corner of the page, just in case the link
68-
* references the same page we are already in. */
69-
} else window.scroll(0, 0);
62+
window.scroll(0, 0);
7063
},
7164
}, children);
7265
}

0 commit comments

Comments
 (0)