Skip to content

Commit

Permalink
Add KeyboardEvent.code to synthetic event (#18287)
Browse files Browse the repository at this point in the history
* Add KeyboardEvent.code to synthetic event

* remove null to 0 transformation

* make onKeyPress work
  • Loading branch information
bl00mber committed Apr 4, 2020
1 parent 5022fdf commit f625fce
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/react-dom/src/events/SyntheticKeyboardEvent.js
Expand Up @@ -16,6 +16,7 @@ import getEventModifierState from './getEventModifierState';
*/
const SyntheticKeyboardEvent = SyntheticUIEvent.extend({
key: getEventKey,
code: null,
location: null,
ctrlKey: null,
shiftKey: null,
Expand Down
Expand Up @@ -451,6 +451,53 @@ describe('SyntheticKeyboardEvent', () => {
});
});
});

describe('code', () => {
it('returns code on `keydown`, `keyup` and `keypress`', () => {
let codeDown = null;
let codeUp = null;
let codePress = null;
const node = ReactDOM.render(
<input
onKeyDown={e => {
codeDown = e.code;
}}
onKeyUp={e => {
codeUp = e.code;
}}
onKeyPress={e => {
codePress = e.code;
}}
/>,
container,
);
node.dispatchEvent(
new KeyboardEvent('keydown', {
code: 'KeyQ',
bubbles: true,
cancelable: true,
}),
);
node.dispatchEvent(
new KeyboardEvent('keyup', {
code: 'KeyQ',
bubbles: true,
cancelable: true,
}),
);
node.dispatchEvent(
new KeyboardEvent('keypress', {
code: 'KeyQ',
charCode: 113,
bubbles: true,
cancelable: true,
}),
);
expect(codeDown).toBe('KeyQ');
expect(codeUp).toBe('KeyQ');
expect(codePress).toBe('KeyQ');
});
});
});

describe('EventInterface', () => {
Expand Down

0 comments on commit f625fce

Please sign in to comment.