@@ -13,6 +13,7 @@ import SelectorEngine from './dom/selector-engine.js'
13
13
import {
14
14
defineJQueryPlugin ,
15
15
getNextActiveElement ,
16
+ getElement ,
16
17
isVisible ,
17
18
isRTL
18
19
} from './util/index.js'
@@ -80,6 +81,7 @@ const CLASS_NAME_TAG_DELETE = 'form-multi-select-tag-delete'
80
81
81
82
const Default = {
82
83
cleaner : true ,
84
+ container : false ,
83
85
disabled : false ,
84
86
invalid : false ,
85
87
multiple : true ,
@@ -100,6 +102,7 @@ const Default = {
100
102
101
103
const DefaultType = {
102
104
cleaner : 'boolean' ,
105
+ container : '(string|element|boolean)' ,
103
106
disabled : 'boolean' ,
104
107
invalid : 'boolean' ,
105
108
multiple : 'boolean' ,
@@ -179,6 +182,12 @@ class MultiSelect extends BaseComponent {
179
182
EventHandler . trigger ( this . _element , EVENT_SHOW )
180
183
this . _clone . classList . add ( CLASS_NAME_SHOW )
181
184
this . _clone . setAttribute ( 'aria-expanded' , true )
185
+
186
+ if ( this . _config . container ) {
187
+ this . _menu . style . minWidth = `${ this . _clone . offsetWidth } px`
188
+ this . _menu . classList . add ( CLASS_NAME_SHOW )
189
+ }
190
+
182
191
EventHandler . trigger ( this . _element , EVENT_SHOWN )
183
192
184
193
this . _createPopper ( )
@@ -199,6 +208,11 @@ class MultiSelect extends BaseComponent {
199
208
this . _onSearchChange ( this . _searchElement )
200
209
this . _clone . classList . remove ( CLASS_NAME_SHOW )
201
210
this . _clone . setAttribute ( 'aria-expanded' , 'false' )
211
+
212
+ if ( this . _config . container ) {
213
+ this . _menu . classList . remove ( CLASS_NAME_SHOW )
214
+ }
215
+
202
216
EventHandler . trigger ( this . _element , EVENT_HIDDEN )
203
217
}
204
218
@@ -220,6 +234,7 @@ class MultiSelect extends BaseComponent {
220
234
this . _config = this . _getConfig ( config )
221
235
this . _options = this . _getOptions ( )
222
236
this . _selected = this . _getSelectedOptions ( this . _options )
237
+ this . _menu . remove ( )
223
238
this . _clone . remove ( )
224
239
this . _element . innerHTML = ''
225
240
this . _createNativeOptions ( this . _element , this . _options )
@@ -534,6 +549,7 @@ class MultiSelect extends BaseComponent {
534
549
} ] ,
535
550
placement : isRTL ( ) ? 'bottom-end' : 'bottom-start'
536
551
}
552
+
537
553
this . _popper = Popper . createPopper ( this . _togglerElement , this . _menu , popperConfig )
538
554
}
539
555
@@ -575,7 +591,13 @@ class MultiSelect extends BaseComponent {
575
591
576
592
dropdownDiv . append ( optionsDiv )
577
593
578
- this . _clone . append ( dropdownDiv )
594
+ const { container } = this . _config
595
+ if ( container ) {
596
+ // this._clone.parentNode.insertBefore(dropdownDiv, this._clone.nextSibling)
597
+ getElement ( container ) . append ( dropdownDiv )
598
+ } else {
599
+ this . _clone . append ( dropdownDiv )
600
+ }
579
601
580
602
this . _createOptions ( optionsDiv , this . _options )
581
603
this . _optionsElement = optionsDiv
@@ -649,7 +671,7 @@ class MultiSelect extends BaseComponent {
649
671
}
650
672
651
673
const value = String ( element . dataset . value )
652
- const { text } = this . _options . find ( option => option . value === value )
674
+ const { text } = this . _findOptionByValue ( value )
653
675
654
676
if ( this . _config . multiple && element . classList . contains ( CLASS_NAME_SELECTED ) ) {
655
677
this . _deselectOption ( value )
@@ -666,6 +688,23 @@ class MultiSelect extends BaseComponent {
666
688
}
667
689
}
668
690
691
+ _findOptionByValue ( value , options = this . _options ) {
692
+ for ( const option of options ) {
693
+ if ( option . value === value ) {
694
+ return option
695
+ }
696
+
697
+ if ( option . options && Array . isArray ( option . options ) ) {
698
+ const found = this . _findOptionByValue ( value , option . options )
699
+ if ( found ) {
700
+ return found
701
+ }
702
+ }
703
+ }
704
+
705
+ return null
706
+ }
707
+
669
708
_selectOption ( value , text ) {
670
709
if ( ! this . _config . multiple ) {
671
710
this . deselectAll ( )
@@ -860,7 +899,7 @@ class MultiSelect extends BaseComponent {
860
899
}
861
900
862
901
_filterOptionsList ( ) {
863
- const options = SelectorEngine . find ( SELECTOR_OPTION , this . _clone )
902
+ const options = SelectorEngine . find ( SELECTOR_OPTION , this . _menu )
864
903
let visibleOptions = 0
865
904
866
905
for ( const option of options ) {
@@ -884,8 +923,8 @@ class MultiSelect extends BaseComponent {
884
923
}
885
924
886
925
if ( visibleOptions > 0 ) {
887
- if ( SelectorEngine . findOne ( SELECTOR_OPTIONS_EMPTY , this . _clone ) ) {
888
- SelectorEngine . findOne ( SELECTOR_OPTIONS_EMPTY , this . _clone ) . remove ( )
926
+ if ( SelectorEngine . findOne ( SELECTOR_OPTIONS_EMPTY , this . _menu ) ) {
927
+ SelectorEngine . findOne ( SELECTOR_OPTIONS_EMPTY , this . _menu ) . remove ( )
889
928
}
890
929
891
930
return
@@ -896,8 +935,8 @@ class MultiSelect extends BaseComponent {
896
935
placeholder . classList . add ( CLASS_NAME_OPTIONS_EMPTY )
897
936
placeholder . innerHTML = this . _config . searchNoResultsLabel
898
937
899
- if ( ! SelectorEngine . findOne ( SELECTOR_OPTIONS_EMPTY , this . _clone ) ) {
900
- SelectorEngine . findOne ( SELECTOR_OPTIONS , this . _clone ) . append ( placeholder )
938
+ if ( ! SelectorEngine . findOne ( SELECTOR_OPTIONS_EMPTY , this . _menu ) ) {
939
+ SelectorEngine . findOne ( SELECTOR_OPTIONS , this . _menu ) . append ( placeholder )
901
940
}
902
941
}
903
942
}
0 commit comments