| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602 |
134x
134x
134x
13x
268x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
13x
106x
106x
108x
108x
13x
13x
13x
8x
13x
5x
13x
13x
13x
13x
13x
13x
13x
13x
3x
3x
3x
3x
3x
| import _ from 'lodash';
import React from 'react';
import PropTypes from 'prop-types';
import { lucidClassNames } from '../../util/style-helpers';
import { buildHybridComponent } from '../../util/state-management';
import { partitionText, propsSearch } from '../../util/text-manipulation';
import {
createClass,
omitProps,
getFirst,
findTypes,
rejectTypes,
} from '../../util/component-types';
import { SearchFieldDumb as SearchField } from '../SearchField/SearchField';
import { DropMenuDumb as DropMenu } from '../DropMenu/DropMenu';
import LoadingIcon from '../Icon/LoadingIcon/LoadingIcon';
import Checkbox from '../Checkbox/Checkbox';
import Selection from '../Selection/Selection';
import * as reducers from './SearchableMultiSelect.reducers';
const {
any,
arrayOf,
bool,
func,
number,
oneOfType,
shape,
string,
oneOf,
node,
} = PropTypes;
const cx = lucidClassNames.bind('&-SearchableMultiSelect');
/**
*
* {"categories": ["controls", "selectors"], "madeFrom": ["Checkbox", "SearchField", "DropMenu", "LoadingIcon", "Selection"]}
*
* A control used to select multiple options from a dropdown list using a
* SearchField.
*/
const SearchableMultiSelect = createClass({
displayName: 'SearchableMultiSelect',
reducers,
components: {
/**
* A selectable option in the list.
*/
Option: createClass({
displayName: 'SearchableMultiSelect.Option',
propName: 'Option',
propTypes: DropMenu.Option.propTypes,
components: {
Selection: createClass({
displayName: 'SearchableMultiSelect.Option.Selection',
propName: 'Selection',
propTypes: Selection.propTypes,
}),
},
}),
/**
* Passes props through to the `SearchField` component.
*/
SearchField: createClass({
displayName: 'SearchableMultiSelect.SearchField',
propName: 'SearchField',
propTypes: SearchField.propTypes,
}),
/**
* Groups `Option`s together with a non-selectable heading.
*/
OptionGroup: createClass({
displayName: 'SearchableMultiSelect.OptionGroup',
propName: 'OptionGroup',
propTypes: DropMenu.OptionGroup.propTypes,
}),
/**
* Label for the selected section header.
*/
SelectionLabel: createClass({
displayName: 'SearchableMultiSelect.SelectionLabel',
propName: 'SelectionLabel',
}),
},
propTypes: {
/**
* Should be instances of {`SearchableMultiSelect.Option`}. Other direct child elements will not render.
*/
children: node,
/**
* Appended to the component-specific class names set on the root element.
*/
className: string,
/**
* Disables the control from being clicked or focused.
*/
isDisabled: bool,
/**
* Displays a LoadingIcon to allow for asynchronous loading of options.
*/
isLoading: bool,
/**
* The max height of the fly-out menu.
*/
maxMenuHeight: oneOfType([number, string]),
/**
* Called when the user enters a value to search for; the set of visible
* Options will be filtered using the value.
*
* Signature: `(searchText, firstVisibleIndex, {props, event}) => {}`
*
* `searchText` is the value from the `SearchField` and `firstVisibleIndex`
* is the index of the first option that will be visible after filtering.
*/
onSearch: func,
/**
* Called when an option is selected.
*
* Signature: `(optionIndex, {props, event}) => {}`
*
* `optionIndex` is the new `selectedIndex` or `null`.
*/
onSelect: func,
/**
* Called when the user clicks to remove all selections.
*
* Signature: `({props, event}) => {}`
*/
onRemoveAll: func,
/**
* The function that will be run against each Option's props to determine
* whether it should be visible or not. The default behavior of the
* function is to match, ignoring case, against any text node descendant of
* the `Option`.
*
* Signature: `(searchText, optionProps) => {}`
*
* If `true` is returned, the option will be visible. If `false`, the
* option will not be visible.
*/
optionFilter: func,
/**
* The current search text to filter the list of options by.
*/
searchText: string,
/**
* An array of currently selected `SearchableMultiSelect.Option` indices or
* `null` if nothing is selected.
*/
selectedIndices: arrayOf(number),
/**
* Object of DropMenu props which are passed through to the underlying
* DropMenu component.
*/
DropMenu: shape(DropMenu.propTypes),
/**
* *Child Element* - These are menu options. Each `Option` may be passed a
* prop called `isDisabled` to disable selection of that `Option`. Any
* other props pass to Option will be available from the `onSelect`
* handler.
*
* It also support the `Selection` prop that can be used to forward along
* props to the underlying `Selection` component.
*/
Option: any,
/**
* Adjusts the display of this component. This should typically be driven
* by screen size. Currently `small` and `large` are explicitly handled
* by this component.
*/
responsiveMode: oneOf(['small', 'medium', 'large']),
/**
* Controls the visibility of the "remove all" button that's shown with the
* selected items.
*/
hasRemoveAll: bool,
/**
* Controls the visibility of the `Selection` component that appears below
* the search field.
*/
hasSelections: bool,
},
getInitialState() {
return {
optionGroups: [],
flattenedOptionsData: [],
ungroupedOptionData: [],
optionGroupDataLookup: {},
};
},
getDefaultProps() {
return {
isDisabled: false,
isLoading: false,
onRemoveAll: _.noop,
optionFilter: propsSearch,
searchText: '',
selectedIndices: [],
DropMenu: DropMenu.getDefaultProps(),
responsiveMode: 'large',
hasRemoveAll: true,
hasSelections: true,
};
},
handleDropMenuSelect(optionIndex, { event, props }) {
const { onSelect } = this.props;
return onSelect(optionIndex, { event, props });
},
handleCheckboxSelect(
_isSelected,
{
// TODO: make sure the consumer can do callbackId somehow
event,
props: { callbackId: optionIndex },
}
) {
// This is needed otherwise clicking the checkbox will double fire this
// event _and_ the `handleDropMenuSelect` handler
event.stopPropagation();
// We don't want to send the consumer the checkbox's props so we have to
// lookup the option they clicked and send its props along
const selectedOptionProps = _.get(
findTypes(this.props, SearchableMultiSelect.Option),
`[${optionIndex}].props`
);
return this.props.onSelect(optionIndex, {
event,
props: selectedOptionProps,
});
},
handleSelectionRemove({ event, props, props: { callbackId: optionIndex } }) {
// We don't want to send the consumer the selection's props so we have to
// lookup the option they clicked and send its props along
const selectedOptionProps = _.get(
findTypes(this.props, SearchableMultiSelect.Option),
`[${optionIndex}].props`
);
return this.props.onSelect(optionIndex, {
event,
props: selectedOptionProps,
});
},
handleRemoveAll({ event }) {
this.props.onRemoveAll({ event, props: this.props });
},
handleSearch(searchText, { event }) {
const {
props,
props: { onSearch, optionFilter, DropMenu: { onExpand } },
} = this;
const options = _.map(
findTypes(props, SearchableMultiSelect.Option),
'props'
);
const firstVisibleIndex = _.findIndex(options, option => {
return optionFilter(searchText, option);
});
const firstVisibleProps = options[firstVisibleIndex];
// Just an extra call to make sure the search results show up when a user
// is typing
onExpand();
return onSearch(searchText, firstVisibleIndex, {
event,
props: firstVisibleProps,
});
},
componentWillMount() {
// preprocess the options data before rendering
this.setState(
DropMenu.preprocessOptionData(this.props, SearchableMultiSelect)
);
},
componentWillReceiveProps(nextProps) {
// only preprocess options data when it changes (via new props) - better performance than doing this each render
this.setState(
DropMenu.preprocessOptionData(nextProps, SearchableMultiSelect)
);
},
renderUnderlinedChildren(childText, searchText) {
const [pre, match, post] = partitionText(
childText,
new RegExp(_.escapeRegExp(searchText), 'i'),
searchText.length
);
return [
pre &&
<span key="pre" className={cx('&-Option-underline-pre')}>{pre}</span>,
match &&
<span key="match" className={cx('&-Option-underline-match')}>
{match}
</span>,
post &&
<span key="post" className={cx('&-Option-underline-post')}>
{post}
</span>,
];
},
renderOption({ optionProps, optionIndex }) {
const { optionFilter, searchText, selectedIndices, isLoading } = this.props;
return (
<DropMenu.Option
key={'SearchableMultiSelectOption' + optionIndex}
{..._.omit(optionProps, 'children')}
isHidden={!optionFilter(searchText, optionProps)}
isDisabled={isLoading}
>
<div className={cx('&-checkbox')}>
<Checkbox
onSelect={this.handleCheckboxSelect}
callbackId={optionIndex}
isSelected={_.includes(selectedIndices, optionIndex)}
/>
<label className={cx('&-checkbox-label')}>
{_.isString(optionProps.children)
? this.renderUnderlinedChildren(optionProps.children, searchText)
: optionProps.children}
</label>
</div>
</DropMenu.Option>
);
},
renderOptions() {
const { optionFilter, searchText, isLoading } = this.props;
const {
optionGroups,
optionGroupDataLookup,
ungroupedOptionData,
flattenedOptionsData,
} = this.state;
const isAllOptionsHidden = _.every(
flattenedOptionsData,
({ optionProps }) => !optionFilter(searchText, optionProps)
);
// for each option group passed in, render a DropMenu.OptionGroup, any label will be included in it's children, render each option inside the group
const dropMenuOptions = _.map(
optionGroups,
(optionGroupProps, optionGroupIndex) => (
<DropMenu.OptionGroup
key={'SearchableMultiSelectOptionGroup' + optionGroupIndex}
{..._.omit(optionGroupProps, 'children')}
>
{optionGroupProps.children}
{_.map(optionGroupDataLookup[optionGroupIndex], this.renderOption)}
</DropMenu.OptionGroup>
)
).concat(
// then render all the ungrouped options at the end
_.map(ungroupedOptionData, this.renderOption)
);
Eif (!isAllOptionsHidden || _.isEmpty(searchText)) {
return dropMenuOptions;
}
if (!isLoading) {
return (
<DropMenu.Option isDisabled>
<span className={cx('&-noresults')}>
No results match "{searchText}"
</span>
</DropMenu.Option>
);
}
return null;
},
render() {
const {
props,
props: {
className,
isLoading,
isDisabled,
maxMenuHeight,
selectedIndices,
DropMenu: dropMenuProps,
DropMenu: { optionContainerStyle },
responsiveMode,
searchText,
hasRemoveAll,
hasSelections,
...passThroughs
},
} = this;
const {
optionGroupDataLookup,
optionGroups,
ungroupedOptionData,
} = this.state;
const searchFieldProps = _.get(
getFirst(props, SearchableMultiSelect.SearchField),
'props',
{}
);
const selectionLabel = getFirst(
props,
SearchableMultiSelect.SelectionLabel,
<SearchableMultiSelect.SelectionLabel>
Selected
</SearchableMultiSelect.SelectionLabel>
);
const isSmall = responsiveMode === 'small';
return (
<div
{...omitProps(passThroughs, SearchableMultiSelect)}
className={cx('&', className)}
>
<DropMenu
{...dropMenuProps}
selectedIndices={null}
className={cx(
'&-DropMenu',
{
'&-DropMenu-is-small': isSmall,
},
dropMenuProps.className
)}
optionContainerStyle={_.assign(
{},
optionContainerStyle,
!_.isNil(maxMenuHeight) ? { maxHeight: maxMenuHeight } : null
)}
isDisabled={isDisabled}
isLoading={isLoading}
onSelect={this.handleDropMenuSelect}
ContextMenu={{
alignmentOffset: -13,
directonOffset: -1,
minWidthOffset: -28,
}}
>
<DropMenu.Control>
<SearchField
{...searchFieldProps}
isDisabled={isDisabled}
className={cx(
'&-search',
{
'&-search-is-small': isSmall,
},
searchFieldProps.className
)}
value={searchText}
onChange={this.handleSearch}
/>
</DropMenu.Control>
{isLoading
? <DropMenu.Option
key="SearchableMultiSelectLoading"
className={cx('&-loading')}
isDisabled
>
<LoadingIcon />
</DropMenu.Option>
: null}
{this.renderOptions()}
</DropMenu>
{hasSelections && !_.isEmpty(selectedIndices)
? <Selection
className={cx('&-Selection-section')}
isBold
hasBackground
kind="container"
onRemove={this.handleRemoveAll}
responsiveMode={responsiveMode}
isRemovable={hasRemoveAll}
>
<Selection.Label>
{selectionLabel.props.children}
</Selection.Label>
{_.map(
optionGroupDataLookup,
(groupedOptionsData, optionGroupIndex) => {
const selectedGroupedOptions = _.filter(groupedOptionsData, ({
optionIndex,
}) => _.includes(selectedIndices, optionIndex));
if (!_.isEmpty(selectedGroupedOptions)) {
return (
<Selection
className={cx('&-Selection-group')}
key={'optionGroup-' + optionGroupIndex}
responsiveMode={responsiveMode}
isRemovable={false}
isBold
kind="container"
>
<Selection.Label>
{_.first(
rejectTypes(
optionGroups[optionGroupIndex].children,
SearchableMultiSelect.Option
)
)}
</Selection.Label>
{_.map(selectedGroupedOptions, ({
optionIndex,
optionProps,
}) => {
const selectionProps = _.get(
getFirst(
optionProps,
SearchableMultiSelect.Option.Selection
),
'props'
);
return (
<Selection
key={optionIndex}
{...selectionProps}
callbackId={optionIndex}
responsiveMode={responsiveMode}
onRemove={this.handleSelectionRemove}
>
<Selection.Label>
{optionProps.children}
</Selection.Label>
</Selection>
);
})}
</Selection>
);
}
return null;
}
)}
{_.map(selectedIndices, selectedIndex => {
const selectedUngroupedOptionData = _.find(
ungroupedOptionData,
{ optionIndex: selectedIndex }
);
Eif (selectedUngroupedOptionData) {
const { optionProps } = selectedUngroupedOptionData;
const selectionProps = _.get(
getFirst(
optionProps,
SearchableMultiSelect.Option.Selection
),
'props'
);
return (
<Selection
key={selectedIndex}
{...selectionProps}
callbackId={selectedIndex}
responsiveMode={responsiveMode}
onRemove={this.handleSelectionRemove}
>
<Selection.Label>
{optionProps.children}
</Selection.Label>
</Selection>
);
}
return null;
})}
</Selection>
: null}
</div>
);
},
});
export default buildHybridComponent(SearchableMultiSelect);
export { SearchableMultiSelect as SearchableMultiSelectDumb };
|