Skip to content

Commit

Permalink
feat: apply custom styling to active dropdown item (#609)
Browse files Browse the repository at this point in the history
Closes #608
  • Loading branch information
SkMamtajuddin authored Aug 12, 2024
1 parent 1cb509c commit 4626a4e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ export interface PickerSelectProps {
Icon?: React.FC;
InputAccessoryView?: React.ReactNode;
darkTheme?: boolean;
dropdownItemStyle?: StyleProp<ViewStyle>,
activeItemStyle?: StyleProp<ViewStyle>,
}

declare class Picker extends React.Component<PickerSelectProps> {
Expand Down
9 changes: 6 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export default class RNPickerSelect extends PureComponent {
Icon: PropTypes.func,
InputAccessoryView: PropTypes.func,
dropdownItemStyle: PropTypes.shape({}),
activeItemStyle: PropTypes.shape({}),
};

static defaultProps = {
Expand Down Expand Up @@ -91,6 +92,7 @@ export default class RNPickerSelect extends PureComponent {
InputAccessoryView: null,
darkTheme: false,
dropdownItemStyle: {},
activeItemStyle: {},
};

static handlePlaceholder({ placeholder }) {
Expand Down Expand Up @@ -276,14 +278,15 @@ export default class RNPickerSelect extends PureComponent {
}

renderPickerItems() {
const { items } = this.state;
const { items, selectedItem } = this.state;
const defaultItemColor = this.isDarkTheme() ? '#fff' : undefined;
const { dropdownItemStyle } = this.props;

const { dropdownItemStyle, activeItemStyle } = this.props;

return items.map((item) => {
return (
<Picker.Item
style={dropdownItemStyle}
style={selectedItem.value === item.value ? activeItemStyle : dropdownItemStyle}
label={item.label}
value={item.value}
key={item.key || item.label}
Expand Down
43 changes: 43 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,4 +579,47 @@ describe('RNPickerSelect', () => {
expect(item.props().style).toEqual(customDropdownItemStyle);
});
});

it('should apply custom styles to the active dropdown item', () => {
const customActiveItemStyle = {
backgroundColor: '#d0d4da',
color: '#000',
};

const selectItems = [
{ label: 'Item 1', value: 'item1', key: '1' },
{ label: 'Item 2', value: 'item2', key: '2' },
];

const placeholder = { label: 'Select an item...', value: null };

const wrapper = shallow(
<RNPickerSelect
items={selectItems}
placeholder={placeholder}
onValueChange={() => {}}
activeItemStyle={customActiveItemStyle}
value="item2" // Select "Item 2"
/>
);

// Open the picker
wrapper.find('[testID="ios_touchable_wrapper"]').simulate('press');

// Find picker items
const pickerItems = wrapper.find('Picker').find('PickerItem');

// Ensure picker items are found
expect(pickerItems.length).toBeGreaterThan(0);

// Check if the active item has the custom styles
const activeItem = pickerItems.findWhere((item) => item.prop('value') === 'item2');

// Ensure activeItem is found
expect(activeItem.exists()).toBe(true);

// Check styles applied to the active item
expect(activeItem.prop('style')).toEqual(customActiveItemStyle);
});

});

0 comments on commit 4626a4e

Please sign in to comment.