Skip to content

Commit a5b5d1a

Browse files
mchowningfacebook-github-bot
authored andcommitted
Allow overriding EditText construction in ReactTextInputShadowNode (#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
1 parent a27e31c commit a5b5d1a

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

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

+14-6
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class ReactTextInputShadowNode extends ReactBaseTextShadowNode
3737
implements YogaMeasureFunction {
3838

3939
private int mMostRecentEventCount = UNSET;
40-
private @Nullable EditText mDummyEditText;
40+
private @Nullable EditText mInternalEditText;
4141
private @Nullable ReactTextInputLocalData mLocalData;
4242

4343
@VisibleForTesting public static final String PROP_TEXT = "text";
@@ -75,20 +75,20 @@ public void setThemedContext(ThemedReactContext themedContext) {
7575
// of Android), and it cannot be changed.
7676
// So, we have to enforce it as a default padding.
7777
// TODO #7120264: Cache this stuff better.
78-
EditText editText = new EditText(getThemedContext());
78+
EditText editText = createInternalEditText();
7979
setDefaultPadding(Spacing.START, ViewCompat.getPaddingStart(editText));
8080
setDefaultPadding(Spacing.TOP, editText.getPaddingTop());
8181
setDefaultPadding(Spacing.END, ViewCompat.getPaddingEnd(editText));
8282
setDefaultPadding(Spacing.BOTTOM, editText.getPaddingBottom());
8383

84-
mDummyEditText = editText;
84+
mInternalEditText = editText;
8585

8686
// We must measure the EditText without paddings, so we have to reset them.
87-
mDummyEditText.setPadding(0, 0, 0, 0);
87+
mInternalEditText.setPadding(0, 0, 0, 0);
8888

8989
// This is needed to fix an android bug since 4.4.3 which will throw an NPE in measure,
9090
// setting the layoutParams fixes it: https://code.google.com/p/android/issues/detail?id=75877
91-
mDummyEditText.setLayoutParams(
91+
mInternalEditText.setLayoutParams(
9292
new ViewGroup.LayoutParams(
9393
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
9494
}
@@ -101,7 +101,7 @@ public long measure(
101101
float height,
102102
YogaMeasureMode heightMode) {
103103
// measure() should never be called before setThemedContext()
104-
EditText editText = Assertions.assertNotNull(mDummyEditText);
104+
EditText editText = Assertions.assertNotNull(mInternalEditText);
105105

106106
if (mLocalData != null) {
107107
mLocalData.apply(editText);
@@ -249,4 +249,12 @@ public void setPadding(int spacingType, float padding) {
249249
super.setPadding(spacingType, padding);
250250
markUpdated();
251251
}
252+
253+
/**
254+
* May be overriden by subclasses that would like to provide their own instance of the internal
255+
* {@code EditText} this class uses to determine the expected size of the view.
256+
*/
257+
protected EditText createInternalEditText() {
258+
return new EditText(getThemedContext());
259+
}
252260
}

0 commit comments

Comments
 (0)