From efa9a813ded151c3bd9f4f7aabe50ba31734b8d4 Mon Sep 17 00:00:00 2001 From: "valerio.ponte" Date: Fri, 24 May 2019 15:34:55 -0700 Subject: [PATCH] Add showSoftInputOnFocus to TextInput (#25028) Summary: Add prop showSoftInputOnFocus to TextInput. This fixes #14045. This prop can be used to prevent the system keyboard from displaying at all when focusing an input text, for example if a custom keyboard component needs to be displayed instead. On Android, currently TextInput always open the soft keyboard when focused. This is because `requestFocus` calls `showSoftKeyboard`, which in turn instructs `InputMethodManager` to show the soft keyboard. Unfortunately even if we were to define a new input type that extends ReactEditText, there is no way to overcome this issue. This is because `showSoftKeyboard` is a private method so it can't be overriden. And at the same time `requestFocus` needs to invoke `super.requestFocus` to properly instruct Android that the field has gained focused, so overriding `requestFocus` in a subclass of ReactEditText is also not an option, as when invoking `super.requestFocus` we would end up calling again the one defined in ReactEditText. So currently the only way of doing this is to basically add a listener on the focus event that will close the soft keyboard immediately after. But for a split second it will still be displayed. The code in the PR changes `requestFocus` to honor showSoftInputOnFocus as defined in Android TextView, displaying the soft keyboard unless instructed otherwise. ## Changelog [Android] [Added] - Add showSoftInputOnFocus to TextInput Pull Request resolved: https://github.com/facebook/react-native/pull/25028 Differential Revision: D15503070 Pulled By: mdvacca fbshipit-source-id: db4616fa165643d6ef2b3185008c4d279ae08092 --- Libraries/Components/TextInput/TextInput.js | 7 +++++++ .../com/facebook/react/views/textinput/ReactEditText.java | 4 +++- .../react/views/textinput/ReactTextInputManager.java | 6 ++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Libraries/Components/TextInput/TextInput.js b/Libraries/Components/TextInput/TextInput.js index bb40beb12c54fe..93474585e6aa82 100644 --- a/Libraries/Components/TextInput/TextInput.js +++ b/Libraries/Components/TextInput/TextInput.js @@ -252,6 +252,7 @@ type AndroidProps = $ReadOnly<{| | 'yes' | 'yesExcludeDescendants' ), + showSoftInputOnFocus?: ?boolean, |}>; type Props = $ReadOnly<{| @@ -925,6 +926,12 @@ const TextInput = createReactClass({ 'newPassword', 'oneTimeCode', ]), + /** + * When `false`, it will prevent the soft keyboard from showing when the field is focused. + * Defaults to `true`. + * @platform android + */ + showSoftInputOnFocus: PropTypes.bool, }, getDefaultProps() { return { 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 f603bfb847193e..dc0ea57f276519 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 @@ -206,7 +206,9 @@ public boolean requestFocus(int direction, Rect previouslyFocusedRect) { } setFocusableInTouchMode(true); boolean focused = super.requestFocus(direction, previouslyFocusedRect); - showSoftKeyboard(); + if (getShowSoftInputOnFocus()) { + showSoftKeyboard(); + } return focused; } 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 63d80befc066ab..5358a4450e6093 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 @@ -715,6 +715,12 @@ public void setBorderStyle(ReactEditText view, @Nullable String borderStyle) { view.setBorderStyle(borderStyle); } + @ReactProp(name = "showSoftInputOnFocus", defaultBoolean = true) + public void showKeyboardOnFocus(ReactEditText view, boolean showKeyboardOnFocus) { + view.setShowSoftInputOnFocus(showKeyboardOnFocus); + } + + @ReactPropGroup(names = { ViewProps.BORDER_WIDTH, ViewProps.BORDER_LEFT_WIDTH,