Skip to content

Commit 73b459e

Browse files
committed
[add] TextInput onKeyPress support for arrow keys
Close necolas#791 Close necolas#792
1 parent a41af0f commit 73b459e

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

packages/react-native-web/src/exports/TextInput/__tests__/index-test.js

+19
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,25 @@ describe('components/TextInput', () => {
216216
);
217217
});
218218

219+
test('arrow key', () => {
220+
const onKeyPress = jest.fn();
221+
const input = findNativeInput(mount(<TextInput onKeyPress={onKeyPress} />));
222+
input.simulate('keyPress', { which: 37 });
223+
expect(onKeyPress).toHaveBeenCalledTimes(1);
224+
expect(onKeyPress).toBeCalledWith(
225+
expect.objectContaining({
226+
nativeEvent: {
227+
altKey: undefined,
228+
ctrlKey: undefined,
229+
key: 'ArrowLeft',
230+
metaKey: undefined,
231+
shiftKey: undefined,
232+
target: expect.anything()
233+
}
234+
})
235+
);
236+
});
237+
219238
test('text key', () => {
220239
const onKeyPress = jest.fn();
221240
const input = findNativeInput(mount(<TextInput onKeyPress={onKeyPress} />));

packages/react-native-web/src/exports/TextInput/index.js

+24-8
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,19 @@ class TextInput extends Component<*> {
297297
};
298298

299299
_handleKeyDown = e => {
300-
// prevent key events bubbling (see #612)
300+
// Prevent key events bubbling (see #612)
301301
e.stopPropagation();
302302

303-
// Backspace, Tab, and Cmd+Enter only fire 'keydown' DOM events
304-
if (e.which === 8 || e.which === 9 || (e.which === 13 && e.metaKey)) {
303+
// Backspace, Tab, Cmd+Enter, and Arrow keys only fire 'keydown' DOM events
304+
if (
305+
e.which === 8 ||
306+
e.which === 9 ||
307+
(e.which === 13 && e.metaKey) ||
308+
e.which === 37 ||
309+
e.which === 38 ||
310+
e.which === 39 ||
311+
e.which === 40
312+
) {
305313
this._handleKeyPress(e);
306314
}
307315
};
@@ -314,24 +322,32 @@ class TextInput extends Component<*> {
314322
if (onKeyPress) {
315323
let keyValue;
316324
switch (e.which) {
317-
// backspace
318325
case 8:
319326
keyValue = 'Backspace';
320327
break;
321-
// tab
322328
case 9:
323329
keyValue = 'Tab';
324330
break;
325-
// enter
326331
case 13:
327332
keyValue = 'Enter';
328333
break;
329-
// spacebar
330334
case 32:
331335
keyValue = ' ';
332336
break;
337+
case 37:
338+
keyValue = 'ArrowLeft';
339+
break;
340+
case 38:
341+
keyValue = 'ArrowUp';
342+
break;
343+
case 39:
344+
keyValue = 'ArrowRight';
345+
break;
346+
case 40:
347+
keyValue = 'ArrowDown';
348+
break;
333349
default: {
334-
// we trim to only care about the keys that has a textual representation
350+
// Trim to only care about the keys that have a textual representation
335351
if (e.shiftKey) {
336352
keyValue = String.fromCharCode(e.which).trim();
337353
} else {

website/storybook/1-components/TextInput/TextInputScreen.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,11 @@ const TextInputScreen = () => (
220220
Callback that is called when a key is pressed. This will be called with{' '}
221221
<Code>{`{
222222
nativeEvent: { key: keyValue } }`}</Code>{' '}
223-
where keyValue is <Code>Enter</Code> or <Code>Backspace</Code> for respective keys and
224-
the typed-in character otherwise including <Code>' '</Code>
225-
for space. Modifier keys (e.g., <Code>shiftKey</Code>) are also included in the{' '}
226-
<Code>nativeEvent</Code>. Fires before <Code>onChange</Code> callbacks.
223+
where keyValue is <Code>Enter</Code>, <Code>Backspace</Code>, <Code>Tab</Code>,{' '}
224+
<Code>{'Arrow{Up,Right,Down,Left}'}</Code> for respective keys and the typed-in
225+
character otherwise including <Code>' '</Code> for space. Modifier keys (e.g.,{' '}
226+
<Code>shiftKey</Code>) are also included in the <Code>nativeEvent</Code>. Fires before{' '}
227+
<Code>onChange</Code> callbacks.
227228
</AppText>
228229
}
229230
/>

0 commit comments

Comments
 (0)