mirror of
https://github.com/zoriya/react-native-web.git
synced 2026-05-22 22:44:52 +00:00
[add] TextInput onKeyPress support for arrow keys
Close #791 Close #792
This commit is contained in:
@@ -216,6 +216,25 @@ describe('components/TextInput', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('arrow key', () => {
|
||||||
|
const onKeyPress = jest.fn();
|
||||||
|
const input = findNativeInput(mount(<TextInput onKeyPress={onKeyPress} />));
|
||||||
|
input.simulate('keyPress', { which: 37 });
|
||||||
|
expect(onKeyPress).toHaveBeenCalledTimes(1);
|
||||||
|
expect(onKeyPress).toBeCalledWith(
|
||||||
|
expect.objectContaining({
|
||||||
|
nativeEvent: {
|
||||||
|
altKey: undefined,
|
||||||
|
ctrlKey: undefined,
|
||||||
|
key: 'ArrowLeft',
|
||||||
|
metaKey: undefined,
|
||||||
|
shiftKey: undefined,
|
||||||
|
target: expect.anything()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
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} />));
|
||||||
|
|||||||
+24
-8
@@ -297,11 +297,19 @@ class TextInput extends Component<*> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
_handleKeyDown = e => {
|
_handleKeyDown = e => {
|
||||||
// prevent key events bubbling (see #612)
|
// Prevent key events bubbling (see #612)
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
|
|
||||||
// Backspace, Tab, and Cmd+Enter only fire 'keydown' DOM events
|
// Backspace, Tab, Cmd+Enter, and Arrow keys only fire 'keydown' DOM events
|
||||||
if (e.which === 8 || e.which === 9 || (e.which === 13 && e.metaKey)) {
|
if (
|
||||||
|
e.which === 8 ||
|
||||||
|
e.which === 9 ||
|
||||||
|
(e.which === 13 && e.metaKey) ||
|
||||||
|
e.which === 37 ||
|
||||||
|
e.which === 38 ||
|
||||||
|
e.which === 39 ||
|
||||||
|
e.which === 40
|
||||||
|
) {
|
||||||
this._handleKeyPress(e);
|
this._handleKeyPress(e);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -314,24 +322,32 @@ class TextInput extends Component<*> {
|
|||||||
if (onKeyPress) {
|
if (onKeyPress) {
|
||||||
let keyValue;
|
let keyValue;
|
||||||
switch (e.which) {
|
switch (e.which) {
|
||||||
// backspace
|
|
||||||
case 8:
|
case 8:
|
||||||
keyValue = 'Backspace';
|
keyValue = 'Backspace';
|
||||||
break;
|
break;
|
||||||
// tab
|
|
||||||
case 9:
|
case 9:
|
||||||
keyValue = 'Tab';
|
keyValue = 'Tab';
|
||||||
break;
|
break;
|
||||||
// enter
|
|
||||||
case 13:
|
case 13:
|
||||||
keyValue = 'Enter';
|
keyValue = 'Enter';
|
||||||
break;
|
break;
|
||||||
// spacebar
|
|
||||||
case 32:
|
case 32:
|
||||||
keyValue = ' ';
|
keyValue = ' ';
|
||||||
break;
|
break;
|
||||||
|
case 37:
|
||||||
|
keyValue = 'ArrowLeft';
|
||||||
|
break;
|
||||||
|
case 38:
|
||||||
|
keyValue = 'ArrowUp';
|
||||||
|
break;
|
||||||
|
case 39:
|
||||||
|
keyValue = 'ArrowRight';
|
||||||
|
break;
|
||||||
|
case 40:
|
||||||
|
keyValue = 'ArrowDown';
|
||||||
|
break;
|
||||||
default: {
|
default: {
|
||||||
// we trim to only care about the keys that has a textual representation
|
// Trim to only care about the keys that have a textual representation
|
||||||
if (e.shiftKey) {
|
if (e.shiftKey) {
|
||||||
keyValue = String.fromCharCode(e.which).trim();
|
keyValue = String.fromCharCode(e.which).trim();
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -220,10 +220,11 @@ const TextInputScreen = () => (
|
|||||||
Callback that is called when a key is pressed. This will be called with{' '}
|
Callback that is called when a key is pressed. This will be called with{' '}
|
||||||
<Code>{`{
|
<Code>{`{
|
||||||
nativeEvent: { key: keyValue } }`}</Code>{' '}
|
nativeEvent: { key: keyValue } }`}</Code>{' '}
|
||||||
where keyValue is <Code>Enter</Code> or <Code>Backspace</Code> for respective keys and
|
where keyValue is <Code>Enter</Code>, <Code>Backspace</Code>, <Code>Tab</Code>,{' '}
|
||||||
the typed-in character otherwise including <Code>' '</Code>
|
<Code>{'Arrow{Up,Right,Down,Left}'}</Code> for respective keys and the typed-in
|
||||||
for space. Modifier keys (e.g., <Code>shiftKey</Code>) are also included in the{' '}
|
character otherwise including <Code>' '</Code> for space. Modifier keys (e.g.,{' '}
|
||||||
<Code>nativeEvent</Code>. Fires before <Code>onChange</Code> callbacks.
|
<Code>shiftKey</Code>) are also included in the <Code>nativeEvent</Code>. Fires before{' '}
|
||||||
|
<Code>onChange</Code> callbacks.
|
||||||
</AppText>
|
</AppText>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
|||||||
Reference in New Issue
Block a user