Skip to content

Commit d88e470

Browse files
valerio.pontefacebook-github-bot
valerio.ponte
authored andcommitted
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: #25028 Differential Revision: D15503070 Pulled By: mdvacca fbshipit-source-id: db4616fa165643d6ef2b3185008c4d279ae08092
1 parent 206bb6d commit d88e470

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

Libraries/Components/TextInput/TextInput.js

+7
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ type AndroidProps = $ReadOnly<{|
253253
| 'yes'
254254
| 'yesExcludeDescendants'
255255
),
256+
showSoftInputOnFocus?: ?boolean,
256257
|}>;
257258

258259
type Props = $ReadOnly<{|
@@ -926,6 +927,12 @@ const TextInput = createReactClass({
926927
'newPassword',
927928
'oneTimeCode',
928929
]),
930+
/**
931+
* When `false`, it will prevent the soft keyboard from showing when the field is focused.
932+
* Defaults to `true`.
933+
* @platform android
934+
*/
935+
showSoftInputOnFocus: PropTypes.bool,
929936
},
930937
getDefaultProps() {
931938
return {

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
213213
}
214214
setFocusableInTouchMode(true);
215215
boolean focused = super.requestFocus(direction, previouslyFocusedRect);
216-
showSoftKeyboard();
216+
if (getShowSoftInputOnFocus()) {
217+
showSoftKeyboard();
218+
}
217219
return focused;
218220
}
219221

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

+6
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,12 @@ public void setBorderStyle(ReactEditText view, @Nullable String borderStyle) {
715715
view.setBorderStyle(borderStyle);
716716
}
717717

718+
@ReactProp(name = "showSoftInputOnFocus", defaultBoolean = true)
719+
public void showKeyboardOnFocus(ReactEditText view, boolean showKeyboardOnFocus) {
720+
view.setShowSoftInputOnFocus(showKeyboardOnFocus);
721+
}
722+
723+
718724
@ReactPropGroup(names = {
719725
ViewProps.BORDER_WIDTH,
720726
ViewProps.BORDER_LEFT_WIDTH,

0 commit comments

Comments
 (0)