Skip to content

Commit

Permalink
fix: handle links with a _self target (#321)
Browse files Browse the repository at this point in the history
Links having a `_self` target could be handled by Found.
This fix is based on a [PR](https://github.com/ReactTraining/react-router/pull/6138/files#diff-8c17f8f99262d24cba447ddc8f465455R41) in react-router.
  • Loading branch information
qgerome authored and taion committed Apr 3, 2019
1 parent 61533f4 commit 0b65d2c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/BaseLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ class BaseLink extends React.Component {

// Don't do anything if the user's onClick handler prevented default.
// Otherwise, let the browser handle the link with the computed href if the
// event wasn't an unmodified left click, or if the link has a target.
// event wasn't an unmodified left click, or if the link has a target other
// than _self.
if (
event.defaultPrevented ||
event.metaKey ||
event.altKey ||
event.ctrlKey ||
event.shiftKey ||
event.button !== 0 ||
target
(target && target !== '_self')
) {
return;
}
Expand Down
11 changes: 10 additions & 1 deletion test/BaseLink.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,23 @@ describe('<BaseLink>', () => {
expect(router.push).not.toBeCalled();
});

it('should not navigate if target is defined', () => {
it('should not navigate if target is defined and not _self', () => {
const link = mount(
<BaseLink to="/" match={{}} router={router} target="_blank" />,
);

link.find('a').simulate('click', { button: 0 });
expect(router.push).not.toBeCalled();
});

it('should navigate if target is _self', () => {
const link = mount(
<BaseLink to="/" match={{}} router={router} target="_self" />,
);

link.find('a').simulate('click', { button: 0 });
expect(router.push).toBeCalled();
});
});

describe('active state', () => {
Expand Down

0 comments on commit 0b65d2c

Please sign in to comment.