Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement native TextInput autoFocus on Android #27924

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ public class ReactEditText extends AppCompatEditText {
private @Nullable String mFontFamily = null;
private int mFontWeight = ReactTypefaceUtils.UNSET;
private int mFontStyle = ReactTypefaceUtils.UNSET;
private boolean mAutoFocus = false;
private boolean mDidAttachToWindow = false;

private ReactViewBackgroundManager mReactBackgroundManager;

Expand Down Expand Up @@ -750,6 +752,14 @@ public void onAttachedToWindow() {
span.onAttachedToWindow();
}
}

if (mAutoFocus && !mDidAttachToWindow) {
mShouldAllowFocus = true;
requestFocus();
mShouldAllowFocus = false;
}

mDidAttachToWindow = true;
}

@Override
Expand Down Expand Up @@ -813,6 +823,10 @@ public void setMaxFontSizeMultiplier(float maxFontSizeMultiplier) {
}
}

public void setAutoFocus(boolean autoFocus) {
mAutoFocus = autoFocus;
}

protected void applyTextAttributes() {
// In general, the `getEffective*` functions return `Float.NaN` if the
// property hasn't been set.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,11 @@ public void showKeyboardOnFocus(ReactEditText view, boolean showKeyboardOnFocus)
view.setShowSoftInputOnFocus(showKeyboardOnFocus);
}

@ReactProp(name = "autoFocus", defaultBoolean = false)
public void setAutoFocus(ReactEditText view, boolean autoFocus) {
view.setAutoFocus(autoFocus);
}

@ReactPropGroup(
names = {
ViewProps.BORDER_WIDTH,
Expand Down