@@ -43,10 +43,7 @@ export default class Dropdown extends Component {
43
43
as : customPropTypes . as ,
44
44
45
45
/** Label prefixed to an option added by a user. */
46
- additionLabel : PropTypes . oneOfType ( [
47
- PropTypes . element ,
48
- PropTypes . string ,
49
- ] ) ,
46
+ additionLabel : PropTypes . oneOfType ( [ PropTypes . element , PropTypes . string ] ) ,
50
47
51
48
/** Position of the `Add: ...` option in the dropdown list ('top' or 'bottom'). */
52
49
additionPosition : PropTypes . oneOf ( [ 'top' , 'bottom' ] ) ,
@@ -55,10 +52,7 @@ export default class Dropdown extends Component {
55
52
* Allow user additions to the list of options (boolean).
56
53
* Requires the use of `selection`, `options` and `search`.
57
54
*/
58
- allowAdditions : customPropTypes . every ( [
59
- customPropTypes . demand ( [ 'options' , 'selection' , 'search' ] ) ,
60
- PropTypes . bool ,
61
- ] ) ,
55
+ allowAdditions : customPropTypes . every ( [ customPropTypes . demand ( [ 'options' , 'selection' , 'search' ] ) , PropTypes . bool ] ) ,
62
56
63
57
/** A Dropdown can reduce its complexity. */
64
58
basic : PropTypes . bool ,
@@ -69,10 +63,7 @@ export default class Dropdown extends Component {
69
63
/** Primary content. */
70
64
children : customPropTypes . every ( [
71
65
customPropTypes . disallow ( [ 'options' , 'selection' ] ) ,
72
- customPropTypes . givenProps (
73
- { children : PropTypes . any . isRequired } ,
74
- PropTypes . element . isRequired ,
75
- ) ,
66
+ customPropTypes . givenProps ( { children : PropTypes . any . isRequired } , PropTypes . element . isRequired ) ,
76
67
] ) ,
77
68
78
69
/** Additional classes. */
@@ -103,20 +94,14 @@ export default class Dropdown extends Component {
103
94
/** Currently selected label in multi-select. */
104
95
defaultSelectedLabel : customPropTypes . every ( [
105
96
customPropTypes . demand ( [ 'multiple' ] ) ,
106
- PropTypes . oneOfType ( [
107
- PropTypes . number ,
108
- PropTypes . string ,
109
- ] ) ,
97
+ PropTypes . oneOfType ( [ PropTypes . number , PropTypes . string ] ) ,
110
98
] ) ,
111
99
112
100
/** Initial value or value array if multiple. */
113
101
defaultValue : PropTypes . oneOfType ( [
114
102
PropTypes . number ,
115
103
PropTypes . string ,
116
- PropTypes . arrayOf ( PropTypes . oneOfType ( [
117
- PropTypes . string ,
118
- PropTypes . number ,
119
- ] ) ) ,
104
+ PropTypes . arrayOf ( PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ) ,
120
105
] ) ,
121
106
122
107
/** A dropdown menu can open to the left or to the right. */
@@ -138,10 +123,7 @@ export default class Dropdown extends Component {
138
123
header : PropTypes . node ,
139
124
140
125
/** Shorthand for Icon. */
141
- icon : PropTypes . oneOfType ( [
142
- PropTypes . node ,
143
- PropTypes . object ,
144
- ] ) ,
126
+ icon : PropTypes . oneOfType ( [ PropTypes . node , PropTypes . object ] ) ,
145
127
146
128
/** A dropdown can be formatted to appear inline in other content. */
147
129
inline : PropTypes . bool ,
@@ -283,17 +265,10 @@ export default class Dropdown extends Component {
283
265
* A selection dropdown can allow a user to search through a large list of choices.
284
266
* Pass a function here to replace the default search.
285
267
*/
286
- search : PropTypes . oneOfType ( [
287
- PropTypes . bool ,
288
- PropTypes . func ,
289
- ] ) ,
268
+ search : PropTypes . oneOfType ( [ PropTypes . bool , PropTypes . func ] ) ,
290
269
291
270
/** A shorthand for a search input. */
292
- searchInput : PropTypes . oneOfType ( [
293
- PropTypes . array ,
294
- PropTypes . node ,
295
- PropTypes . object ,
296
- ] ) ,
271
+ searchInput : PropTypes . oneOfType ( [ PropTypes . array , PropTypes . node , PropTypes . object ] ) ,
297
272
298
273
/** Current value of searchQuery. Creates a controlled component. */
299
274
searchQuery : PropTypes . string ,
@@ -312,10 +287,7 @@ export default class Dropdown extends Component {
312
287
/** Currently selected label in multi-select. */
313
288
selectedLabel : customPropTypes . every ( [
314
289
customPropTypes . demand ( [ 'multiple' ] ) ,
315
- PropTypes . oneOfType ( [
316
- PropTypes . string ,
317
- PropTypes . number ,
318
- ] ) ,
290
+ PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ,
319
291
] ) ,
320
292
321
293
/** A dropdown can be used to select between choices in a form. */
@@ -329,30 +301,20 @@ export default class Dropdown extends Component {
329
301
simple : PropTypes . bool ,
330
302
331
303
/** A dropdown can receive focus. */
332
- tabIndex : PropTypes . oneOfType ( [
333
- PropTypes . number ,
334
- PropTypes . string ,
335
- ] ) ,
304
+ tabIndex : PropTypes . oneOfType ( [ PropTypes . number , PropTypes . string ] ) ,
336
305
337
306
/** The text displayed in the dropdown, usually for the active item. */
338
307
text : PropTypes . string ,
339
308
340
309
/** Custom element to trigger the menu to become visible. Takes place of 'text'. */
341
- trigger : customPropTypes . every ( [
342
- customPropTypes . disallow ( [ 'selection' , 'text' ] ) ,
343
- PropTypes . node ,
344
- ] ) ,
310
+ trigger : customPropTypes . every ( [ customPropTypes . disallow ( [ 'selection' , 'text' ] ) , PropTypes . node ] ) ,
345
311
346
312
/** Current value or value array if multiple. Creates a controlled component. */
347
313
value : PropTypes . oneOfType ( [
348
314
PropTypes . bool ,
349
315
PropTypes . string ,
350
316
PropTypes . number ,
351
- PropTypes . arrayOf ( PropTypes . oneOfType ( [
352
- PropTypes . bool ,
353
- PropTypes . string ,
354
- PropTypes . number ,
355
- ] ) ) ,
317
+ PropTypes . arrayOf ( PropTypes . oneOfType ( [ PropTypes . bool , PropTypes . string , PropTypes . number ] ) ) ,
356
318
] ) ,
357
319
358
320
/** A dropdown can open upward. */
@@ -381,12 +343,7 @@ export default class Dropdown extends Component {
381
343
wrapSelection : true ,
382
344
}
383
345
384
- static autoControlledProps = [
385
- 'open' ,
386
- 'searchQuery' ,
387
- 'selectedLabel' ,
388
- 'value' ,
389
- ]
346
+ static autoControlledProps = [ 'open' , 'searchQuery' , 'selectedLabel' , 'value' ]
390
347
391
348
static _meta = {
392
349
name : 'Dropdown' ,
@@ -430,12 +387,12 @@ export default class Dropdown extends Component {
430
387
if ( hasValue && nextProps . multiple && ! isNextValueArray ) {
431
388
console . error (
432
389
'Dropdown `value` must be an array when `multiple` is set.' +
433
- ` Received type: \`${ Object . prototype . toString . call ( nextProps . value ) } \`.` ,
390
+ ` Received type: \`${ Object . prototype . toString . call ( nextProps . value ) } \`.` ,
434
391
)
435
392
} else if ( hasValue && ! nextProps . multiple && isNextValueArray ) {
436
393
console . error (
437
394
'Dropdown `value` must not be an array when `multiple` is not set.' +
438
- ' Either set `multiple={true}` or use a string or number value.' ,
395
+ ' Either set `multiple={true}` or use a string or number value.' ,
439
396
)
440
397
}
441
398
}
@@ -456,7 +413,8 @@ export default class Dropdown extends Component {
456
413
return ! shallowEqual ( nextProps , this . props ) || ! shallowEqual ( nextState , this . state )
457
414
}
458
415
459
- componentDidUpdate ( prevProps , prevState ) { // eslint-disable-line complexity
416
+ componentDidUpdate ( prevProps , prevState ) {
417
+ // eslint-disable-line complexity
460
418
debug ( 'componentDidUpdate()' )
461
419
debug ( 'to state:' , objectDiff ( prevState , this . state ) )
462
420
@@ -465,7 +423,7 @@ export default class Dropdown extends Component {
465
423
debug ( 'dropdown focused' )
466
424
if ( ! this . isMouseDown ) {
467
425
const { minCharacters, openOnFocus, search } = this . props
468
- const openable = ! search || ( search && minCharacters === 1 )
426
+ const openable = ! search || ( search && minCharacters === 1 && ! this . state . open )
469
427
470
428
debug ( 'mouse is not down, opening' )
471
429
if ( openOnFocus && openable ) this . open ( )
@@ -500,11 +458,7 @@ export default class Dropdown extends Component {
500
458
} else if ( prevState . open && ! this . state . open ) {
501
459
debug ( 'dropdown closed' )
502
460
this . handleClose ( )
503
- eventStack . unsub ( 'keydown' , [
504
- this . closeOnEscape ,
505
- this . moveSelectionOnKeyDown ,
506
- this . selectItemOnEnter ,
507
- ] )
461
+ eventStack . unsub ( 'keydown' , [ this . closeOnEscape , this . moveSelectionOnKeyDown , this . selectItemOnEnter ] )
508
462
eventStack . unsub ( 'click' , this . closeOnDocumentClick )
509
463
if ( ! this . state . focus ) {
510
464
eventStack . unsub ( 'keydown' , this . removeItemOnBackspace )
@@ -539,9 +493,7 @@ export default class Dropdown extends Component {
539
493
540
494
closeOnChange = ( e ) => {
541
495
const { closeOnChange, multiple } = this . props
542
- const shouldClose = _ . isUndefined ( closeOnChange )
543
- ? ! multiple
544
- : closeOnChange
496
+ const shouldClose = _ . isUndefined ( closeOnChange ) ? ! multiple : closeOnChange
545
497
546
498
if ( shouldClose ) this . close ( e )
547
499
}
@@ -831,8 +783,7 @@ export default class Dropdown extends Component {
831
783
832
784
const re = new RegExp ( _ . escapeRegExp ( strippedQuery ) , 'i' )
833
785
834
- filteredOptions = _ . filter ( filteredOptions , opt =>
835
- re . test ( deburr ? _ . deburr ( opt . text ) : opt . text ) )
786
+ filteredOptions = _ . filter ( filteredOptions , opt => re . test ( deburr ? _ . deburr ( opt . text ) : opt . text ) )
836
787
}
837
788
}
838
789
@@ -846,10 +797,7 @@ export default class Dropdown extends Component {
846
797
key : 'addition' ,
847
798
// by using an array, we can pass multiple elements, but when doing so
848
799
// we must specify a `key` for React to know which one is which
849
- text : [
850
- additionLabelElement ,
851
- < b key = 'addition-query' > { searchQuery } </ b > ,
852
- ] ,
800
+ text : [ additionLabelElement , < b key = 'addition-query' > { searchQuery } </ b > ] ,
853
801
value : searchQuery ,
854
802
className : 'addition' ,
855
803
'data-additional' : true ,
@@ -871,10 +819,14 @@ export default class Dropdown extends Component {
871
819
getEnabledIndices = ( givenOptions ) => {
872
820
const options = givenOptions || this . getMenuOptions ( )
873
821
874
- return _ . reduce ( options , ( memo , item , index ) => {
875
- if ( ! item . disabled ) memo . push ( index )
876
- return memo
877
- } , [ ] )
822
+ return _ . reduce (
823
+ options ,
824
+ ( memo , item , index ) => {
825
+ if ( ! item . disabled ) memo . push ( index )
826
+ return memo
827
+ } ,
828
+ [ ] ,
829
+ )
878
830
}
879
831
880
832
getItemByValue = ( value ) => {
@@ -944,9 +896,7 @@ export default class Dropdown extends Component {
944
896
// Select the currently active item, if none, use the first item.
945
897
// Multiple selects remove active items from the list,
946
898
// their initial selected index should be 0.
947
- newSelectedIndex = multiple
948
- ? firstIndex
949
- : this . getMenuItemIndexByValue ( value , options ) || enabledIndicies [ 0 ]
899
+ newSelectedIndex = multiple ? firstIndex : this . getMenuItemIndexByValue ( value , options ) || enabledIndicies [ 0 ]
950
900
} else if ( multiple ) {
951
901
// multiple selects remove options from the menu as they are made active
952
902
// keep the selected index within range of the remaining items
@@ -1096,12 +1046,13 @@ export default class Dropdown extends Component {
1096
1046
debug ( `menu: ${ menu } ` )
1097
1047
debug ( `item: ${ item } ` )
1098
1048
const isOutOfUpperView = item . offsetTop < menu . scrollTop
1099
- const isOutOfLowerView = ( item . offsetTop + item . clientHeight ) > menu . scrollTop + menu . clientHeight
1049
+ const isOutOfLowerView = item . offsetTop + item . clientHeight > menu . scrollTop + menu . clientHeight
1100
1050
1101
1051
if ( isOutOfUpperView ) {
1102
1052
menu . scrollTop = item . offsetTop
1103
1053
} else if ( isOutOfLowerView ) {
1104
- menu . scrollTop = ( item . offsetTop + item . clientHeight ) - menu . clientHeight
1054
+ // eslint-disable-next-line no-mixed-operators
1055
+ menu . scrollTop = item . offsetTop + item . clientHeight - menu . clientHeight
1105
1056
}
1106
1057
}
1107
1058
@@ -1152,15 +1103,9 @@ export default class Dropdown extends Component {
1152
1103
renderText = ( ) => {
1153
1104
const { multiple, placeholder, search, text } = this . props
1154
1105
const { searchQuery, value, open } = this . state
1155
- const hasValue = multiple
1156
- ? ! _ . isEmpty ( value )
1157
- : ! _ . isNil ( value ) && value !== ''
1106
+ const hasValue = multiple ? ! _ . isEmpty ( value ) : ! _ . isNil ( value ) && value !== ''
1158
1107
1159
- const classes = cx (
1160
- placeholder && ! hasValue && 'default' ,
1161
- 'text' ,
1162
- search && searchQuery && 'filtered' ,
1163
- )
1108
+ const classes = cx ( placeholder && ! hasValue && 'default' , 'text' , search && searchQuery && 'filtered' )
1164
1109
let _text = placeholder
1165
1110
if ( searchQuery ) {
1166
1111
_text = null
@@ -1172,21 +1117,27 @@ export default class Dropdown extends Component {
1172
1117
_text = _ . get ( this . getItemByValue ( value ) , 'text' )
1173
1118
}
1174
1119
1175
- return < div className = { classes } role = 'alert' aria-live = 'polite' > { _text } </ div >
1120
+ return (
1121
+ < div className = { classes } role = 'alert' aria-live = 'polite' >
1122
+ { _text }
1123
+ </ div >
1124
+ )
1176
1125
}
1177
1126
1178
1127
renderSearchInput = ( ) => {
1179
1128
const { search, searchInput } = this . props
1180
1129
const { searchQuery } = this . state
1181
1130
1182
1131
if ( ! search ) return null
1183
- return DropdownSearchInput . create ( searchInput , { defaultProps : {
1184
- inputRef : this . handleSearchRef ,
1185
- onChange : this . handleSearchChange ,
1186
- style : { width : this . computeSearchInputWidth ( ) } ,
1187
- tabIndex : this . computeSearchInputTabIndex ( ) ,
1188
- value : searchQuery ,
1189
- } } )
1132
+ return DropdownSearchInput . create ( searchInput , {
1133
+ defaultProps : {
1134
+ inputRef : this . handleSearchRef ,
1135
+ onChange : this . handleSearchChange ,
1136
+ style : { width : this . computeSearchInputWidth ( ) } ,
1137
+ tabIndex : this . computeSearchInputTabIndex ( ) ,
1138
+ value : searchQuery ,
1139
+ } ,
1140
+ } )
1190
1141
}
1191
1142
1192
1143
renderSearchSizer = ( ) => {
@@ -1218,10 +1169,7 @@ export default class Dropdown extends Component {
1218
1169
value : item . value ,
1219
1170
}
1220
1171
1221
- return Label . create (
1222
- renderLabel ( item , index , defaultProps ) ,
1223
- { defaultProps } ,
1224
- )
1172
+ return Label . create ( renderLabel ( item , index , defaultProps ) , { defaultProps } )
1225
1173
} )
1226
1174
}
1227
1175
@@ -1234,19 +1182,19 @@ export default class Dropdown extends Component {
1234
1182
return < div className = 'message' > { noResultsMessage } </ div >
1235
1183
}
1236
1184
1237
- const isActive = multiple
1238
- ? optValue => _ . includes ( value , optValue )
1239
- : optValue => optValue === value
1240
-
1241
- return _ . map ( options , ( opt , i ) => DropdownItem . create ( {
1242
- active : isActive ( opt . value ) ,
1243
- onClick : this . handleItemClick ,
1244
- selected : selectedIndex === i ,
1245
- ... opt ,
1246
- key : getKeyOrValue ( opt . key , opt . value ) ,
1247
- // Needed for handling click events on disabled items
1248
- style : { ... opt . style , pointerEvents : 'all' } ,
1249
- } ) )
1185
+ const isActive = multiple ? optValue => _ . includes ( value , optValue ) : optValue => optValue === value
1186
+
1187
+ return _ . map ( options , ( opt , i ) =>
1188
+ DropdownItem . create ( {
1189
+ active : isActive ( opt . value ) ,
1190
+ onClick : this . handleItemClick ,
1191
+ selected : selectedIndex === i ,
1192
+ ... opt ,
1193
+ key : getKeyOrValue ( opt . key , opt . value ) ,
1194
+ // Needed for handling click events on disabled items
1195
+ style : { ... opt . style , pointerEvents : 'all' } ,
1196
+ } ) ,
1197
+ )
1250
1198
}
1251
1199
1252
1200
renderMenu = ( ) => {
@@ -1257,11 +1205,7 @@ export default class Dropdown extends Component {
1257
1205
// single menu child
1258
1206
if ( ! childrenUtils . isNil ( children ) ) {
1259
1207
const menuChild = Children . only ( children )
1260
- const className = cx (
1261
- direction ,
1262
- useKeyOnly ( open , 'visible' ) ,
1263
- menuChild . props . className ,
1264
- )
1208
+ const className = cx ( direction , useKeyOnly ( open , 'visible' ) , menuChild . props . className )
1265
1209
1266
1210
return cloneElement ( menuChild , { className, ...ariaOptions } )
1267
1211
}
0 commit comments