@@ -51,6 +51,7 @@ const EVENT_LOAD_DATA_API = `load${EVENT_KEY}${DATA_API_KEY}`
51
51
52
52
const CLASS_NAME_CLEANER = 'form-multi-select-cleaner'
53
53
const CLASS_NAME_DISABLED = 'disabled'
54
+ const CLASS_NAME_INPUT_GROUP = 'form-multi-select-input-group'
54
55
const CLASS_NAME_LABEL = 'label'
55
56
const CLASS_NAME_SELECT = 'form-multi-select'
56
57
const CLASS_NAME_SELECT_DROPDOWN = 'form-multi-select-dropdown'
@@ -68,7 +69,6 @@ const CLASS_NAME_SELECTION_TAGS = 'form-multi-select-selection-tags'
68
69
const CLASS_NAME_SHOW = 'show'
69
70
const CLASS_NAME_TAG = 'form-multi-select-tag'
70
71
const CLASS_NAME_TAG_DELETE = 'form-multi-select-tag-delete'
71
- const CLASS_NAME_TOGGLER = 'form-multi-select-toggler'
72
72
73
73
const Default = {
74
74
cleaner : true ,
@@ -131,7 +131,7 @@ class MultiSelect extends BaseComponent {
131
131
this . _options = this . _getOptions ( )
132
132
this . _popper = null
133
133
this . _search = ''
134
- this . _selection = this . _getSelectedOptions ( this . _options )
134
+ this . _selected = this . _getSelectedOptions ( this . _options ) // TODO : change to this._selected
135
135
136
136
if ( this . _config . options . length > 0 ) {
137
137
this . _createNativeSelect ( this . _config . options )
@@ -207,7 +207,7 @@ class MultiSelect extends BaseComponent {
207
207
update ( config ) {
208
208
this . _config = this . _getConfig ( config )
209
209
this . _options = this . _getOptions ( )
210
- this . _selection = this . _getSelectedOptions ( this . _options )
210
+ this . _selected = this . _getSelectedOptions ( this . _options )
211
211
this . _clone . remove ( )
212
212
this . _element . innerHTML = ''
213
213
this . _createNativeOptions ( this . _element , this . _options )
@@ -246,7 +246,7 @@ class MultiSelect extends BaseComponent {
246
246
}
247
247
248
248
getValue ( ) {
249
- return this . _selection
249
+ return this . _selected
250
250
}
251
251
252
252
// Private
@@ -424,26 +424,22 @@ class MultiSelect extends BaseComponent {
424
424
}
425
425
426
426
_createSelect ( ) {
427
- const div = document . createElement ( 'div' )
428
- div . classList . add ( CLASS_NAME_SELECT )
429
- div . classList . toggle ( 'is-invalid' , this . _config . invalid )
430
- div . classList . toggle ( 'is-valid' , this . _config . valid )
431
- div . setAttribute ( 'aria-expanded' , 'false' )
427
+ const multiSelectEl = document . createElement ( 'div' )
428
+ multiSelectEl . classList . add ( CLASS_NAME_SELECT )
429
+ multiSelectEl . classList . toggle ( 'is-invalid' , this . _config . invalid )
430
+ multiSelectEl . classList . toggle ( 'is-valid' , this . _config . valid )
431
+ multiSelectEl . setAttribute ( 'aria-expanded' , 'false' )
432
432
433
433
if ( this . _config . disabled ) {
434
434
this . _element . classList . add ( CLASS_NAME_DISABLED )
435
435
}
436
436
437
437
for ( const className of this . _getClassNames ( ) ) {
438
- div . classList . add ( className )
438
+ multiSelectEl . classList . add ( className )
439
439
}
440
440
441
- if ( this . _config . multiple && this . _config . selectionType === 'tags' ) {
442
- div . classList . add ( CLASS_NAME_SELECTION_TAGS )
443
- }
444
-
445
- this . _clone = div
446
- this . _element . parentNode . insertBefore ( div , this . _element . nextSibling )
441
+ this . _clone = multiSelectEl
442
+ this . _element . parentNode . insertBefore ( multiSelectEl , this . _element . nextSibling )
447
443
this . _createSelection ( )
448
444
this . _createButtons ( )
449
445
@@ -463,12 +459,16 @@ class MultiSelect extends BaseComponent {
463
459
464
460
_createSelection ( ) {
465
461
const togglerEl = document . createElement ( 'div' )
466
- togglerEl . classList . add ( CLASS_NAME_TOGGLER )
462
+ togglerEl . classList . add ( CLASS_NAME_INPUT_GROUP )
467
463
this . _togglerElement = togglerEl
468
464
469
465
const selectionEl = document . createElement ( 'div' )
470
466
selectionEl . classList . add ( CLASS_NAME_SELECTION )
471
467
468
+ if ( this . _config . multiple && this . _config . selectionType === 'tags' ) {
469
+ selectionEl . classList . add ( CLASS_NAME_SELECTION_TAGS )
470
+ }
471
+
472
472
togglerEl . append ( selectionEl )
473
473
this . _clone . append ( togglerEl )
474
474
@@ -658,8 +658,8 @@ class MultiSelect extends BaseComponent {
658
658
this . deselectAll ( )
659
659
}
660
660
661
- if ( this . _selection . filter ( e => e . value === value ) . length === 0 ) {
662
- this . _selection . push ( {
661
+ if ( this . _selected . filter ( e => e . value === value ) . length === 0 ) {
662
+ this . _selected . push ( {
663
663
value,
664
664
text
665
665
} )
@@ -677,7 +677,7 @@ class MultiSelect extends BaseComponent {
677
677
}
678
678
679
679
EventHandler . trigger ( this . _element , EVENT_CHANGED , {
680
- value : this . _selection
680
+ value : this . _selected
681
681
} )
682
682
683
683
this . _updateSelection ( )
@@ -687,8 +687,8 @@ class MultiSelect extends BaseComponent {
687
687
}
688
688
689
689
_deselectOption ( value ) {
690
- const selected = this . _selection . filter ( e => e . value !== value )
691
- this . _selection = selected
690
+ const selected = this . _selected . filter ( e => e . value !== value )
691
+ this . _selected = selected
692
692
693
693
SelectorEngine . findOne ( `option[value="${ value } "]` , this . _element ) . selected = false
694
694
@@ -698,7 +698,7 @@ class MultiSelect extends BaseComponent {
698
698
}
699
699
700
700
EventHandler . trigger ( this . _element , EVENT_CHANGED , {
701
- value : this . _selection
701
+ value : this . _selected
702
702
} )
703
703
704
704
this . _updateSelection ( )
@@ -708,8 +708,8 @@ class MultiSelect extends BaseComponent {
708
708
}
709
709
710
710
_deselectLastOption ( ) {
711
- if ( this . _selection . length > 0 ) {
712
- const last = this . _selection . pop ( )
711
+ if ( this . _selected . length > 0 ) {
712
+ const last = this . _selected . pop ( )
713
713
this . _deselectOption ( last . value )
714
714
}
715
715
}
@@ -718,29 +718,29 @@ class MultiSelect extends BaseComponent {
718
718
const selection = SelectorEngine . findOne ( SELECTOR_SELECTION , this . _clone )
719
719
const search = SelectorEngine . findOne ( SELECTOR_SEARCH , this . _clone )
720
720
721
- if ( this . _selection . length === 0 && ! this . _config . search ) {
722
- selection . innerHTML = `<span class="form-multi-select-text ">${ this . _config . placeholder } </span>`
721
+ if ( this . _selected . length === 0 && ! this . _config . search ) {
722
+ selection . innerHTML = `<span class="form-multi-select-placeholder ">${ this . _config . placeholder } </span>`
723
723
return
724
724
}
725
725
726
726
if ( this . _config . multiple && this . _config . selectionType === 'counter' && ! this . _config . search ) {
727
- selection . innerHTML = `<span class="form-multi-select-text"> ${ this . _selection . length } ${ this . _config . selectionTypeCounterText } </span> `
727
+ selection . innerHTML = `${ this . _selected . length } ${ this . _config . selectionTypeCounterText } `
728
728
}
729
729
730
730
if ( this . _config . multiple && this . _config . selectionType === 'tags' ) {
731
731
selection . innerHTML = ''
732
732
733
- for ( const e of this . _selection ) {
734
- selection . append ( this . _createTag ( e . value , e . text ) )
733
+ for ( const option of this . _selected ) {
734
+ selection . append ( this . _createTag ( option . value , option . text ) )
735
735
}
736
736
}
737
737
738
738
if ( this . _config . multiple && this . _config . selectionType === 'text' ) {
739
- selection . innerHTML = this . _selection . map ( ( option , index ) => `<span>${ option . text } ${ index === this . _selection . length - 1 ? '' : ',' } </span>` ) . join ( '' )
739
+ selection . innerHTML = this . _selected . map ( ( option , index ) => `<span>${ option . text } ${ index === this . _selected . length - 1 ? '' : ',' } </span>` ) . join ( '' )
740
740
}
741
741
742
- if ( ! this . _config . multiple && this . _selection . length > 0 && ! this . _config . search ) {
743
- selection . innerHTML = `<span class="form-multi-select-text"> ${ this . _selection [ 0 ] . text } </span>`
742
+ if ( ! this . _config . multiple && this . _selected . length > 0 && ! this . _config . search ) {
743
+ selection . innerHTML = this . _selected [ 0 ] . text
744
744
}
745
745
746
746
if ( search ) {
@@ -759,7 +759,7 @@ class MultiSelect extends BaseComponent {
759
759
760
760
const selectionCleaner = SelectorEngine . findOne ( SELECTOR_CLEANER , this . _clone )
761
761
762
- if ( this . _selection . length > 0 ) {
762
+ if ( this . _selected . length > 0 ) {
763
763
selectionCleaner . style . removeProperty ( 'display' )
764
764
return
765
765
}
@@ -774,30 +774,30 @@ class MultiSelect extends BaseComponent {
774
774
775
775
// Select single
776
776
777
- if ( ! this . _config . multiple && this . _selection . length > 0 ) {
778
- this . _searchElement . placeholder = this . _selection [ 0 ] . text
777
+ if ( ! this . _config . multiple && this . _selected . length > 0 ) {
778
+ this . _searchElement . placeholder = this . _selected [ 0 ] . text
779
779
return
780
780
}
781
781
782
- if ( ! this . _config . multiple && this . _selection . length === 0 ) {
782
+ if ( ! this . _config . multiple && this . _selected . length === 0 ) {
783
783
this . _searchElement . placeholder = this . _config . placeholder
784
784
return
785
785
}
786
786
787
787
// Select multiple
788
788
789
- if ( this . _config . multiple && this . _selection . length > 0 && this . _config . selectionType !== 'counter' ) {
789
+ if ( this . _config . multiple && this . _selected . length > 0 && this . _config . selectionType !== 'counter' ) {
790
790
this . _searchElement . removeAttribute ( 'placeholder' )
791
791
return
792
792
}
793
793
794
- if ( this . _config . multiple && this . _selection . length === 0 ) {
794
+ if ( this . _config . multiple && this . _selected . length === 0 ) {
795
795
this . _searchElement . placeholder = this . _config . placeholder
796
796
return
797
797
}
798
798
799
799
if ( this . _config . multiple && this . _config . selectionType === 'counter' ) {
800
- this . _searchElement . placeholder = `${ this . _selection . length } item(s) selected`
800
+ this . _searchElement . placeholder = `${ this . _selected . length } item(s) selected`
801
801
}
802
802
}
803
803
@@ -806,12 +806,12 @@ class MultiSelect extends BaseComponent {
806
806
return
807
807
}
808
808
809
- if ( this . _selection . length > 0 && ( this . _config . selectionType === 'tags' || this . _config . selectionType === 'text' ) ) {
809
+ if ( this . _selected . length > 0 && ( this . _config . selectionType === 'tags' || this . _config . selectionType === 'text' ) ) {
810
810
this . _searchElement . size = size
811
811
return
812
812
}
813
813
814
- if ( this . _selection . length === 0 && ( this . _config . selectionType === 'tags' || this . _config . selectionType === 'text' ) ) {
814
+ if ( this . _selected . length === 0 && ( this . _config . selectionType === 'tags' || this . _config . selectionType === 'text' ) ) {
815
815
this . _searchElement . removeAttribute ( 'size' )
816
816
}
817
817
}
@@ -939,18 +939,17 @@ class MultiSelect extends BaseComponent {
939
939
continue
940
940
}
941
941
942
- context . _clone . classList . remove ( CLASS_NAME_SHOW )
942
+ context . hide ( )
943
943
944
944
EventHandler . trigger ( context . _element , EVENT_HIDDEN )
945
945
}
946
946
}
947
947
}
948
948
949
949
/**
950
- * ------------------------------------------------------------------------
951
- * Data Api implementation
952
- * ------------------------------------------------------------------------
950
+ * Data API implementation
953
951
*/
952
+
954
953
EventHandler . on ( window , EVENT_LOAD_DATA_API , ( ) => {
955
954
for ( const ms of SelectorEngine . find ( SELECTOR_SELECT ) ) {
956
955
if ( ms . tabIndex !== - 1 ) {
@@ -963,10 +962,7 @@ EventHandler.on(document, EVENT_CLICK_DATA_API, MultiSelect.clearMenus)
963
962
EventHandler . on ( document , EVENT_KEYUP_DATA_API , MultiSelect . clearMenus )
964
963
965
964
/**
966
- * ------------------------------------------------------------------------
967
965
* jQuery
968
- * ------------------------------------------------------------------------
969
- * add .MultiSelect to jQuery only if jQuery is present
970
966
*/
971
967
972
968
defineJQueryPlugin ( MultiSelect )
0 commit comments