[change] TextInput 'onKeyPress' includes modifier keys

Include modifier keys in the 'nativeEvent'. This allows keyboard
shortcuts such as "Shift+Enter" to be implemented for submiting text
input content when the modality is keyboard + mouse rather than touch.
This commit is contained in:
Nicolas Gallagher
2017-06-23 14:48:34 -07:00
parent dfa8087f9a
commit e9101abefe
3 changed files with 42 additions and 6 deletions

View File

@@ -106,7 +106,8 @@ Callback that is called when the text input is focused.
Callback that is called when a key is pressed. This will be called with `{
nativeEvent: { key: keyValue } }` where keyValue is 'Enter` or 'Backspace' for
respective keys and the typed-in character otherwise including ' ' for space.
Fires before onChange callbacks.
Modifier keys are also included in the nativeEvent. Fires before onChange
callbacks.
**onSelectionChange**: ?function

View File

@@ -147,7 +147,7 @@ describe('components/TextInput', () => {
});
describe('prop "onKeyPress"', () => {
test('enter', done => {
test('enter key', done => {
const input = findNativeInput(mount(<TextInput onKeyPress={onKeyPress} />));
input.simulate('keyPress', { which: 13 });
function onKeyPress(e) {
@@ -156,7 +156,7 @@ describe('components/TextInput', () => {
}
});
test('space', done => {
test('space key', done => {
const input = findNativeInput(mount(<TextInput onKeyPress={onKeyPress} />));
input.simulate('keyPress', { which: 32 });
function onKeyPress(e) {
@@ -165,7 +165,7 @@ describe('components/TextInput', () => {
}
});
test('backspace', done => {
test('backspace key', done => {
const input = findNativeInput(mount(<TextInput onKeyPress={onKeyPress} />));
input.simulate('keyDown', { which: 8 });
function onKeyPress(e) {
@@ -174,7 +174,7 @@ describe('components/TextInput', () => {
}
});
test('text', done => {
test('text key', done => {
const input = findNativeInput(mount(<TextInput onKeyPress={onKeyPress} />));
input.simulate('keyPress', { which: 97 });
function onKeyPress(e) {
@@ -182,6 +182,33 @@ describe('components/TextInput', () => {
done();
}
});
test('target element is included', done => {
const input = findNativeInput(mount(<TextInput onKeyPress={onKeyPress} />));
input.simulate('keyPress');
function onKeyPress(e) {
expect(e.nativeEvent.target).toBeDefined();
done();
}
});
test('modifier keys', done => {
const input = findNativeInput(mount(<TextInput onKeyPress={onKeyPress} />));
input.simulate('keyPress', {
altKey: true,
ctrlKey: true,
metaKey: true,
shiftKey: true,
which: 97
});
function onKeyPress(e) {
expect(e.nativeEvent.altKey).toEqual(true);
expect(e.nativeEvent.ctrlKey).toEqual(true);
expect(e.nativeEvent.metaKey).toEqual(true);
expect(e.nativeEvent.shiftKey).toEqual(true);
done();
}
});
});
test('prop "onSelectionChange"', done => {

View File

@@ -306,7 +306,15 @@ class TextInput extends Component {
}
if (keyValue) {
onKeyPress({ nativeEvent: { key: keyValue } });
const nativeEvent = {
altKey: e.altKey,
ctrlKey: e.ctrlKey,
key: keyValue,
metaKey: e.metaKey,
shiftKey: e.shiftKey,
target: e.target
};
onKeyPress({ nativeEvent });
}
}