Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added onKeyPress handler to Link. #6664

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/react-router-dom/modules/Link.js
Expand Up @@ -12,6 +12,23 @@ function isModifiedEvent(event) {
* The public API for rendering a history-aware <a>.
*/
class Link extends React.Component {
handleKeyPress(event, history) {
if (this.props.onKeyPress) this.props.onKeyPress(event);

if (
(event.key === "Enter" || event.key === " ") &&
!event.defaultPrevented && // onClick prevented default
(!this.props.target || this.props.target === "_self") && // let browser handle "target=_blank" etc.
!isModifiedEvent(event) // ignore clicks with modifier keys
) {
event.preventDefault();

const method = this.props.replace ? history.replace : history.push;

method(this.props.to);
}
}

handleClick(event, history) {
if (this.props.onClick) this.props.onClick(event);

Expand Down Expand Up @@ -47,6 +64,7 @@ class Link extends React.Component {
<a
{...rest}
onClick={event => this.handleClick(event, context.history)}
onKeyPress={event => this.handleKeyPress(event, context.history)}
href={href}
ref={innerRef}
/>
Expand All @@ -68,6 +86,7 @@ if (__DEV__) {
Link.propTypes = {
innerRef: innerRefType,
onClick: PropTypes.func,
onKeyPress: PropTypes.func,
replace: PropTypes.bool,
target: PropTypes.string,
to: toType.isRequired
Expand Down
111 changes: 111 additions & 0 deletions packages/react-router-dom/modules/__tests__/Link-test.js
Expand Up @@ -274,6 +274,117 @@ describe("A <Link>", () => {
button: 0
});

expect(memoryHistory.push).toBeCalledTimes(0);
});
});

describe("on keypress events", () => {
const memoryHistory = createMemoryHistory();
memoryHistory.push = jest.fn();

beforeEach(() => {
memoryHistory.push.mockReset();
});

it("calls onKeyPress eventhandler", () => {
const keyPressHandler = jest.fn();
const to = "/the/path?the=query#the-hash";

renderStrict(
<Router history={memoryHistory}>
<Link to={to} onKeyPress={keyPressHandler}>
link
</Link>
</Router>,
node
);

const a = node.querySelector("a");
ReactTestUtils.Simulate.keyPress(a, {
defaultPrevented: false,
key: "A"
});

expect(keyPressHandler).toBeCalledTimes(1);
});

it("calls history.push on Enter", () => {
const to = "/the/path?the=query#the-hash";

renderStrict(
<Router history={memoryHistory}>
<Link to={to}>link</Link>
</Router>,
node
);

const a = node.querySelector("a");
ReactTestUtils.Simulate.keyPress(a, {
defaultPrevented: false,
key: "Enter"
});

expect(memoryHistory.push).toBeCalledTimes(1);
expect(memoryHistory.push).toBeCalledWith(to);
});

it("calls history.push on space", () => {
const to = "/the/path?the=query#the-hash";

renderStrict(
<Router history={memoryHistory}>
<Link to={to}>link</Link>
</Router>,
node
);

const a = node.querySelector("a");
ReactTestUtils.Simulate.keyPress(a, {
defaultPrevented: false,
key: " "
});

expect(memoryHistory.push).toBeCalledTimes(1);
expect(memoryHistory.push).toBeCalledWith(to);
});

it("does not call history.push on prevented event.", () => {
const to = "/the/path?the=query#the-hash";

renderStrict(
<Router history={memoryHistory}>
<Link to={to}>link</Link>
</Router>,
node
);

const a = node.querySelector("a");
ReactTestUtils.Simulate.keyPress(a, {
defaultPrevented: true,
key: "Enter"
});

expect(memoryHistory.push).toBeCalledTimes(0);
});

it("does not call history.push target not specifying 'self'", () => {
const to = "/the/path?the=query#the-hash";

renderStrict(
<Router history={memoryHistory}>
<Link to={to} target="_blank">
link
</Link>
</Router>,
node
);

const a = node.querySelector("a");
ReactTestUtils.Simulate.keyPress(a, {
defaultPrevented: false,
key: "Enter"
});

expect(memoryHistory.push).toBeCalledTimes(0);
});
});
Expand Down