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

ReactEditText extends AppCompatEditText #23322

Closed
wants to merge 1 commit into from

Conversation

dulmandakh
Copy link
Contributor

@dulmandakh dulmandakh commented Feb 7, 2019

Summary

Google recommends to extend AppCompat widgets, and this PR changes ReactEditText to extend AppCompatEditText.

Changelog

[Android] [Changed] - ReactEditText extends AppCompatEditText

Test Plan

CI is green, and everything works as before.

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Contributor A React Native contributor. Partner labels Feb 7, 2019
@pull-bot
Copy link

pull-bot commented Feb 7, 2019

Warnings
⚠️

📋 Changelog Format - Did you include a Changelog? A changelog entry has the following format: [CATEGORY] [TYPE] - Message.

Generated by 🚫 dangerJS

@facebook-github-bot facebook-github-bot added the Import Started This pull request has been imported. This does not imply the PR has been approved. label Feb 7, 2019
Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cpojer is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@cpojer cpojer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@dulmandakh
Copy link
Contributor Author

@cpojer is there anything I can help with?

@facebook-github-bot
Copy link
Contributor

I tried to merge this pull request into the Facebook internal repo but some checks failed. To unblock yourself please check the following: Does this pull request pass all open source tests on GitHub? If not please fix those. Does the code still apply cleanly on top of GitHub master? If not can please rebase. In all other cases this means some internal test failed, for example a part of a fb app won't work with this pull request. I've added the Import Failed label to this pull request so it is easy for someone at fb to find the pull request and check what failed. If you don't see anyone comment in a few days feel free to comment mentioning one of the core contributors to the project so they get a notification.

@facebook-github-bot facebook-github-bot added Import Failed and removed Import Started This pull request has been imported. This does not imply the PR has been approved. labels Feb 8, 2019
@hey99xx
Copy link

hey99xx commented Feb 11, 2019

You may want to test this with old Android OS like API 19.

Earlier I found some AppCompat widgets wrap the original context, making any (ReactContext) getContext() throws

