Skip to content

Commit

Permalink
enables ctrl + enter for keypress event on browsers other than firefox (
Browse files Browse the repository at this point in the history
facebook#10514)

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

* makes comment more descriptive as to affected platforms

* reverting fiber results

* Reset changes to results.json

* Remove old test file

* Add tests in the right place
  • Loading branch information
nstraub authored and ManasJayanth committed Jan 12, 2018
1 parent de9b3e1 commit 69397d6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
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

0 comments on commit 69397d6

Please sign in to comment.