Skip to content

Commit 4626a4e

Browse files
feat: apply custom styling to active dropdown item (#609)
Closes #608
1 parent 1cb509c commit 4626a4e

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

index.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ export interface PickerSelectProps {
9191
Icon?: React.FC;
9292
InputAccessoryView?: React.ReactNode;
9393
darkTheme?: boolean;
94+
dropdownItemStyle?: StyleProp<ViewStyle>,
95+
activeItemStyle?: StyleProp<ViewStyle>,
9496
}
9597

9698
declare class Picker extends React.Component<PickerSelectProps> {

src/index.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export default class RNPickerSelect extends PureComponent {
6161
Icon: PropTypes.func,
6262
InputAccessoryView: PropTypes.func,
6363
dropdownItemStyle: PropTypes.shape({}),
64+
activeItemStyle: PropTypes.shape({}),
6465
};
6566

6667
static defaultProps = {
@@ -91,6 +92,7 @@ export default class RNPickerSelect extends PureComponent {
9192
InputAccessoryView: null,
9293
darkTheme: false,
9394
dropdownItemStyle: {},
95+
activeItemStyle: {},
9496
};
9597

9698
static handlePlaceholder({ placeholder }) {
@@ -276,14 +278,15 @@ export default class RNPickerSelect extends PureComponent {
276278
}
277279

278280
renderPickerItems() {
279-
const { items } = this.state;
281+
const { items, selectedItem } = this.state;
280282
const defaultItemColor = this.isDarkTheme() ? '#fff' : undefined;
281-
const { dropdownItemStyle } = this.props;
283+
284+
const { dropdownItemStyle, activeItemStyle } = this.props;
282285

283286
return items.map((item) => {
284287
return (
285288
<Picker.Item
286-
style={dropdownItemStyle}
289+
style={selectedItem.value === item.value ? activeItemStyle : dropdownItemStyle}
287290
label={item.label}
288291
value={item.value}
289292
key={item.key || item.label}

test/test.js

+43
Original file line numberDiff line numberDiff line change
@@ -579,4 +579,47 @@ describe('RNPickerSelect', () => {
579579
expect(item.props().style).toEqual(customDropdownItemStyle);
580580
});
581581
});
582+
583+
it('should apply custom styles to the active dropdown item', () => {
584+
const customActiveItemStyle = {
585+
backgroundColor: '#d0d4da',
586+
color: '#000',
587+
};
588+
589+
const selectItems = [
590+
{ label: 'Item 1', value: 'item1', key: '1' },
591+
{ label: 'Item 2', value: 'item2', key: '2' },
592+
];
593+
594+
const placeholder = { label: 'Select an item...', value: null };
595+
596+
const wrapper = shallow(
597+
<RNPickerSelect
598+
items={selectItems}
599+
placeholder={placeholder}
600+
onValueChange={() => {}}
601+
activeItemStyle={customActiveItemStyle}
602+
value="item2" // Select "Item 2"
603+
/>
604+
);
605+
606+
// Open the picker
607+
wrapper.find('[testID="ios_touchable_wrapper"]').simulate('press');
608+
609+
// Find picker items
610+
const pickerItems = wrapper.find('Picker').find('PickerItem');
611+
612+
// Ensure picker items are found
613+
expect(pickerItems.length).toBeGreaterThan(0);
614+
615+
// Check if the active item has the custom styles
616+
const activeItem = pickerItems.findWhere((item) => item.prop('value') === 'item2');
617+
618+
// Ensure activeItem is found
619+
expect(activeItem.exists()).toBe(true);
620+
621+
// Check styles applied to the active item
622+
expect(activeItem.prop('style')).toEqual(customActiveItemStyle);
623+
});
624+
582625
});

0 commit comments

Comments
 (0)