Skip to content

Commit

Permalink
Allowing Litho TextInput to accept null inputBackground
Browse files Browse the repository at this point in the history
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
  • Loading branch information
zaxy78 authored and facebook-github-bot committed Mar 12, 2019
1 parent 05b7a2e commit d1fd03b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -695,7 +705,7 @@ static void dispatchKey(
view.dispatchKeyEvent(keyEvent);
}
}

@OnTrigger(SetSelectionEvent.class)
static void setSelection(
ComponentContext c,
Expand Down

0 comments on commit d1fd03b

Please sign in to comment.