Skip to content

Commit 41975f7

Browse files
Disable onKeyPress logic when handler not defined
Summary: <!-- Required: Write your motivation here. If this PR fixes an issue, type "Fixes #issueNumber" to automatically close the issue when the PR is merged. --> There are some bugs surrounding the use of unicode characters that are causing issues with `onKeyPress` on Android: see #18405 (comment). 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. <!-- Required: Write your test plan here. If you changed any code, please provide us with clear instructions on how you verified your changes work. Bonus points for screenshots and videos! --> [ANDROID] [BUGFIX] [TextInput] - Disable TextInput onKeyPress event from being fired unless callback specified. Closes #18443 Differential Revision: D8149625 Pulled By: hramos fbshipit-source-id: cdf28141d71cdedd67a6ef350e3a3b955f97e340
1 parent 94393f8 commit 41975f7

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public class ReactEditText extends EditText {
8181
private @Nullable ScrollWatcher mScrollWatcher;
8282
private final InternalKeyListener mKeyListener;
8383
private boolean mDetectScrollMovement = false;
84+
private boolean mOnKeyPress = false;
8485
private float mLetterSpacingPt = 0;
8586

8687
private ReactViewBackgroundManager mReactBackgroundManager;
@@ -175,7 +176,7 @@ protected void onScrollChanged(int horiz, int vert, int oldHoriz, int oldVert) {
175176
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
176177
ReactContext reactContext = (ReactContext) getContext();
177178
InputConnection inputConnection = super.onCreateInputConnection(outAttrs);
178-
if (inputConnection != null) {
179+
if (inputConnection != null && mOnKeyPress) {
179180
inputConnection = new ReactEditTextInputConnectionWrapper(inputConnection, reactContext, this);
180181
}
181182

@@ -274,6 +275,10 @@ public void setBlurOnSubmit(@Nullable Boolean blurOnSubmit) {
274275
mBlurOnSubmit = blurOnSubmit;
275276
}
276277

278+
public void setOnKeyPress(boolean onKeyPress) {
279+
mOnKeyPress = onKeyPress;
280+
}
281+
277282
public boolean getBlurOnSubmit() {
278283
if (mBlurOnSubmit == null) {
279284
// Default blurOnSubmit

ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java

+5
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,11 @@ public void setOnScroll(final ReactEditText view, boolean onScroll) {
308308
}
309309
}
310310

311+
@ReactProp(name = "onKeyPress", defaultBoolean = false)
312+
public void setOnKeyPress(final ReactEditText view, boolean onKeyPress) {
313+
view.setOnKeyPress(onKeyPress);
314+
}
315+
311316
// Sets the letter spacing as an absolute point size.
312317
// This extra handling, on top of what ReactBaseTextShadowNode already does, is required for the
313318
// correct display of spacing in placeholder (hint) text.

0 commit comments

Comments
 (0)