Skip to content

Commit 13ca81b

Browse files
mendyEdriethanshar
andauthored
Picker multi select support (#871)
* pass dialog events to picker custom model to support multi-selection * refactor code to make label text decision more intuitive * Made label source selection more intuitive Co-authored-by: Ethan Sharabi <[email protected]>
1 parent b0723ee commit 13ca81b

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

src/components/picker/index.js

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,33 @@ class Picker extends BaseComponent {
183183
return items;
184184
}
185185

186-
getLabel() {
187-
const {value} = this.state;
188-
const {getLabel, getItemLabel} = this.props;
186+
shouldNotChangePickerLabelWhileSelecting = () => {
187+
const {mode} = this.props;
188+
return mode === Picker.modes.MULTI;
189+
}
190+
191+
getLabelValueText = () => {
192+
const {value: propsValue} = this.props;
193+
const {value: stateValue} = this.props;
194+
if (this.shouldNotChangePickerLabelWhileSelecting()) {
195+
return this.getLabel(propsValue);
196+
}
197+
return this.getLabel(stateValue);
198+
}
199+
200+
getLabelsFromArray = (value) => {
201+
const {getItemLabel} = this.props;
202+
return _.chain(value)
203+
.map(getItemLabel || 'label')
204+
.join(', ')
205+
.value();
206+
};
207+
208+
getLabel(value) {
209+
const {getLabel} = this.props;
189210

190211
if (_.isArray(value)) {
191-
return _.chain(value)
192-
.map(getItemLabel || 'label')
193-
.join(', ')
194-
.value();
212+
return this.getLabelsFromArray(value);
195213
}
196214

197215
if (_.isFunction(getLabel)) {
@@ -206,7 +224,7 @@ class Picker extends BaseComponent {
206224
const {items} = this.state;
207225
const selectedItem = _.find(items, {value});
208226
return _.get(selectedItem, 'label');
209-
}
227+
};
210228

211229
handlePickerOnPress = () => {
212230
this.toggleExpandableModal(true);
@@ -260,7 +278,7 @@ class Picker extends BaseComponent {
260278

261279
clearSearchField = () => {
262280
this.setState({searchValue: ''});
263-
}
281+
};
264282

265283
appendPropsToChildren = () => {
266284
const {children, mode, getItemValue, getItemLabel, showSearch, renderItem} = this.props;
@@ -301,17 +319,18 @@ class Picker extends BaseComponent {
301319
listProps,
302320
testID
303321
} = this.getThemeProps();
304-
const {showExpandableModal, selectedItemPosition} = this.state;
322+
const {showExpandableModal, selectedItemPosition, value} = this.state;
305323
const children = this.appendPropsToChildren(this.props.children);
306324

307325
if (renderCustomModal) {
308326
const modalProps = {
309327
visible: showExpandableModal,
310328
toggleModal: this.toggleExpandableModal,
311329
onSearchChange: this.onSearchChange,
312-
children
330+
children,
331+
onDone: () => this.onDoneSelecting(value),
332+
onCancel: this.cancelSelect
313333
};
314-
315334
return renderCustomModal(modalProps);
316335
}
317336

@@ -324,7 +343,7 @@ class Picker extends BaseComponent {
324343
topBarProps={{
325344
...topBarProps,
326345
onCancel: this.cancelSelect,
327-
onDone: mode === Picker.modes.MULTI ? () => this.onDoneSelecting(this.state.value) : undefined
346+
onDone: mode === Picker.modes.MULTI ? () => this.onDoneSelecting(value) : undefined
328347
}}
329348
showSearch={showSearch}
330349
searchStyle={searchStyle}
@@ -358,7 +377,7 @@ class Picker extends BaseComponent {
358377
}
359378

360379
const textInputProps = TextField.extractOwnProps(this.getThemeProps());
361-
const label = this.getLabel();
380+
const label = this.getLabelValueText();
362381
return (
363382
<TextField
364383
{...textInputProps}

0 commit comments

Comments
 (0)