RN picker - implement background color

Summary:
add support to the android implementation of the Picker component for setting the background color.

Changelog: [Android] [Added] - Support item background color in Dialog Picker

Differential Revision: D20566131

fbshipit-source-id: d693b40803fa1051ec955c5728994c820fecd9e9
This commit is contained in:
Eddie Dugan
2020-03-23 08:33:15 -07:00
committed by Facebook GitHub Bot
parent 327fbd0509
commit 22eb711c84
9 changed files with 47 additions and 0 deletions
@@ -26,6 +26,9 @@ public class AndroidDialogPickerManagerDelegate<T extends View, U extends BaseVi
case "color":
mViewManager.setColor(view, value == null ? null : ((Double) value).intValue());
break;
case "backgroundColor":
mViewManager.setBackgroundColor(view, value == null ? null : ((Double) value).intValue());
break;
case "enabled":
mViewManager.setEnabled(view, value == null ? true : (boolean) value);
break;
@@ -15,6 +15,7 @@ import com.facebook.react.bridge.ReadableArray;
public interface AndroidDialogPickerManagerInterface<T extends View> {
void setColor(T view, @Nullable Integer value);
void setBackgroundColor(T view, @Nullable int value);
void setEnabled(T view, boolean value);
void setItems(T view, @Nullable ReadableArray value);
void setPrompt(T view, @Nullable String value);
@@ -8,6 +8,7 @@
package com.facebook.react.views.picker;
import android.widget.Spinner;
import androidx.annotation.NonNull;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.ViewManagerDelegate;
@@ -41,4 +42,9 @@ public class ReactDialogPickerManager extends ReactPickerManager
protected ViewManagerDelegate<ReactPicker> getDelegate() {
return mDelegate;
}
@Override
public void setBackgroundColor(@NonNull ReactPicker view, int backgroundColor) {
view.setStagedBackgroundColor(backgroundColor);
}
}
@@ -25,6 +25,7 @@ public class ReactPicker extends AppCompatSpinner {
private @Nullable List<ReactPickerItem> mStagedItems;
private @Nullable Integer mStagedSelection;
private @Nullable Integer mStagedPrimaryTextColor;
private @Nullable Integer mStagedBackgroundColor;
private final OnItemSelectedListener mItemSelectedListener =
new OnItemSelectedListener() {
@@ -136,6 +137,10 @@ public class ReactPicker extends AppCompatSpinner {
mStagedPrimaryTextColor = primaryColor;
}
/* package */ void setStagedBackgroundColor(@Nullable Integer backgroundColor) {
mStagedBackgroundColor = backgroundColor;
}
/**
* Used to commit staged data into ReactPicker view. During this period, we will disable {@link
* OnSelectListener#onItemSelected(int)} temporarily, so we don't get an event when changing the
@@ -171,6 +176,13 @@ public class ReactPicker extends AppCompatSpinner {
mStagedPrimaryTextColor = null;
}
if (mStagedBackgroundColor != null
&& adapter != null
&& mStagedBackgroundColor != adapter.getBackgroundColor()) {
adapter.setBackgroundColor(mStagedBackgroundColor);
mStagedBackgroundColor = null;
}
setOnItemSelectedListener(mItemSelectedListener);
}
@@ -23,6 +23,7 @@ class ReactPickerAdapter extends ArrayAdapter<ReactPickerItem> {
private final LayoutInflater mInflater;
private @Nullable Integer mPrimaryTextColor;
private @Nullable Integer mBackgroundColor;
public ReactPickerAdapter(Context context, List<ReactPickerItem> data) {
super(context, 0, data);
@@ -67,6 +68,10 @@ class ReactPickerAdapter extends ArrayAdapter<ReactPickerItem> {
textView.setTextColor((ColorStateList) textView.getTag());
}
if (mBackgroundColor != null) {
textView.setBackgroundColor(mBackgroundColor);
}
return textView;
}
@@ -74,8 +79,17 @@ class ReactPickerAdapter extends ArrayAdapter<ReactPickerItem> {
return mPrimaryTextColor;
}
public @Nullable Integer getBackgroundColor() {
return mBackgroundColor;
}
public void setPrimaryTextColor(@Nullable Integer primaryTextColor) {
mPrimaryTextColor = primaryTextColor;
notifyDataSetChanged();
}
public void setBackgroundColor(@Nullable Integer backgroundColor) {
mBackgroundColor = backgroundColor;
notifyDataSetChanged();
}
}