[change] implement TextInput without e.which

e.which is considered deprecated and should be replaced with e.key.

Fix #1190
Close #1193
This commit is contained in:
Nicolas Gallagher
2018-12-20 18:42:39 +00:00
parent 77d4bd8a97
commit 8925bf76af
2 changed files with 23 additions and 62 deletions
@@ -183,7 +183,7 @@ describe('components/TextInput', () => {
test('arrow key', () => { test('arrow key', () => {
const onKeyPress = jest.fn(); const onKeyPress = jest.fn();
const input = findNativeInput(mount(<TextInput onKeyPress={onKeyPress} />)); const input = findNativeInput(mount(<TextInput onKeyPress={onKeyPress} />));
input.simulate('keyPress', { which: 37 }); input.simulate('keyPress', { key: 'ArrowLeft' });
expect(onKeyPress).toHaveBeenCalledTimes(1); expect(onKeyPress).toHaveBeenCalledTimes(1);
expect(onKeyPress).toBeCalledWith( expect(onKeyPress).toBeCalledWith(
expect.objectContaining({ expect.objectContaining({
@@ -202,7 +202,7 @@ describe('components/TextInput', () => {
test('backspace key', () => { test('backspace key', () => {
const onKeyPress = jest.fn(); const onKeyPress = jest.fn();
const input = findNativeInput(mount(<TextInput onKeyPress={onKeyPress} />)); const input = findNativeInput(mount(<TextInput onKeyPress={onKeyPress} />));
input.simulate('keyDown', { which: 8 }); input.simulate('keyDown', { key: 'Backspace' });
expect(onKeyPress).toHaveBeenCalledTimes(1); expect(onKeyPress).toHaveBeenCalledTimes(1);
expect(onKeyPress).toBeCalledWith( expect(onKeyPress).toBeCalledWith(
expect.objectContaining({ expect.objectContaining({
@@ -221,7 +221,7 @@ describe('components/TextInput', () => {
test('enter key', () => { test('enter key', () => {
const onKeyPress = jest.fn(); const onKeyPress = jest.fn();
const input = findNativeInput(mount(<TextInput onKeyPress={onKeyPress} />)); const input = findNativeInput(mount(<TextInput onKeyPress={onKeyPress} />));
input.simulate('keyPress', { which: 13 }); input.simulate('keyPress', { key: 'Enter' });
expect(onKeyPress).toHaveBeenCalledTimes(1); expect(onKeyPress).toHaveBeenCalledTimes(1);
expect(onKeyPress).toBeCalledWith( expect(onKeyPress).toBeCalledWith(
expect.objectContaining({ expect.objectContaining({
@@ -240,7 +240,7 @@ describe('components/TextInput', () => {
test('escape key', () => { test('escape key', () => {
const onKeyPress = jest.fn(); const onKeyPress = jest.fn();
const input = findNativeInput(mount(<TextInput onKeyPress={onKeyPress} />)); const input = findNativeInput(mount(<TextInput onKeyPress={onKeyPress} />));
input.simulate('keyPress', { which: 27 }); input.simulate('keyPress', { key: 'Escape' });
expect(onKeyPress).toHaveBeenCalledTimes(1); expect(onKeyPress).toHaveBeenCalledTimes(1);
expect(onKeyPress).toBeCalledWith( expect(onKeyPress).toBeCalledWith(
expect.objectContaining({ expect.objectContaining({
@@ -259,7 +259,7 @@ describe('components/TextInput', () => {
test('space key', () => { test('space key', () => {
const onKeyPress = jest.fn(); const onKeyPress = jest.fn();
const input = findNativeInput(mount(<TextInput onKeyPress={onKeyPress} />)); const input = findNativeInput(mount(<TextInput onKeyPress={onKeyPress} />));
input.simulate('keyPress', { which: 32 }); input.simulate('keyPress', { key: ' ' });
expect(onKeyPress).toHaveBeenCalledTimes(1); expect(onKeyPress).toHaveBeenCalledTimes(1);
expect(onKeyPress).toBeCalledWith( expect(onKeyPress).toBeCalledWith(
expect.objectContaining({ expect.objectContaining({
@@ -278,7 +278,7 @@ describe('components/TextInput', () => {
test('tab key', () => { test('tab key', () => {
const onKeyPress = jest.fn(); const onKeyPress = jest.fn();
const input = findNativeInput(mount(<TextInput onKeyPress={onKeyPress} />)); const input = findNativeInput(mount(<TextInput onKeyPress={onKeyPress} />));
input.simulate('keyDown', { which: 9 }); input.simulate('keyDown', { key: 'Tab' });
expect(onKeyPress).toHaveBeenCalledTimes(1); expect(onKeyPress).toHaveBeenCalledTimes(1);
expect(onKeyPress).toBeCalledWith( expect(onKeyPress).toBeCalledWith(
expect.objectContaining({ expect.objectContaining({
@@ -297,7 +297,7 @@ describe('components/TextInput', () => {
test('text key', () => { test('text key', () => {
const onKeyPress = jest.fn(); const onKeyPress = jest.fn();
const input = findNativeInput(mount(<TextInput onKeyPress={onKeyPress} />)); const input = findNativeInput(mount(<TextInput onKeyPress={onKeyPress} />));
input.simulate('keyPress', { which: 97 }); input.simulate('keyPress', { key: 'a' });
expect(onKeyPress).toHaveBeenCalledTimes(1); expect(onKeyPress).toHaveBeenCalledTimes(1);
expect(onKeyPress).toBeCalledWith( expect(onKeyPress).toBeCalledWith(
expect.objectContaining({ expect.objectContaining({
@@ -321,7 +321,7 @@ describe('components/TextInput', () => {
ctrlKey: true, ctrlKey: true,
metaKey: true, metaKey: true,
shiftKey: true, shiftKey: true,
which: 32 key: ' '
}); });
expect(onKeyPress).toHaveBeenCalledTimes(1); expect(onKeyPress).toHaveBeenCalledTimes(1);
expect(onKeyPress).toBeCalledWith( expect(onKeyPress).toBeCalledWith(
@@ -343,7 +343,7 @@ describe('components/TextInput', () => {
const input = findNativeInput(mount(<TextInput onKeyPress={onKeyPress} />)); const input = findNativeInput(mount(<TextInput onKeyPress={onKeyPress} />));
input.simulate('keyDown', { input.simulate('keyDown', {
metaKey: true, metaKey: true,
which: 13 key: 'Enter'
}); });
expect(onKeyPress).toHaveBeenCalledTimes(1); expect(onKeyPress).toHaveBeenCalledTimes(1);
}); });
@@ -377,7 +377,7 @@ describe('components/TextInput', () => {
const input = findNativeInput( const input = findNativeInput(
mount(<TextInput defaultValue="12345" onSubmitEditing={onSubmitEditing} />) mount(<TextInput defaultValue="12345" onSubmitEditing={onSubmitEditing} />)
); );
input.simulate('keyPress', { which: 13 }); input.simulate('keyPress', { key: 'Enter' });
function onSubmitEditing(e) { function onSubmitEditing(e) {
expect(e.nativeEvent.target).toBeDefined(); expect(e.nativeEvent.target).toBeDefined();
expect(e.nativeEvent.text).toBe('12345'); expect(e.nativeEvent.text).toBe('12345');
@@ -390,7 +390,7 @@ describe('components/TextInput', () => {
const input = findNativeTextarea( const input = findNativeTextarea(
mount(<TextInput defaultValue="12345" multiline onSubmitEditing={onSubmitEditing} />) mount(<TextInput defaultValue="12345" multiline onSubmitEditing={onSubmitEditing} />)
); );
input.simulate('keyPress', { which: 13 }); input.simulate('keyPress', { key: 'Enter' });
expect(onSubmitEditing).not.toHaveBeenCalled(); expect(onSubmitEditing).not.toHaveBeenCalled();
}); });
@@ -410,11 +410,11 @@ describe('components/TextInput', () => {
); );
// shift+enter should enter newline, not submit // shift+enter should enter newline, not submit
input.simulate('keyPress', { which: 13, preventDefault, shiftKey: true }); input.simulate('keyPress', { key: 'Enter', preventDefault, shiftKey: true });
expect(onSubmitEditing).not.toHaveBeenCalledWith(expect.objectContaining({ shiftKey: true })); expect(onSubmitEditing).not.toHaveBeenCalledWith(expect.objectContaining({ shiftKey: true }));
expect(preventDefault).not.toHaveBeenCalled(); expect(preventDefault).not.toHaveBeenCalled();
input.simulate('keyPress', { which: 13, preventDefault }); input.simulate('keyPress', { key: 'Enter', preventDefault });
expect(onSubmitEditing).toHaveBeenCalledTimes(1); expect(onSubmitEditing).toHaveBeenCalledTimes(1);
expect(preventDefault).toHaveBeenCalledTimes(1); expect(preventDefault).toHaveBeenCalledTimes(1);
}); });
+10 -49
View File
@@ -318,14 +318,14 @@ class TextInput extends Component<*> {
// Backspace, Escape, Tab, Cmd+Enter, and Arrow keys only fire 'keydown' // Backspace, Escape, Tab, Cmd+Enter, and Arrow keys only fire 'keydown'
// DOM events // DOM events
if ( if (
e.which === 8 || e.key === 'ArrowLeft' ||
e.which === 9 || e.key === 'ArrowUp' ||
(e.which === 13 && e.metaKey) || e.key === 'ArrowRight' ||
e.which === 27 || e.key === 'ArrowDown' ||
e.which === 37 || e.key === 'Backspace' ||
e.which === 38 || e.key === 'Escape' ||
e.which === 39 || (e.key === 'Enter' && e.metaKey) ||
e.which === 40 e.key === 'Tab'
) { ) {
this._handleKeyPress(e); this._handleKeyPress(e);
} }
@@ -337,46 +337,7 @@ class TextInput extends Component<*> {
const shouldBlurOnSubmit = blurOnSubmit == null ? blurOnSubmitDefault : blurOnSubmit; const shouldBlurOnSubmit = blurOnSubmit == null ? blurOnSubmitDefault : blurOnSubmit;
if (onKeyPress) { if (onKeyPress) {
let keyValue; const keyValue = e.key;
switch (e.which) {
case 8:
keyValue = 'Backspace';
break;
case 9:
keyValue = 'Tab';
break;
case 13:
keyValue = 'Enter';
break;
case 27:
keyValue = 'Escape';
break;
case 32:
keyValue = ' ';
break;
case 37:
keyValue = 'ArrowLeft';
break;
case 38:
keyValue = 'ArrowUp';
break;
case 39:
keyValue = 'ArrowRight';
break;
case 40:
keyValue = 'ArrowDown';
break;
default: {
// Trim to only care about the keys that have a textual representation
if (e.shiftKey) {
keyValue = String.fromCharCode(e.which).trim();
} else {
keyValue = String.fromCharCode(e.which)
.toLowerCase()
.trim();
}
}
}
if (keyValue) { if (keyValue) {
e.nativeEvent = { e.nativeEvent = {
@@ -391,7 +352,7 @@ class TextInput extends Component<*> {
} }
} }
if (!e.isDefaultPrevented() && e.which === 13 && !e.shiftKey) { if (!e.isDefaultPrevented() && e.key === 'Enter' && !e.shiftKey) {
if ((blurOnSubmit || !multiline) && onSubmitEditing) { if ((blurOnSubmit || !multiline) && onSubmitEditing) {
// prevent "Enter" from inserting a newline // prevent "Enter" from inserting a newline
e.preventDefault(); e.preventDefault();