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

Add KeyboardEvent.code to synthetic event #18287

Merged
merged 3 commits into from Apr 4, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
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,52 @@ describe('SyntheticKeyboardEvent', () => {
});
});
});

describe('code', () => {
it('returns code on `keydown`, `keyup` and null on `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',
bubbles: true,
cancelable: true,
}),
);
expect(codeDown).toBe('KeyQ');
expect(codeUp).toBe('KeyQ');
expect(codePress).toBe(null);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the spec say it should be?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spec says it should be a valid code. For some reason onKeyPress callback do not called when dispatched without charCode! I added charCode and codePress initializes now.

});
});
});

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