Skip to content

Commit d3af6be

Browse files
authored
Fix #848 - Fix issue with Picker in mulitselect mode with custom value object structure (#863)
1 parent 794d2a1 commit d3af6be

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

demo/src/screens/componentScreens/PickerScreen.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ export default class PickerScreen extends Component {
109109
onChange={items => this.setState({languages: items})}
110110
mode={Picker.modes.MULTI}
111111
rightIconSource={dropdown}
112-
hideUnderline
113112
>
114113
{_.map(options, option => (
115114
<Picker.Item key={option.value} value={option} disabled={option.disabled}/>

src/components/picker/index.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,13 @@ class Picker extends BaseComponent {
7777
*/
7878
onPress: PropTypes.func,
7979
/**
80-
* A function that extract the unique value out of the value prop in case value has a custom structure
80+
* A function that extract the unique value out of the value prop in case value has a custom structure (e.g. {myValue, myLabel})
8181
*/
8282
getItemValue: PropTypes.func,
83+
/**
84+
* A function that extract the label out of the value prop in case value has a custom structure (e.g. {myValue, myLabel})
85+
*/
86+
getItemLabel: PropTypes.func,
8387
/**
8488
* A function that returns the label to show for the selected Picker value
8589
*/
@@ -181,15 +185,15 @@ class Picker extends BaseComponent {
181185

182186
getLabel() {
183187
const {value} = this.state;
188+
const {getLabel, getItemLabel} = this.props;
184189

185190
if (_.isArray(value)) {
186191
return _.chain(value)
187-
.map('label')
192+
.map(getItemLabel || 'label')
188193
.join(', ')
189194
.value();
190195
}
191196

192-
const {getLabel} = this.props;
193197
if (_.isFunction(getLabel)) {
194198
return getLabel(value);
195199
}
@@ -214,8 +218,9 @@ class Picker extends BaseComponent {
214218
};
215219

216220
toggleItemSelection = item => {
221+
const {getItemValue} = this.props;
217222
const {value} = this.state;
218-
const newValue = _.xorBy(value, [item], 'value');
223+
const newValue = _.xorBy(value, [item], getItemValue || 'value');
219224
this.setState({
220225
value: newValue
221226
});
@@ -251,7 +256,7 @@ class Picker extends BaseComponent {
251256
};
252257

253258
appendPropsToChildren = () => {
254-
const {children, mode, getItemValue, showSearch, renderItem} = this.props;
259+
const {children, mode, getItemValue, getItemLabel, showSearch, renderItem} = this.props;
255260
const {value, searchValue} = this.state;
256261
const childrenWithProps = React.Children.map(children, child => {
257262
const childValue = PickerPresenter.getItemValue({getItemValue, ...child.props});
@@ -264,6 +269,7 @@ class Picker extends BaseComponent {
264269
isSelected,
265270
onPress: mode === Picker.modes.MULTI ? this.toggleItemSelection : this.onDoneSelecting,
266271
getItemValue: child.props.getItemValue || getItemValue,
272+
getItemLabel: child.props.getItemLabel || getItemLabel,
267273
onSelectedLayout: this.onSelectedItemLayout,
268274
renderItem: child.props.renderItem || renderItem,
269275
accessibilityState: isSelected ? {selected: true} : undefined,

0 commit comments

Comments
 (0)