From 509783374d679069ebd79cd6ff803267c7e126d1 Mon Sep 17 00:00:00 2001 From: Josh Hargreaves Date: Fri, 25 May 2018 10:54:54 -0700 Subject: [PATCH] Disable onKeyPress logic when handler not defined Summary: There are some bugs surrounding the use of unicode characters that are causing issues with `onKeyPress` on Android: see https://github.com/facebook/react-native/issues/18405#issuecomment-373624413. We disable the creation and use of `ReactEditTextInputConnectionWrapper` unless the onKeyPress prop is specified, so that this code is 'opt-in' & not a general regression for every use of the TextInput. N.B. it seems to introduce a lot of unnecessary code complexity to allow for enabling/disabling the onKeyPress events after a InputConnection has been created in `onCreateInputConnection` when the keyboard focusses (a new input connection is created whenever a TextInput gains focus) so I opted not to for simplicity's sake. Build & debug RNTest app, verify ReactEditTextInputConnectionWrapper code not executed if onKeyPress function not specified. [ANDROID] [BUGFIX] [TextInput] - Disable TextInput onKeyPress event from being fired unless callback specified. Closes https://github.com/facebook/react-native/pull/18443 Differential Revision: D8149625 Pulled By: hramos fbshipit-source-id: cdf28141d71cdedd67a6ef350e3a3b955f97e340 --- .../com/facebook/react/views/textinput/ReactEditText.java | 7 ++++++- .../react/views/textinput/ReactTextInputManager.java | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java index 7a9f34e2a6a0dd..fb4a4d9cfc174e 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java @@ -81,6 +81,7 @@ public class ReactEditText extends EditText { private @Nullable ScrollWatcher mScrollWatcher; private final InternalKeyListener mKeyListener; private boolean mDetectScrollMovement = false; + private boolean mOnKeyPress = false; private float mLetterSpacingPt = 0; private ReactViewBackgroundManager mReactBackgroundManager; @@ -175,7 +176,7 @@ protected void onScrollChanged(int horiz, int vert, int oldHoriz, int oldVert) { public InputConnection onCreateInputConnection(EditorInfo outAttrs) { ReactContext reactContext = (ReactContext) getContext(); InputConnection inputConnection = super.onCreateInputConnection(outAttrs); - if (inputConnection != null) { + if (inputConnection != null && mOnKeyPress) { inputConnection = new ReactEditTextInputConnectionWrapper(inputConnection, reactContext, this); } @@ -274,6 +275,10 @@ public void setBlurOnSubmit(@Nullable Boolean blurOnSubmit) { mBlurOnSubmit = blurOnSubmit; } + public void setOnKeyPress(boolean onKeyPress) { + mOnKeyPress = onKeyPress; + } + public boolean getBlurOnSubmit() { if (mBlurOnSubmit == null) { // Default blurOnSubmit diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java index 637027140ec010..9d847e3629ebe7 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java @@ -306,6 +306,11 @@ public void setOnScroll(final ReactEditText view, boolean onScroll) { } } + @ReactProp(name = "onKeyPress", defaultBoolean = false) + public void setOnKeyPress(final ReactEditText view, boolean onKeyPress) { + view.setOnKeyPress(onKeyPress); + } + // Sets the letter spacing as an absolute point size. // This extra handling, on top of what ReactBaseTextShadowNode already does, is required for the // correct display of spacing in placeholder (hint) text.