Skip to content

Commit 31f7862

Browse files
authoredMar 22, 2024··
Merge pull request #543 from lawnstarter/PE-36722
PE-36722: Adjust comparison for getSelectedItem
2 parents 99d6bfa + 8fdc35a commit 31f7862

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed
 

‎package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"/src"
2525
],
2626
"dependencies": {
27-
"lodash.isequal": "^4.5.0"
27+
"lodash.isequal": "^4.5.0",
28+
"lodash.isobject": "^3.0.2"
2829
},
2930
"devDependencies": {
3031
"@react-native-picker/picker": "^2.4.0",

‎src/index.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import { Picker } from '@react-native-picker/picker';
2+
import isEqual from 'lodash.isequal';
3+
import isObject from 'lodash.isobject';
4+
import PropTypes from 'prop-types';
15
import React, { PureComponent } from 'react';
26
import { Keyboard, Modal, Platform, Text, TextInput, TouchableOpacity, View } from 'react-native';
3-
import PropTypes from 'prop-types';
4-
import isEqual from 'lodash.isequal';
5-
import { Picker } from '@react-native-picker/picker';
67
import { defaultStyles } from './styles';
78

89
export default class RNPickerSelect extends PureComponent {
@@ -102,7 +103,11 @@ export default class RNPickerSelect extends PureComponent {
102103
if (item.key && key) {
103104
return isEqual(item.key, key);
104105
}
105-
return isEqual(item.value, value);
106+
if (isObject(item.value) || isObject(value)) {
107+
return isEqual(item.value, value);
108+
}
109+
// convert to string to make sure types match
110+
return isEqual(String(item.value), String(value));
106111
});
107112
if (idx === -1) {
108113
idx = 0;

‎test/test.js

+42-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { Platform, Keyboard, View } from 'react-native';
2+
import { Keyboard, Platform, View } from 'react-native';
33
import RNPickerSelect from '../src';
44

55
const selectItems = [
@@ -29,6 +29,33 @@ const selectItems = [
2929
},
3030
];
3131

32+
const objectSelectItems = [
33+
{
34+
label: 'Red',
35+
value: { label: 'red' },
36+
},
37+
{
38+
label: 'Orange',
39+
value: { label: 'orange' },
40+
},
41+
{
42+
label: 'Yellow',
43+
value: { label: 'yellow' },
44+
},
45+
{
46+
label: 'Green',
47+
value: { label: 'green' },
48+
},
49+
{
50+
label: 'Blue',
51+
value: { label: 'blue' },
52+
},
53+
{
54+
label: 'Indigo',
55+
value: { label: 'indigo' },
56+
},
57+
];
58+
3259
const violet = { label: 'Violet', value: 'violet' };
3360

3461
const placeholder = {
@@ -134,6 +161,20 @@ describe('RNPickerSelect', () => {
134161
expect(onValueChangeSpy).toHaveBeenCalledWith('orange', 2);
135162
});
136163

164+
it('should return the expected option to a callback passed into onSelect when the value is an object', () => {
165+
const onValueChangeSpy = jest.fn();
166+
const wrapper = shallow(
167+
<RNPickerSelect
168+
items={objectSelectItems}
169+
placeholder={placeholder}
170+
onValueChange={onValueChangeSpy}
171+
/>
172+
);
173+
174+
wrapper.find('[testID="ios_picker"]').props().onValueChange(objectSelectItems[5].value, 5);
175+
expect(onValueChangeSpy).toHaveBeenCalledWith(objectSelectItems[5].value, 5);
176+
});
177+
137178
it('should show the picker when pressed', () => {
138179
const wrapper = shallow(
139180
<RNPickerSelect items={selectItems} placeholder={placeholder} onValueChange={noop} />

‎yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -5719,6 +5719,11 @@ lodash.isequal@^4.5.0:
57195719
resolved "https://registry.yarnpkg.com/lodash.isequal/-/lodash.isequal-4.5.0.tgz#415c4478f2bcc30120c22ce10ed3226f7d3e18e0"
57205720
integrity sha1-QVxEePK8wwEgwizhDtMib30+GOA=
57215721

5722+
lodash.isobject@^3.0.2:
5723+
version "3.0.2"
5724+
resolved "https://registry.yarnpkg.com/lodash.isobject/-/lodash.isobject-3.0.2.tgz#3c8fb8d5b5bf4bf90ae06e14f2a530a4ed935e1d"
5725+
integrity sha512-3/Qptq2vr7WeJbB4KHUSKlq8Pl7ASXi3UG6CMbBm8WRtXi8+GHm7mKaU3urfpSEzWe2wCIChs6/sdocUsTKJiA==
5726+
57225727
lodash.merge@^4.6.2:
57235728
version "4.6.2"
57245729
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"

0 commit comments

Comments
 (0)
Please sign in to comment.