Skip to content

Commit 22eb711

Browse files
Eddie Duganfacebook-github-bot
Eddie Dugan
authored andcommitted
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
1 parent 327fbd0 commit 22eb711

9 files changed

+47
-0
lines changed

Libraries/Components/Picker/AndroidDialogPickerNativeComponent.js

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type NativeProps = $ReadOnly<{|
4141

4242
// Props
4343
color?: ?ColorValue,
44+
backgroundColor?: ?ColorValue,
4445
enabled?: WithDefault<boolean, true>,
4546
items: $ReadOnlyArray<PickerItem>,
4647
prompt?: WithDefault<string, ''>,

Libraries/Components/Picker/AndroidDropdownPickerNativeComponent.js

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type NativeProps = $ReadOnly<{|
4141

4242
// Props
4343
color?: ?ColorValue,
44+
backgroundColor?: ?ColorValue,
4445
enabled?: WithDefault<boolean, true>,
4546
items: $ReadOnlyArray<PickerItem>,
4647
prompt?: WithDefault<string, ''>,

Libraries/Components/Picker/Picker.js

+6
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ type PickerProps = $ReadOnly<{|
9696
*/
9797
itemStyle?: ?TextStyleProp,
9898

99+
/**
100+
* Color of the item background.
101+
* @platform android
102+
*/
103+
backgroundColor?: ColorValue,
104+
99105
/**
100106
* Prompt string for this picker, used on Android in dialog mode as the title of the dialog.
101107
* @platform android

Libraries/Components/Picker/PickerAndroid.android.js

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import StyleSheet from '../../StyleSheet/StyleSheet';
2121
import invariant from 'invariant';
2222
import processColor from '../../StyleSheet/processColor';
2323

24+
import type {ColorValue} from '../../StyleSheet/StyleSheetTypes';
2425
import type {SyntheticEvent} from '../../Types/CoreEventTypes';
2526
import type {TextStyleProp} from '../../StyleSheet/StyleSheet';
2627

@@ -36,6 +37,7 @@ type Props = $ReadOnly<{|
3637
accessibilityLabel?: ?Stringish,
3738
children?: React.Node,
3839
style?: ?TextStyleProp,
40+
backgroundColor?: ?ColorValue,
3941
selectedValue?: ?PickerItemValue,
4042
enabled?: ?boolean,
4143
mode?: ?('dialog' | 'dropdown'),
@@ -123,6 +125,7 @@ function PickerAndroid(props: Props): React.Node {
123125
styles.pickerAndroid,
124126
props.style,
125127
),
128+
backgroundColor: props.backgroundColor,
126129
testID: props.testID,
127130
};
128131
return props.mode === 'dropdown' ? (

ReactAndroid/src/main/java/com/facebook/react/viewmanagers/AndroidDialogPickerManagerDelegate.java

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public void setProperty(T view, String propName, @Nullable Object value) {
2626
case "color":
2727
mViewManager.setColor(view, value == null ? null : ((Double) value).intValue());
2828
break;
29+
case "backgroundColor":
30+
mViewManager.setBackgroundColor(view, value == null ? null : ((Double) value).intValue());
31+
break;
2932
case "enabled":
3033
mViewManager.setEnabled(view, value == null ? true : (boolean) value);
3134
break;

ReactAndroid/src/main/java/com/facebook/react/viewmanagers/AndroidDialogPickerManagerInterface.java

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
public interface AndroidDialogPickerManagerInterface<T extends View> {
1717
void setColor(T view, @Nullable Integer value);
18+
void setBackgroundColor(T view, @Nullable int value);
1819
void setEnabled(T view, boolean value);
1920
void setItems(T view, @Nullable ReadableArray value);
2021
void setPrompt(T view, @Nullable String value);

ReactAndroid/src/main/java/com/facebook/react/views/picker/ReactDialogPickerManager.java

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package com.facebook.react.views.picker;
99

1010
import android.widget.Spinner;
11+
import androidx.annotation.NonNull;
1112
import com.facebook.react.module.annotations.ReactModule;
1213
import com.facebook.react.uimanager.ThemedReactContext;
1314
import com.facebook.react.uimanager.ViewManagerDelegate;
@@ -41,4 +42,9 @@ protected ReactPicker createViewInstance(ThemedReactContext reactContext) {
4142
protected ViewManagerDelegate<ReactPicker> getDelegate() {
4243
return mDelegate;
4344
}
45+
46+
@Override
47+
public void setBackgroundColor(@NonNull ReactPicker view, int backgroundColor) {
48+
view.setStagedBackgroundColor(backgroundColor);
49+
}
4450
}

ReactAndroid/src/main/java/com/facebook/react/views/picker/ReactPicker.java

+12
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class ReactPicker extends AppCompatSpinner {
2525
private @Nullable List<ReactPickerItem> mStagedItems;
2626
private @Nullable Integer mStagedSelection;
2727
private @Nullable Integer mStagedPrimaryTextColor;
28+
private @Nullable Integer mStagedBackgroundColor;
2829

2930
private final OnItemSelectedListener mItemSelectedListener =
3031
new OnItemSelectedListener() {
@@ -136,6 +137,10 @@ public OnSelectListener getOnSelectListener() {
136137
mStagedPrimaryTextColor = primaryColor;
137138
}
138139

140+
/* package */ void setStagedBackgroundColor(@Nullable Integer backgroundColor) {
141+
mStagedBackgroundColor = backgroundColor;
142+
}
143+
139144
/**
140145
* Used to commit staged data into ReactPicker view. During this period, we will disable {@link
141146
* OnSelectListener#onItemSelected(int)} temporarily, so we don't get an event when changing the
@@ -171,6 +176,13 @@ public OnSelectListener getOnSelectListener() {
171176
mStagedPrimaryTextColor = null;
172177
}
173178

179+
if (mStagedBackgroundColor != null
180+
&& adapter != null
181+
&& mStagedBackgroundColor != adapter.getBackgroundColor()) {
182+
adapter.setBackgroundColor(mStagedBackgroundColor);
183+
mStagedBackgroundColor = null;
184+
}
185+
174186
setOnItemSelectedListener(mItemSelectedListener);
175187
}
176188

ReactAndroid/src/main/java/com/facebook/react/views/picker/ReactPickerAdapter.java

+14
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class ReactPickerAdapter extends ArrayAdapter<ReactPickerItem> {
2323

2424
private final LayoutInflater mInflater;
2525
private @Nullable Integer mPrimaryTextColor;
26+
private @Nullable Integer mBackgroundColor;
2627

2728
public ReactPickerAdapter(Context context, List<ReactPickerItem> data) {
2829
super(context, 0, data);
@@ -67,15 +68,28 @@ private View getView(int position, View convertView, ViewGroup parent, boolean i
6768
textView.setTextColor((ColorStateList) textView.getTag());
6869
}
6970

71+
if (mBackgroundColor != null) {
72+
textView.setBackgroundColor(mBackgroundColor);
73+
}
74+
7075
return textView;
7176
}
7277

7378
public @Nullable Integer getPrimaryTextColor() {
7479
return mPrimaryTextColor;
7580
}
7681

82+
public @Nullable Integer getBackgroundColor() {
83+
return mBackgroundColor;
84+
}
85+
7786
public void setPrimaryTextColor(@Nullable Integer primaryTextColor) {
7887
mPrimaryTextColor = primaryTextColor;
7988
notifyDataSetChanged();
8089
}
90+
91+
public void setBackgroundColor(@Nullable Integer backgroundColor) {
92+
mBackgroundColor = backgroundColor;
93+
notifyDataSetChanged();
94+
}
8195
}

0 commit comments

Comments
 (0)