java.lang.ClassCastException: android.support.v7.widget.TintContextWrapper cannot be cast to com.facebook.react.bridge.ReactContext`

I see couple dangerous getContext usages that way in ReactEditText class, and AppCompatEditText is one of those widgets that wraps its context:

https://github.com/aosp-mirror/platform_frameworks_support/blob/support-library-27.1.0/v7/appcompat/src/main/java/android/support/v7/widget/AppCompatEditText.java#L64

See #22885 (comment) was fixed in 58437cd In some of these PRs you may need to do something similar or find better way to keep hold of context.

@dulmandakh
Copy link
Contributor Author

dulmandakh commented Feb 11, 2019

@hey99xx We cannot stick to platform widgets forever, even they are getting deprecated. What would be a generic solution for this?

@dulmandakh
Copy link
Contributor Author

dulmandakh commented Feb 11, 2019

@hey99xx quick google search reveals that it can be solved if we extend AppCompatActivity. So I think it'll gone once 0.59 released with ReactActivity extending AppCompatActivity. Is that true?

@hey99xx
Copy link

hey99xx commented Feb 11, 2019

I dont know why activity class matters for casting context. In support library code that wraps the context with TintContextWrapper only api version is checked, not activity type. I have two suggestion for you:


(1) We can either have a generic method that does the context unwrapping done in 58437cd like

public static ReactContext getReactContext(View view) { ... }

then when we need to get the context

@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
-  ReactContext reactContext = (ReactContext) getContext();
+  ReactContext reactContext = ReactContextHelper.getReactContext(this);

(2) or we save the ReactContext as variable when we know it'll be used again:

+final ReactContext mReactContext;

-public ReactEditText(Context context) {
-  super(context);
+public ReactEditText(ReactContext context) {
+  super(context);
+  mReactContext = context;
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
- ReactContext reactContext = (ReactContext) getContext();
  InputConnection inputConnection = super.onCreateInputConnection(outAttrs);
  if (inputConnection != null && mOnKeyPress) {
-   inputConnection = new ReactEditTextInputConnectionWrapper(inputConnection, reactContext, this);
+   inputConnection = new ReactEditTextInputConnectionWrapper(inputConnection, mReactContext, this);
  }

I prefer 2nd as it's cleaner, and you don't need to worry about which context class wraps which in the future.

@hey99xx
Copy link

hey99xx commented Feb 11, 2019

Btw platform widgets like EditText is not getting deprecated. AppCompatEditText is a subclass of EditText that's documented as

A EditText which supports compatible features on older versions of the platform, ...

As I understand, AppCompat widgets only add value for old Android versions to make app look consistent with modern Android versions, as also mentioned in original checkbox PR #18318. So I support your PRs like this, just make sure you test it :)

@dulmandakh
Copy link
Contributor Author

dulmandakh commented Feb 11, 2019

@hey99xx I target M for my current project, because we've been experiencing many issues/inconsistencies on Android versions below. Recently, I found that RN use many APIs that are not available in Android 4.x, and I think the solution might be more use of Support Library. Thank you.

@dulmandakh
Copy link
Contributor Author

@cpojer @mdvacca please review and merge 👍

@hey99xx
Copy link

hey99xx commented Feb 12, 2019

Aren't setIntrinsicContentSize and onCreateInputConnection crash on lower API versions after this?

@dulmandakh dulmandakh closed this Feb 22, 2019
@dulmandakh dulmandakh deleted the textinput-appcompat branch February 22, 2019 14:21
@dulmandakh dulmandakh restored the textinput-appcompat branch February 22, 2019 14:21
@dulmandakh dulmandakh deleted the textinput-appcompat branch February 22, 2019 14:23
@hramos hramos added Merged This PR has been merged. and removed Import Failed labels Mar 8, 2019
facebook-github-bot pushed a commit that referenced this pull request Feb 24, 2022
Summary:
This sync includes the following changes:
- **[4de99b3ca](facebook/react@4de99b3ca )**: fix getSnapshot warning when a selector returns NaN ([#23333](facebook/react#23333)) //<OGURA Daiki>//
- **[40eaa22d9](facebook/react@40eaa22d9 )**: Remove dependency on Offscreen Fiber updateQueue for React Cache ([#23229](facebook/react#23229)) //<Luna Ruan>//
- **[caf6d4707](facebook/react@caf6d4707 )**: Enable enableCache on Test Renderer native ([#23314](facebook/react#23314)) //<David McCabe>//
- **[419ccc2b1](facebook/react@419ccc2b1 )**: Land skipUnmountedBoundaries experiment ([#23322](facebook/react#23322)) //<Andrew Clark>//
- **[54f785bc5](facebook/react@54f785bc5 )**: Disallow comments as DOM containers for createRoot ([#23321](facebook/react#23321)) //<Andrew Clark>//
- **[e9aa9592c](facebook/react@e9aa9592c )**: change ReactBatchConfig.transition //<Luna Ruan>//
- **[51c8411d9](facebook/react@51c8411d9 )**: Log a recoverable error whenever hydration fails ([#23319](facebook/react#23319)) //<Andrew Clark>//
- **[79ed5e18f](facebook/react@79ed5e18f )**: Delete vestigial RetryAfterError logic ([#23312](facebook/react#23312)) //<Andrew Clark>//
- **[80059bb73](facebook/react@80059bb73 )**: Switch to client rendering if root receives update ([#23309](facebook/react#23309)) //<Andrew Clark>//
- **[f7f7ed089](facebook/react@f7f7ed089 )**: Allow suspending in the shell during hydration ([#23304](facebook/react#23304)) //<Andrew Clark>//

Changelog:
[General][Changed] - React Native sync for revisions 27b5699...4de99b3

jest_e2e[run_all_tests]

Reviewed By: rickhanlonii

Differential Revision: D34399162

fbshipit-source-id: 5c49e2bdcf63eb6a601cfa6a4e4b8f2e1f83e2dd
Saadnajmi pushed a commit to Saadnajmi/react-native-macos that referenced this pull request Jan 15, 2023
Summary:
This sync includes the following changes:
- **[4de99b3ca](facebook/react@4de99b3ca )**: fix getSnapshot warning when a selector returns NaN ([facebook#23333](facebook/react#23333)) //<OGURA Daiki>//
- **[40eaa22d9](facebook/react@40eaa22d9 )**: Remove dependency on Offscreen Fiber updateQueue for React Cache ([facebook#23229](facebook/react#23229)) //<Luna Ruan>//
- **[caf6d4707](facebook/react@caf6d4707 )**: Enable enableCache on Test Renderer native ([facebook#23314](facebook/react#23314)) //<David McCabe>//
- **[419ccc2b1](facebook/react@419ccc2b1 )**: Land skipUnmountedBoundaries experiment ([facebook#23322](facebook/react#23322)) //<Andrew Clark>//
- **[54f785bc5](facebook/react@54f785bc5 )**: Disallow comments as DOM containers for createRoot ([facebook#23321](facebook/react#23321)) //<Andrew Clark>//
- **[e9aa9592c](facebook/react@e9aa9592c )**: change ReactBatchConfig.transition //<Luna Ruan>//
- **[51c8411d9](facebook/react@51c8411d9 )**: Log a recoverable error whenever hydration fails ([facebook#23319](facebook/react#23319)) //<Andrew Clark>//
- **[79ed5e18f](facebook/react@79ed5e18f )**: Delete vestigial RetryAfterError logic ([facebook#23312](facebook/react#23312)) //<Andrew Clark>//
- **[80059bb73](facebook/react@80059bb73 )**: Switch to client rendering if root receives update ([facebook#23309](facebook/react#23309)) //<Andrew Clark>//
- **[f7f7ed089](facebook/react@f7f7ed089 )**: Allow suspending in the shell during hydration ([facebook#23304](facebook/react#23304)) //<Andrew Clark>//

Changelog:
[General][Changed] - React Native sync for revisions 27b5699...4de99b3

jest_e2e[run_all_tests]

Reviewed By: rickhanlonii

Differential Revision: D34399162

fbshipit-source-id: 5c49e2bdcf63eb6a601cfa6a4e4b8f2e1f83e2dd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Contributor A React Native contributor. Merged This PR has been merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants