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

Allow overriding EditText construction in ReactTextInputShadowNode #27782

Conversation

mchowning
Copy link
Contributor

@mchowning mchowning commented Jan 15, 2020

Summary

This PR makes it possible for subclasses of ReactTextInputShadowNode to control the construction of the "dummy" EditText instance that ReactTextInputShadowNode internally uses to determine the expected height of the view. This PR does not change the default behavior, it just opens up that default to being overriden.

This is useful in the case of custom views that have different behavior from a "default" EditText instance (new EditText(context)). For example, it might have a different style applied.

As a side benefit, this change also makes it easy to have subclasses not apply the default theme, which can allow the custom view to avoid a longstanding crash issue (#17530).

Changelog

[Android] [Added] - Allow overriding EditText construction in ReactTextInputShadowNode

Test Plan

All tests pass.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jan 15, 2020
@react-native-bot react-native-bot added Component: TextInput Related to the TextInput component. Platform: Android Android applications. Type: Enhancement A new feature or enhancement of an existing feature. labels Jan 15, 2020
@mchowning
Copy link
Contributor Author

The iOS CI failures don't appear to be related to the changes in this PR.

@JoshuaGross JoshuaGross self-assigned this Jan 17, 2020
@JoshuaGross
Copy link
Contributor

I guess this seems fine, since it doesn't change any default behavior. Do you have examples of how you'd actually use this to override the default theme?

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.

@JoshuaGross has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@mchowning
Copy link
Contributor Author

mchowning commented Jan 17, 2020

Do you have examples of how you'd actually use this to override the default theme?

The simplest example would be to just create an EditText with a null AttributeSet and no (0) default style attributes:

@Override
 protected EditText createDummyEditText() {
   return new EditText(getThemedContext(), null, 0);
 }

This is actually a sufficient change to avoid the crash in #17530, and it also avoids applying padding based on the default EditText background, which varies by device manufacturer.

Another example would be that you have a customized EditText with various attributes (custom style, padding, etc.) that you are applying either in xml or in a subclass, and you would like the ShadowNode's measurement of the view to account for those attributes. In that case, you could do something like:

@Override
 protected EditText createDummyEditText() {
   return LayoutInflater.from(reactContext)
                  .inflate(R.layout.my_edit_text, ...);
 }

@@ -249,4 +249,8 @@ public void setPadding(int spacingType, float padding) {
super.setPadding(spacingType, padding);
markUpdated();
}

protected EditText createDummyEditText() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's call this something like createInternalEditText or createEditTextForMeasure since this is becoming a public API.

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's also add a javadoc here to document what this is for and how it can be used.

@JoshuaGross
Copy link
Contributor

See comments; I'm mostly fine with this but if this is enabling someone to manually fix a crash, I w wonder if there's an additional, permanent solution to that crash. Requiring that everyone implement a custom TextInput component to avoid a crash doesn't seem great.

@mchowning mchowning force-pushed the allow_overriding_edittext_construction_in_shadownode branch from bdfbbbe to 71f5163 Compare January 21, 2020 19:56
@mchowning
Copy link
Contributor Author

if this is enabling someone to manually fix a crash, I w wonder if there's an additional, permanent solution to that crash. Requiring that everyone implement a custom TextInput component to avoid a crash doesn't seem great.

Agreed. Unfortunately, this can only be used to avoid the crash if you have a custom view that does not rely on the default EditText background. Admittedly, there is probably a decent chance that a custom EditText view will have a different background, but it still makes sense for the default behavior to measure based on the default EditText background. For that reason, I don't consider this change as including a general fix for that crash, I just was pointing out that in a specific scenario (custom view that doesn't use default EditText background), it does allow the crash to be avoided for that view. The actual fix for that crash will need to fix it even when the default drawable is applied because, among other things, that is the situation for the TextInput component.

If that crash fix were the only reason behind this change, I would be hesitant to suggest this change. I think it is the fact that this change allows the subclass to alter the behavior of the internal EditText in other ways is what makes this worthwhile. The ability to fix the crash is just a nice byproduct.

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.

@JoshuaGross has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@react-native-bot
Copy link
Collaborator

This pull request was successfully merged by @mchowning in a5b5d1a.

When will my fix make it into a release? | Upcoming Releases

@react-native-bot react-native-bot added the Merged This PR has been merged. label Jan 21, 2020
alloy pushed a commit that referenced this pull request Feb 13, 2020
…27782)

Summary:
This PR makes it possible for subclasses of `ReactTextInputShadowNode` to control the construction of the "dummy" `EditText` instance that `ReactTextInputShadowNode` internally uses to determine the expected height of the view. This PR does not change the default behavior, it just opens up that default to being overriden.

This is useful in the case of custom views that have different behavior from a "default" `EditText` instance (`new EditText(context)`). For example, it might have a different style applied.

As a side benefit, this change also makes it easy to have subclasses not apply the default theme, which can allow the custom view to avoid a longstanding crash issue (#17530).

## Changelog

[Android] [Added] - Allow overriding `EditText` construction in `ReactTextInputShadowNode`
Pull Request resolved: #27782

Test Plan: All tests pass.

Reviewed By: mdvacca

Differential Revision: D19450593

Pulled By: JoshuaGross

fbshipit-source-id: 8d2ce6117246fc3e2108623312b38583af5722b3
osdnk pushed a commit to osdnk/react-native that referenced this pull request Mar 9, 2020
…acebook#27782)

Summary:
This PR makes it possible for subclasses of `ReactTextInputShadowNode` to control the construction of the "dummy" `EditText` instance that `ReactTextInputShadowNode` internally uses to determine the expected height of the view. This PR does not change the default behavior, it just opens up that default to being overriden.

This is useful in the case of custom views that have different behavior from a "default" `EditText` instance (`new EditText(context)`). For example, it might have a different style applied.

As a side benefit, this change also makes it easy to have subclasses not apply the default theme, which can allow the custom view to avoid a longstanding crash issue (facebook#17530).

## Changelog

[Android] [Added] - Allow overriding `EditText` construction in `ReactTextInputShadowNode`
Pull Request resolved: facebook#27782

Test Plan: All tests pass.

Reviewed By: mdvacca

Differential Revision: D19450593

Pulled By: JoshuaGross

fbshipit-source-id: 8d2ce6117246fc3e2108623312b38583af5722b3
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. Component: TextInput Related to the TextInput component. Merged This PR has been merged. Platform: Android Android applications. Type: Enhancement A new feature or enhancement of an existing feature.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants