| 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 |
13x
13x
13x
41x
41x
41x
1x
1x
11x
11x
29x
30x
30x
30x
4x
1x
1x
26x
70x
26x
72x
12x
12x
12x
12x
12x
4x
4x
4x
4x
4x
5x
6x
6x
6x
6x
7x
7x
7x
1x
6x
7x
7x
7x
6x
1x
1x
1x
5x
5x
5x
5x
5x
6x
6x
6x
54x
54x
54x
54x
54x
54x
12x
54x
57x
3x
10x
54x
54x
89x
89x
| /**
* @category controls
* @component ComboBox
* @variations collab-ui-react
*/
import React from 'react';
import PropTypes from 'prop-types';
import {
EventOverlay,
Input,
ListItem,
SearchInput,
} from '@collab-ui/react';
import { omit, uniqueId } from 'lodash';
class ComboBox extends React.Component {
static displayName = 'ComboBox';
state = {
filteredOptions: [],
focus: -1,
id: this.props.id || uniqueId('cui-combo-box-'),
isOpen: false,
value: '',
};
componentDidMount() {
const { children } = this.props;
this.options =
(children && React.Children.toArray(children))
|| this.mapOptionsToListItem();
this.setFilteredOptions();
}
componentDidUpdate(prevProps) {
const { options, children } = this.props;
const { value } = this.state;
if (
(prevProps.options !== options)
|| (prevProps.children !== children)
) {
this.options =
(children && React.Children.toArray(children))
|| this.mapOptionsToListItem();
this.setFilteredOptions(value);
}
}
mapOptionsToListItem = () => {
const { options } = this.props;
return options.map((option, i) =>
<ListItem key={i} label={option} />
);
}
setFilteredOptions = filter => {
const { onChange } = this.props;
const filteredOptions = !onChange
? this.applyFilter(filter)
: this.options;
this.setState({
isOpen: !!filteredOptions.length,
filteredOptions,
});
}
hidePopover = () => {
this.setState({
isOpen: false
});
};
handleToggle = () => {
const { filteredOptions } = this.state;
filteredOptions.length
&& this.setState({ isOpen: true });
};
applyFilter = value => {
const { searchProp } = this.props;
const isSubString = string => value && string.toLowerCase().includes(value.toLowerCase());
return this.options.filter(option =>
(option.props[searchProp] && isSubString(option.props[searchProp]))
|| ['ListItemHeader'].includes(option.type.displayName)
);
};
handleChange = e => {
const { onChange } = this.props;
const { focus } = this.state;
this.setFilteredOptions(e.target.value);
this.setState({
value: e.target.value,
focus: !onChange ? -1 : focus,
}, () => onChange && onChange(e, e.target.value));
};
handleClick = (e, selectedOption) => {
const { searchProp } = this.props;
const { onSelect } = this.props;
this.setFilteredOptions(selectedOption.props[searchProp]);
this.setState({
value: selectedOption.props[searchProp],
isOpen: false,
focus: -1,
}, () => onSelect && onSelect(e, selectedOption));
};
setFocus = index => {
this.setState({ focus: index });
};
handleKeyDown = e => {
let flag = false;
let newIndex;
const { filteredOptions, focus, isOpen } = this.state;
const length = filteredOptions && filteredOptions.length - 1;
const getNewIndex = (currentIndex, change) => {
const getPossibleIndex = () => {
Iif (currentIndex + change < 0) {
return length;
} else if (currentIndex + change > length) {
return 0;
}
return currentIndex + change;
};
const possibleIndex = getPossibleIndex();
const potentialTarget = React.Children.toArray(filteredOptions)[possibleIndex];
return (potentialTarget.props.disabled || potentialTarget.props.isReadOnly)
? getNewIndex(possibleIndex, change)
: possibleIndex;
};
switch (e.which) {
case 13:
isOpen
&& (focus !== -1)
&& this.handleClick(e, filteredOptions[focus]);
flag = true;
break;
case 38:
if(isOpen) {
newIndex = getNewIndex(focus, -1);
this.setFocus(newIndex);
}
flag = true;
break;
case 40:
Eif(isOpen) {
newIndex = getNewIndex(focus, 1);
this.setFocus(newIndex);
}
flag = true;
break;
default:
break;
}
Eif (flag) {
e.stopPropagation();
e.preventDefault();
}
};
render() {
const {
className,
clear,
disabled,
hasSearchIcon,
inputProps,
placeholder,
theme,
...props
} = this.props;
const otherProps = omit({...props}, [
'children',
'id',
'onChange',
'onSelect',
'options',
'searchProp',
]);
const {
filteredOptions,
focus,
id,
isOpen,
value,
} = this.state;
const activeDescendant = this.activeChild && this.activeChild.id;
const InputComp = hasSearchIcon ? SearchInput : Input;
const input = (
<InputComp
aria-autocomplete='list'
clear={clear}
disabled={disabled}
inputRef={ref => this.anchorNode = ref}
onChange={this.handleChange}
onClick={this.handleToggle}
onKeyDown={this.handleKeyDown}
placeholder={placeholder}
theme={theme}
value={value}
{...activeDescendant && { 'aria-activedescendant': activeDescendant }}
{...inputProps}
/>
);
const renderFilteredOption = filteredOptions
&& filteredOptions.map((option, i) =>
React.cloneElement(option, {
active: i === focus,
key: i,
onClick: e => this.handleClick(e, option),
refName: 'option',
role: 'option',
...focus === i && { ref: ref => this.activeChild = ref },
})
);
const dropdownElement = (
this.anchorNode &&
<EventOverlay
allowClickAway
anchorNode={this.anchorNode}
close={this.hidePopover}
isOpen={isOpen}
{...otherProps}
>
<div
className='cui-combo-box__options'
id={id}
role='listbox'
{...this.anchorNode &&
{
style: {
width: this.anchorNode.getBoundingClientRect().width
}
}
}
>
{renderFilteredOption}
</div>
</EventOverlay>
);
return (
<div
aria-controls={id}
aria-haspopup='listbox'
aria-expanded={isOpen}
className={
'cui-combo-box' +
`${(className && ` ${className}`) || ''}`
}
role='combobox'
>
{input}
{dropdownElement}
</div>
);
}
}
ComboBox.propTypes = {
/** @prop Children nodes to render inside ComboBox | null */
children: PropTypes.node,
/** @prop Optional css class string | '' */
className: PropTypes.string,
/** @prop Sets the initial input element as empty | false */
clear: PropTypes.bool,
/** @prop Sets the attribute disabled to the ComboBox | false */
disabled: PropTypes.bool,
/** @prop Sets the ComboBox to have a search icon | true */
hasSearchIcon: PropTypes.bool,
/** @prop Sets the ID of the ComboBox */
id: PropTypes.string,
/** @prop Collection of props unique for Input element | null */
inputProps: PropTypes.shape({}),
/** @prop Handler invoked when the user presses any key | null */
onChange: PropTypes.func,
/** @prop Handler invoked when the user selects the ComboBox | null */
onSelect: PropTypes.func,
/** @prop Array of options for the ComboBox dropdown | [] */
options: PropTypes.arrayOf(PropTypes.string),
/** @prop Text that initially populates the input field for guidence | '' */
placeholder: PropTypes.string,
/** @prop Sets the search prop | 'label' */
searchProp: PropTypes.string,
/** @prop Sets the target offset | { horizontal: 0, vertical: 4 } */
targetOffset: PropTypes.shape({
horizontal: PropTypes.number,
vertical: PropTypes.number,
}),
/** @prop Sets the color theme of the ComboBox | '' */
theme: PropTypes.string,
};
ComboBox.defaultProps = {
children: null,
className: '',
clear: false,
disabled: false,
hasSearchIcon: true,
id: null,
inputProps: null,
onChange: null,
onSelect: null,
options: [],
placeholder: '',
searchProp: 'label',
targetOffset: {
horizontal: 0,
vertical: 4,
},
theme: '',
};
export default ComboBox;
/**
* @component combo-box
* @section default
* @react
import { ComboBox } from '@collab-ui/react';
export default class ComboBoxDefault extends React.PureComponent {
render() {
return (
<ComboBox options={['a', 'ab', 'abc']} />
);
}
}
**/
/**
* @component combo-box
* @section clear
* @react
import { ComboBox } from '@collab-ui/react';
export default class ComboBoxClear extends React.PureComponent {
render() {
return (
<ComboBox
options={['a', 'ab', 'abc']}
clear
/>
);
}
}
**/
|