From d1fd03b4071794f55fc1617cf365f8a5f2a32558 Mon Sep 17 00:00:00 2001 From: Aviad Sachs Date: Mon, 11 Mar 2019 22:27:51 -0700 Subject: [PATCH] Allowing Litho TextInput to accept null inputBackground Summary: I created a static final dummy drawable, in order to distinguish between **User providing** ***null*** in `inputBackground`, and **Default value** ***null*** - where no background is provided, and default background should be fetched from resources. Differential Revision: D14324519 fbshipit-source-id: 8beca622e04105349f63e04d949d2832958e22a9 --- .../litho/widget/TextInputSpecTest.java | 17 +++++++++++ .../facebook/litho/widget/TextInputSpec.java | 30 ++++++++++++------- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/litho-it/src/test/java/com/facebook/litho/widget/TextInputSpecTest.java b/litho-it/src/test/java/com/facebook/litho/widget/TextInputSpecTest.java index c6e3568c32c..6e56a55c9dc 100644 --- a/litho-it/src/test/java/com/facebook/litho/widget/TextInputSpecTest.java +++ b/litho-it/src/test/java/com/facebook/litho/widget/TextInputSpecTest.java @@ -20,6 +20,7 @@ import android.content.res.Resources; import android.content.res.TypedArray; +import android.graphics.drawable.Drawable; import android.widget.TextView; import com.facebook.litho.Component; import com.facebook.litho.ComponentContext; @@ -106,6 +107,22 @@ public void testCursorDrawableResSet() throws IllegalAccessException, NoSuchFiel assertThat(f.get(editText)).isEqualTo(drawableRes); } + @Test + public void testNullInputBackground() throws IllegalAccessException, NoSuchFieldException { + Component.Builder component = TextInput.create(mContext).inputBackground(null); + final android.widget.EditText editText = getEditText(component); + Drawable editTextBackground = editText.getBackground(); + assertThat(editTextBackground).isEqualTo(null); + } + + @Test + public void testDefaultInputBackground() throws IllegalAccessException, NoSuchFieldException { + Component.Builder component = TextInput.create(mContext); + final android.widget.EditText editText = getEditText(component); + Drawable editTextBackground = editText.getBackground(); + assertThat(editTextBackground).isNotNull(); + } + private static android.widget.EditText getEditText(Component.Builder component) { final LithoView lithoView = ComponentTestHelper.mountComponent(component); return (android.widget.EditText) lithoView.getChildAt(0); diff --git a/litho-widget/src/main/java/com/facebook/litho/widget/TextInputSpec.java b/litho-widget/src/main/java/com/facebook/litho/widget/TextInputSpec.java index 3fe650bdc52..693a339f193 100644 --- a/litho-widget/src/main/java/com/facebook/litho/widget/TextInputSpec.java +++ b/litho-widget/src/main/java/com/facebook/litho/widget/TextInputSpec.java @@ -16,6 +16,7 @@ package com.facebook.litho.widget; +import static android.graphics.Color.TRANSPARENT; import static android.os.Build.VERSION.SDK_INT; import static android.os.Build.VERSION_CODES.JELLY_BEAN; import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1; @@ -154,6 +155,12 @@ } ) class TextInputSpec { + /** + * Dummy drawable used for differentiating user-provided null background drawable from default + * drawable of the spec + */ + private static final Drawable UNSET_DRAWABLE = new ColorDrawable(TRANSPARENT); + @PropDefault protected static final ColorStateList textColorStateList = ColorStateList.valueOf(Color.BLACK); @@ -164,6 +171,7 @@ class TextInputSpec { @PropDefault static final CharSequence initialText = ""; @PropDefault protected static final int shadowColor = Color.GRAY; @PropDefault protected static final int textSize = 13; + @PropDefault protected static final Drawable inputBackground = UNSET_DRAWABLE; @PropDefault protected static final Typeface typeface = Typeface.DEFAULT; @PropDefault protected static final int textAlignment = TEXT_ALIGNMENT_GRAVITY; @PropDefault protected static final int gravity = Gravity.CENTER_VERTICAL | Gravity.START; @@ -620,15 +628,17 @@ static void onUnbind(final ComponentContext c, EditTextWithEventHandlers editTex @Nullable static Drawable getBackgroundOrDefault(ComponentContext c, Drawable specifiedBackground) { - if (specifiedBackground != null) { - return specifiedBackground; - } - final int[] attrs = {android.R.attr.background}; - TypedArray a = - c.getAndroidContext().obtainStyledAttributes(null, attrs, android.R.attr.editTextStyle, 0); - Drawable defaultBackground = a.getDrawable(0); - a.recycle(); - return defaultBackground; + if (specifiedBackground == UNSET_DRAWABLE) { + final int[] attrs = {android.R.attr.background}; + TypedArray a = + c.getAndroidContext() + .obtainStyledAttributes(null, attrs, android.R.attr.editTextStyle, 0); + Drawable defaultBackground = a.getDrawable(0); + a.recycle(); + return defaultBackground; + } + + return specifiedBackground; } @OnTrigger(RequestFocusEvent.class) @@ -695,7 +705,7 @@ static void dispatchKey( view.dispatchKeyEvent(keyEvent); } } - + @OnTrigger(SetSelectionEvent.class) static void setSelection( ComponentContext c,