Skip to content

Commit 09f967a

Browse files
author
Adam Burdette
authored
fix: Issue 294 - selection color (#474)
1 parent 267de73 commit 09f967a

8 files changed

+43
-8
lines changed

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ return <Picker
190190
- [`mode`](#mode)
191191
- [`prompt`](#prompt)
192192
- [`itemStyle`](#itemstyle)
193+
- [`selectionColor`](#selectionColor)
193194

194195
---
195196

@@ -320,6 +321,12 @@ such that the total number of lines does not exceed this number. Default is '1'
320321
| --------- | -------- | -------- |
321322
| function | no | Android |
322323

324+
### `selectionColor`
325+
326+
| Type | Required | Platform |
327+
| ------- | -------- | -------- |
328+
| ColorValue | no | iOS |
329+
323330
## Methods
324331

325332
### `blur` (Android only, lib version 1.16.0+)
@@ -396,6 +403,7 @@ If set to false, the specific item will be disabled, i.e. the user will not be a
396403
- [`itemStyle`](#itemstyle)
397404
- [`onValueChange`](#onvaluechange)
398405
- [`selectedValue`](#selectedvalue)
406+
- [`selectionColor`](#selectionColor)
399407
- [`themeVariant`](#themeVariant)
400408

401409
---
@@ -428,6 +436,14 @@ If set to false, the specific item will be disabled, i.e. the user will not be a
428436

429437
---
430438

439+
### `selectionColor`
440+
441+
| Type | Required | Platform |
442+
| ------- | -------- | -------- |
443+
| ColorValue | no | iOS |
444+
445+
---
446+
431447
### `themeVariant`
432448

433449
| Type | Required |

example/src/PickerIOSExample.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ function PickerStyleExample() {
132132
<PickerIOS
133133
itemStyle={styles.item}
134134
selectedValue={carMake}
135+
selectionColor="rgba(0, 0, 0, 0.3)"
135136
onValueChange={(value) => setCarMake(value)}>
136137
{Object.keys(CAR_MAKES_AND_MODELS).map((value) => (
137138
<PickerIOS.Item

ios/RNCPicker.h

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

1616
@property (nonatomic, copy) NSArray<NSDictionary *> *items;
1717
@property (nonatomic, assign) NSInteger selectedIndex;
18+
@property (nonatomic, assign) NSInteger selectionColor;
1819

1920
@property (nonatomic, strong) UIColor *color;
2021
@property (nonatomic, strong) UIFont *font;

ios/RNCPicker.m

+8-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ - (instancetype)initWithFrame:(CGRect)frame
2020
if ((self = [super initWithFrame:frame])) {
2121
_color = [UIColor blackColor];
2222
_font = [UIFont systemFontOfSize:21]; // TODO: selected title default should be 23.5
23+
_numberOfLines = 1;
2324
_selectedIndex = NSNotFound;
25+
_selectionColor = 0;
2426
_textAlign = NSTextAlignmentCenter;
25-
_numberOfLines = 1;
2627
self.delegate = self;
2728
self.dataSource = self;
2829
[self selectRow:0 inComponent:0 animated:YES]; // Workaround for missing selection indicator lines (see https://stackoverflow.com/questions/39564660/uipickerview-selection-indicator-not-visible-in-ios10)
@@ -108,6 +109,12 @@ - (UIView *)pickerView:(UIPickerView *)pickerView
108109
[view insertSubview:label atIndex:0];
109110
}
110111

112+
if (@available(iOS 14.0, *)) {
113+
if (_selectionColor) {
114+
pickerView.subviews[1].backgroundColor = [RCTConvert UIColor:@(_selectionColor)];
115+
}
116+
}
117+
111118
RNCPickerLabel* label = view.subviews[0];
112119
label.font = _font;
113120

ios/RNCPickerManager.m

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ - (UIView *)view
2222

2323
RCT_EXPORT_VIEW_PROPERTY(items, NSArray<NSDictionary *>)
2424
RCT_EXPORT_VIEW_PROPERTY(selectedIndex, NSInteger)
25+
RCT_EXPORT_VIEW_PROPERTY(selectionColor, NSInteger)
2526
RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock)
2627
RCT_EXPORT_VIEW_PROPERTY(color, UIColor)
2728
RCT_EXPORT_VIEW_PROPERTY(textAlign, NSTextAlignment)

js/PickerIOS.ios.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ type Props = $ReadOnly<{|
4444
...ViewProps,
4545
children: ChildrenArray<Element<typeof PickerIOSItem>>,
4646
itemStyle?: ?TextStyleProp,
47+
numberOfLines: ?number,
4748
onChange?: ?(event: PickerIOSChangeEvent) => mixed,
4849
onValueChange?: ?(itemValue: string | number, itemIndex: number) => mixed,
4950
selectedValue: ?(number | string),
50-
numberOfLines: ?number,
51+
selectionColor: ?string,
5152
themeVariant: ?string,
5253
|}>;
5354

@@ -99,19 +100,21 @@ class PickerIOS extends React.Component<Props, State> {
99100
if (numberOfLines < 1) {
100101
numberOfLines = 1;
101102
}
103+
102104
return (
103105
<View style={this.props.style}>
104106
<RNCPickerNativeComponent
105107
ref={(picker) => {
106108
this._picker = picker;
107109
}}
108-
themeVariant={this.props.themeVariant}
109-
testID={this.props.testID}
110-
style={[styles.pickerIOS, this.props.itemStyle]}
111110
items={this.state.items}
112-
selectedIndex={this.state.selectedIndex}
113-
onChange={this._onChange}
114111
numberOfLines={numberOfLines}
112+
onChange={this._onChange}
113+
selectedIndex={this.state.selectedIndex}
114+
selectionColor={processColor(this.props.selectionColor)}
115+
style={[styles.pickerIOS, this.props.itemStyle]}
116+
testID={this.props.testID}
117+
themeVariant={this.props.themeVariant}
115118
/>
116119
</View>
117120
);

js/RNCPickerNativeComponent.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ type Label = Stringish | number;
3535
export type RNCPickerIOSType = HostComponent<
3636
$ReadOnly<{|
3737
items: $ReadOnlyArray<RNCPickerIOSTypeItemType>,
38+
numberOfLines?: ?number,
3839
onChange: (event: PickerIOSChangeEvent) => void,
3940
selectedIndex: number,
41+
selectionColor?: ?ProcessedColorValue,
4042
style?: ?TextStyleProp,
4143
testID?: ?string,
42-
numberOfLines?: ?number,
4344
themeVariant?: ?string,
4445
|}>,
4546
>;

typings/Picker.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ export interface PickerProps<T = ItemValue> extends ViewProps {
6969
* @platform ios
7070
*/
7171
itemStyle?: StyleProp<TextStyle>;
72+
/**
73+
* Color to apply to the selection indicator.
74+
* @platform ios
75+
*/
76+
selectionColor?: ColorValue;
7277
/**
7378
* Prompt string for this picker, used on Android in dialog mode as the title of the dialog.
7479
* @platform android

0 commit comments

Comments
 (0)