From 535a7b702712f50120a3d507fe6c7752da8959d9 Mon Sep 17 00:00:00 2001 From: Paul Armstrong Date: Fri, 16 Jun 2017 08:23:06 -0700 Subject: [PATCH] [fix] TextInput: fix onSubmitEditing when multiline=true Do not trigger onSubmitEditing when Shift+Enter is pressed in a multiline TextInput. Fix #524 Close #526 --- .../TextInput/__tests__/index-test.js | 29 +++++++++++++++++++ src/components/TextInput/index.js | 4 +-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/components/TextInput/__tests__/index-test.js b/src/components/TextInput/__tests__/index-test.js index 18fca322..38b9c964 100644 --- a/src/components/TextInput/__tests__/index-test.js +++ b/src/components/TextInput/__tests__/index-test.js @@ -237,6 +237,35 @@ describe('components/TextInput', () => { done(); } }); + + test('multi-line input', () => { + const onSubmitEditing = jest.fn(); + const input = findNativeTextarea( + mount() + ); + input.simulate('keyPress', { which: 13 }); + expect(onSubmitEditing).not.toHaveBeenCalled(); + }); + + test('multi-line input with "blurOnSubmit" triggers onSubmitEditing', () => { + const onSubmitEditing = jest.fn(); + const input = findNativeTextarea( + mount( + + ) + ); + + // shift+enter should enter newline, not submit + input.simulate('keyPress', { which: 13, shiftKey: true }); + input.simulate('keyPress', { which: 13 }); + expect(onSubmitEditing).toHaveBeenCalledTimes(1); + expect(onSubmitEditing).not.toHaveBeenCalledWith(expect.objectContaining({ shiftKey: true })); + }); }); test('prop "secureTextEntry"', () => { diff --git a/src/components/TextInput/index.js b/src/components/TextInput/index.js index 2537fd7f..44beeef2 100644 --- a/src/components/TextInput/index.js +++ b/src/components/TextInput/index.js @@ -318,8 +318,8 @@ class TextInput extends Component { } } - if (!e.isDefaultPrevented() && e.which === 13) { - if (onSubmitEditing) { + if (!e.isDefaultPrevented() && e.which === 13 && !e.shiftKey) { + if ((blurOnSubmit || !multiline) && onSubmitEditing) { e.nativeEvent = { target: e.target, text: e.target.value }; onSubmitEditing(e); }