Skip to content

Commit

Permalink
Implement native TextInput autoFocus on Android (#27924)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
janicduplessis authored and facebook-github-bot committed Feb 1, 2020
1 parent e4194a2 commit 055a41b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
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

0 comments on commit 055a41b

Please sign in to comment.