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

Adding code property to SyntheticKeyboardEvent with test #11325

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/react-dom/src/events/SyntheticKeyboardEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@ var KeyboardEventInterface = {
}
return 0;
},
code: function(event) {
// `code` represents a physical key on the keyboard, as opposed to the character
// generated by pressing the key.

// It returns a value not altered by keyboard layout
// or the state of the modifier keys.

// Assuming the input doesn't come from a physical keyboard, but instead from a virtual
// keyboard or accessibility device, it will be returned a value set by the browser to match
// as closely as possible to what would happen with a physical keyboard, to maximize
// compatibility between physical and virtual input devices.
if (event.type === 'keydown' || event.type === 'keyup') {
return event.code;
Copy link
Contributor

Choose a reason for hiding this comment

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

According to MDN, code isn't widely supported. Is there a way to cheaply polyfill this for those browsers?

}

return 0;
},
which: function(event) {
// `which` is an alias for either `keyCode` or `charCode` depending on the
// type of the event.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ describe('SyntheticKeyboardEvent', () => {
});
});

describe('code', () => {
describe('when event is `keydown` or `keyup`', () => {
it('returns a passed code', () => {
var keyboardEvent = createEvent({type: 'keyup', code: 'ArrowDown'});
expect(keyboardEvent.code).toBe('ArrowDown');
});
});

describe('when event is `keypress`', () => {
it('returns 0', () => {
var keyboardEvent = createEvent({type: 'keypress', charCode: 40});
expect(keyboardEvent.code).toBe(0);
});
});
});

describe('which', () => {
describe('when event is `keypress`', () => {
it('returns whatever getEventCharCode returns', () => {
Expand Down