Skip to content

Commit 055a41b

Browse files
janicduplessisfacebook-github-bot
authored andcommitted
Implement native TextInput autoFocus on Android (#27924)
Summary: Follow up to #27803. To keep consistency between the implementations this also implements autoFocus natively on Android. This will allow removing the JS auto focus logic completely. ## Changelog [Android] [Fixed] - Implement native TextInput autoFocus on Android Pull Request resolved: #27924 Test Plan: Test using the TextInput example in RN tester. Differential Revision: D19674506 Pulled By: TheSavior fbshipit-source-id: 9cbb517fc69bccd11f5292a1ccb4acea2e3713e9
1 parent e4194a2 commit 055a41b

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

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

+14
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ public class ReactEditText extends AppCompatEditText {
100100
private @Nullable String mFontFamily = null;
101101
private int mFontWeight = ReactTypefaceUtils.UNSET;
102102
private int mFontStyle = ReactTypefaceUtils.UNSET;
103+
private boolean mAutoFocus = false;
104+
private boolean mDidAttachToWindow = false;
103105

104106
private ReactViewBackgroundManager mReactBackgroundManager;
105107

@@ -750,6 +752,14 @@ public void onAttachedToWindow() {
750752
span.onAttachedToWindow();
751753
}
752754
}
755+
756+
if (mAutoFocus && !mDidAttachToWindow) {
757+
mShouldAllowFocus = true;
758+
requestFocus();
759+
mShouldAllowFocus = false;
760+
}
761+
762+
mDidAttachToWindow = true;
753763
}
754764

755765
@Override
@@ -813,6 +823,10 @@ public void setMaxFontSizeMultiplier(float maxFontSizeMultiplier) {
813823
}
814824
}
815825

826+
public void setAutoFocus(boolean autoFocus) {
827+
mAutoFocus = autoFocus;
828+
}
829+
816830
protected void applyTextAttributes() {
817831
// In general, the `getEffective*` functions return `Float.NaN` if the
818832
// property hasn't been set.

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

+5
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,11 @@ public void showKeyboardOnFocus(ReactEditText view, boolean showKeyboardOnFocus)
795795
view.setShowSoftInputOnFocus(showKeyboardOnFocus);
796796
}
797797

798+
@ReactProp(name = "autoFocus", defaultBoolean = false)
799+
public void setAutoFocus(ReactEditText view, boolean autoFocus) {
800+
view.setAutoFocus(autoFocus);
801+
}
802+
798803
@ReactPropGroup(
799804
names = {
800805
ViewProps.BORDER_WIDTH,

0 commit comments

Comments
 (0)