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

enables ctrl + enter for keypress event on browsers other than firefox #10514

Merged
merged 7 commits into from
Jan 5, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,47 @@ describe('SyntheticKeyboardEvent', () => {
);
expect(called).toBe(false);
});

it('when charCode is 10, returns 13', () => {
let charCode = null;
const node = ReactDOM.render(
<input
onKeyPress={e => {
charCode = e.charCode;
}}
/>,
container,
);
node.dispatchEvent(
new KeyboardEvent('keypress', {
charCode: 10,
bubbles: true,
cancelable: true,
}),
);
expect(charCode).toBe(13);
});

it('when charCode is 10 and ctrl is pressed, returns 13', () => {
let charCode = null;
const node = ReactDOM.render(
<input
onKeyPress={e => {
charCode = e.charCode;
}}
/>,
container,
);
node.dispatchEvent(
new KeyboardEvent('keypress', {
charCode: 10,
ctrlKey: true,
bubbles: true,
cancelable: true,
}),
);
expect(charCode).toBe(13);
});
});

// TODO: this seems IE8 specific.
Expand Down
6 changes: 6 additions & 0 deletions packages/react-dom/src/events/getEventCharCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ function getEventCharCode(nativeEvent) {
charCode = keyCode;
}

// IE and Edge (on Windows) and Chrome / Safari (on Windows and Linux)
// report Enter as charCode 10 when ctrl is pressed.
if (charCode === 10) {
charCode = 13;
}

// Some non-printable keys are reported in `charCode`/`keyCode`, discard them.
// Must not discard the (non-)printable Enter-key.
if (charCode >= 32 || charCode === 13) {
Expand Down