@@ -32,8 +32,10 @@ const RIGHT_MOUSE_BUTTON = 2
32
32
const SELECTOR_INPUT = '.c-select-search'
33
33
const SELECTOR_LIST = '.c-select-options'
34
34
const SELECTOR_MULTI_SELECT = '.c-select'
35
+ const SELECTOR_OPTGROUP = '.c-select-optgroup'
35
36
const SELECTOR_OPTION = '.c-select-option'
36
37
const SELECTOR_OPTIONS = '.c-select-options'
38
+ const SELECTOR_OPTIONS_EMPTY = '.c-select-options-empty'
37
39
const SELECTOR_SELECTED = '.c-selected'
38
40
const SELECTOR_SELECTION = '.c-select-selection'
39
41
const SELECTOR_TAGS = '.c-select-tags'
@@ -53,6 +55,7 @@ const CLASS_NAME_OPTGROUP = 'c-select-optgroup'
53
55
const CLASS_NAME_OPTGROUP_LABEL = 'c-select-optgroup-label'
54
56
const CLASS_NAME_OPTION = 'c-select-option'
55
57
const CLASS_NAME_OPTIONS = 'c-select-options'
58
+ const CLASS_NAME_OPTIONS_EMPTY = 'c-select-options-empty'
56
59
const CLASS_NAME_SEARCH = 'c-select-search'
57
60
const CLASS_NAME_SELECTED = 'c-selected'
58
61
const CLASS_NAME_SELECTION = 'c-select-selection'
@@ -66,17 +69,18 @@ const Default = {
66
69
inline : false ,
67
70
multiple : false ,
68
71
options : [ ] ,
72
+ optionsEmptyPlaceholder : 'no items' ,
69
73
search : false ,
70
74
searchPlaceholder : 'Select...' ,
71
75
selectionType : 'counter' ,
72
- selected : [ ] ,
73
- tags : false
76
+ selected : [ ]
74
77
}
75
78
76
79
const DefaultType = {
77
80
inline : 'boolean' ,
78
81
multiple : 'boolean' ,
79
82
options : 'array' ,
83
+ optionsEmptyPlaceholder : 'string' ,
80
84
search : 'boolean' ,
81
85
searchPlaceholder : 'string' ,
82
86
selectionType : 'string' ,
@@ -183,7 +187,7 @@ class Select {
183
187
const key = event . keyCode || event . charCode
184
188
185
189
if ( ( key === 8 || key === 46 ) && event . target . value . length === 0 ) {
186
- this . _deleteLastTag ( )
190
+ this . _selectionDeleteLast ( )
187
191
}
188
192
} )
189
193
EventHandler . on ( this . _optionsElement , EVENT_CLICK , event => {
@@ -328,6 +332,7 @@ class Select {
328
332
this . _createSelection ( )
329
333
if ( this . _config . search ) {
330
334
this . _createSearchInput ( )
335
+ this . _updateSearch ( )
331
336
}
332
337
333
338
this . _createOptionsContainer ( )
@@ -347,18 +352,18 @@ class Select {
347
352
_createSearchInput ( ) {
348
353
const input = document . createElement ( 'input' )
349
354
input . classList . add ( CLASS_NAME_SEARCH )
350
- if ( this . _selection . length === 0 ) {
351
- input . placeholder = this . _config . searchPlaceholder
352
- }
355
+ // if (this._selection.length === 0) {
356
+ // input.placeholder = this._config.searchPlaceholder
357
+ // }
353
358
354
- if ( this . _selection . length > 0 && ! this . _config . multiple ) {
355
- input . placeholder = this . _selection [ 0 ] . text
356
- this . _selectionElement . style . display = 'none'
357
- }
359
+ // if (this._selection.length > 0 && !this._config.multiple) {
360
+ // input.placeholder = this._selection[0].text
361
+ // this._selectionElement.style.display = 'none'
362
+ // }
358
363
359
- if ( this . _selection . length > 0 && this . _config . multiple ) {
360
- input . placeholder = ''
361
- }
364
+ // if (this._selection.length > 0 && this._config.multiple) {
365
+ // input.placeholder = ''
366
+ // }
362
367
363
368
this . _clone . append ( input )
364
369
this . _searchElement = input
@@ -425,6 +430,7 @@ class Select {
425
430
tag . remove ( )
426
431
this . _selectionDelete ( value )
427
432
this . _updateOptionsList ( )
433
+ this . _updateSearch ( )
428
434
} )
429
435
430
436
return tag
@@ -460,41 +466,16 @@ class Select {
460
466
461
467
if ( this . _config . multiple && element . classList . contains ( CLASS_NAME_SELECTED ) ) {
462
468
this . _selectionDelete ( value )
463
- }
464
-
465
- if ( this . _config . multiple && ! element . classList . contains ( CLASS_NAME_SELECTED ) ) {
469
+ } else if ( this . _config . multiple && ! element . classList . contains ( CLASS_NAME_SELECTED ) ) {
466
470
this . _selectionAdd ( value , text )
467
- }
468
-
469
- if ( ! this . _config . multiple ) {
471
+ } else if ( ! this . _config . multiple ) {
470
472
this . _selectionAdd ( value , text )
471
473
}
472
474
473
475
this . _updateSelection ( )
474
476
this . _updateSearch ( )
475
477
}
476
478
477
- _onListClick ( element ) {
478
- if ( ! element . classList . contains ( CLASS_NAME_OPTION ) || element . classList . contains ( CLASS_NAME_LABEL ) ) {
479
- return
480
- }
481
-
482
- const value = String ( element . dataset . value )
483
- const text = element . textContent
484
-
485
- if ( this . _config . multiple ) {
486
- if ( element . classList . contains ( CLASS_NAME_SELECTED ) ) {
487
- this . _selectionDelete ( value )
488
- } else {
489
- this . _selectionAdd ( value , text )
490
- }
491
- } else {
492
- this . _selectionAdd ( value , element . textContent )
493
- }
494
-
495
- this . _updateSearch ( )
496
- }
497
-
498
479
_selectionAdd ( value , text ) {
499
480
if ( ! this . _config . multiple ) {
500
481
this . _selectionClear ( )
@@ -516,10 +497,19 @@ class Select {
516
497
}
517
498
518
499
_selectionDelete ( value ) {
519
- this . _selection = this . _selection . filter ( e => e . value !== value )
500
+ const selected = this . _selection . filter ( e => e . value !== value )
501
+ this . _selection = selected
520
502
this . _unSelectOption ( value )
521
503
}
522
504
505
+ _selectionDeleteLast ( ) {
506
+ if ( this . _selection . length > 0 ) {
507
+ const last = this . _selection . pop ( )
508
+ this . _selectionDelete ( last . value )
509
+ this . _updateSelection ( )
510
+ this . _updateSearch ( )
511
+ }
512
+ }
523
513
// .c-select-selections
524
514
525
515
_updateSelection ( ) {
@@ -546,34 +536,28 @@ class Select {
546
536
if ( this . _selection . length > 0 ) {
547
537
selection . innerHTML = this . _selection [ 0 ] . text
548
538
}
549
-
550
- // selection.innerHTML = this._selection[0].text
551
- // console.log(this._selection)
552
539
}
553
540
554
541
_updateSearch ( ) {
555
- // if (this._selection.length === 0) {
556
- // this._searchElement.placeholder = this._config.searchPlaceholder
557
- // }
558
-
559
542
if ( this . _selection . length > 0 && ! this . _config . multiple ) {
560
543
this . _searchElement . placeholder = this . _selection [ 0 ] . text
561
544
this . _selectionElement . style . display = 'none'
562
545
}
563
546
564
- if ( this . _selection . length > 0 && this . _config . multiple ) {
547
+ if ( this . _selection . length > 0 && this . _config . multiple && this . _config . selectionType !== 'counter' ) {
565
548
this . _searchElement . placeholder = ''
566
549
this . _selectionElement . style . display = 'initial'
567
550
}
568
551
552
+ if ( this . _selection . length === 0 && this . _config . multiple ) {
553
+ this . _searchElement . placeholder = this . _config . searchPlaceholder
554
+ this . _selectionElement . style . display = 'initial'
555
+ }
569
556
570
- // if (!this._config.multiple) {
571
- // if (this._selection.length > 0) {
572
- // this._searchElement.placeholder = this._selection[0].text
573
- // }
574
-
575
- // return
576
- // }
557
+ if ( this . _config . multiple && this . _config . selectionType === 'counter' ) {
558
+ this . _searchElement . placeholder = `${ this . _selection . length } item(s) selected`
559
+ this . _selectionElement . style . display = 'none'
560
+ }
577
561
}
578
562
579
563
// .c-select-selections
@@ -634,22 +618,54 @@ class Select {
634
618
} )
635
619
}
636
620
621
+ _isHidden ( element ) {
622
+ return element . offsetParent === null
623
+ }
624
+
625
+ _isVisible ( element ) {
626
+ const style = window . getComputedStyle ( element )
627
+ return ( style . display !== 'none' )
628
+ }
629
+
637
630
_filterOptionsList ( ) {
638
631
const options = SelectorEngine . find ( SELECTOR_OPTION , this . _clone )
632
+ let visibleOptions = 0
639
633
640
634
options . forEach ( option => {
641
635
if ( option . textContent . toLowerCase ( ) . indexOf ( this . _search ) === - 1 ) {
642
636
option . style . display = 'none'
643
637
} else {
644
638
option . style . display = 'block'
639
+ visibleOptions ++
640
+ }
641
+
642
+ const optgroup = option . closest ( SELECTOR_OPTGROUP )
643
+ if ( optgroup ) {
644
+ if ( SelectorEngine . children ( optgroup , SELECTOR_OPTION ) . filter ( element => this . _isVisible ( element ) ) . length > 0 ) {
645
+ optgroup . style . display = 'block'
646
+ } else {
647
+ optgroup . style . display = 'none'
648
+ }
645
649
}
646
650
} )
647
- }
648
651
649
- _deleteLastTag ( ) {
650
- const lastVal = Object . keys ( this . _selection ) [ Object . keys ( this . _selection ) . length - 1 ]
651
- this . _selectionDelete ( lastVal )
652
- this . _updateSelection ( )
652
+ if ( visibleOptions > 0 ) {
653
+ if ( SelectorEngine . findOne ( SELECTOR_OPTIONS_EMPTY , this . _clone ) ) {
654
+ SelectorEngine . findOne ( SELECTOR_OPTIONS_EMPTY , this . _clone ) . remove ( )
655
+ }
656
+
657
+ return
658
+ }
659
+
660
+ if ( visibleOptions === 0 ) {
661
+ const placeholder = document . createElement ( 'div' )
662
+ placeholder . classList . add ( CLASS_NAME_OPTIONS_EMPTY )
663
+ placeholder . innerHTML = this . _config . optionsEmptyPlaceholder
664
+
665
+ if ( ! SelectorEngine . findOne ( SELECTOR_OPTIONS_EMPTY , this . _clone ) ) {
666
+ SelectorEngine . findOne ( SELECTOR_OPTIONS , this . _clone ) . append ( placeholder )
667
+ }
668
+ }
653
669
}
654
670
655
671
// Static
0 commit comments