@@ -11,7 +11,7 @@ export default class extends Controller {
1111 type : String ,
1212 default : 'contact(s)'
1313 } ,
14- showAllOption : Boolean ,
14+ showAllOption : Boolean
1515 }
1616
1717 connect ( ) {
@@ -41,33 +41,37 @@ export default class extends Controller {
4141 const showAllOptionCheck = this . showAllOptionValue
4242 const hiddenItemTemplate = showAllOptionCheck && this . hiddenItemTarget && this . hiddenItemTarget . innerHTML
4343 const showAllOptionTemplate = showAllOptionCheck && this . showAllOptionTarget && this . showAllOptionTarget . innerHTML
44-
44+
4545 // orderedOptionVals is of type (" " | number)[] - the " " could appear
4646 // because using it as the value for the select/unselect all option
4747 let orderedOptionVals = this . optionsValue . map ( opt => opt . value )
4848 if ( showAllOptionCheck ) {
4949 // using " " as value instead of "" bc tom-select doesn't init the "" in the item list
50- orderedOptionVals = [ " " ] . concat ( orderedOptionVals )
50+ orderedOptionVals = [ ' ' ] . concat ( orderedOptionVals )
5151 }
5252
53- const initItems = Array . isArray ( this . selectedItemsValue ) && this . selectedItemsValue . length ? showAllOptionCheck ? [ " " ] . concat ( this . selectedItemsValue ) : this . selectedItemsValue : orderedOptionVals
53+ const initItems = Array . isArray ( this . selectedItemsValue ) && this . selectedItemsValue . length
54+ ? showAllOptionCheck
55+ ? [ ' ' ] . concat ( this . selectedItemsValue )
56+ : this . selectedItemsValue
57+ : orderedOptionVals
58+
59+ const dropdownOptions = showAllOptionCheck
60+ ? [ { text : 'Select/Unseselect all' , subtext : '' , value : ' ' , group : '' } ] . concat ( this . optionsValue )
61+ : this . optionsValue
5462
55- const dropdownOptions = showAllOptionCheck ?
56- [ { text : "Select/Unseselect all" , subtext : "" , value : " " , group : "" } ] . concat ( this . optionsValue )
57- : this . optionsValue
58-
5963 // const selectAllBtn = this.selectAllBtnTarget
6064 // assign TomSelect instance to this.selectEl if going with button implementation
6165
6266 /* eslint-disable no-new */
6367 new TomSelect ( this . selectTarget , {
64- onItemRemove : function ( value , data ) {
68+ onItemRemove : function ( value , data ) {
6569 // for the select/unselect all button - add in short circuit in case showAllBtn doesn't exist
6670 // if (this.items.length < orderedOptionVals.length) {
6771 // selectAllBtn.innerText = 'Select all'
6872 // }
6973
70- if ( value === " " ) {
74+ if ( value === ' ' ) {
7175 this . clear ( )
7276 }
7377 } ,
@@ -80,8 +84,8 @@ export default class extends Controller {
8084 // selectAllBtn.innerText = 'Select all'
8185 // }
8286
83- if ( value === " " ) {
84- this . addItems ( orderedOptionVals ) ;
87+ if ( value === ' ' ) {
88+ this . addItems ( orderedOptionVals )
8589 }
8690 } ,
8791 plugins : {
@@ -104,7 +108,7 @@ export default class extends Controller {
104108 option : function ( data , escape ) {
105109 let html
106110
107- if ( showAllOptionCheck && data && data . value === " " ) {
111+ if ( showAllOptionCheck && data && data . value === ' ' ) {
108112 html = showAllOptionTemplate . replace ( / D A T A _ L A B E L / g, escape ( data . text ) )
109113 } else {
110114 html = optionTemplate . replace ( / D A T A _ L A B E L / g, escape ( data . text ) )
@@ -113,7 +117,7 @@ export default class extends Controller {
113117 return html
114118 } ,
115119 item : function ( data , escape ) {
116- return showAllOptionCheck && data . value === " " ? hiddenItemTemplate : itemTemplate . replace ( / D A T A _ L A B E L / g, escape ( data . text ) )
120+ return showAllOptionCheck && data . value === ' ' ? hiddenItemTemplate : itemTemplate . replace ( / D A T A _ L A B E L / g, escape ( data . text ) )
117121 }
118122 }
119123 } )
@@ -124,7 +128,7 @@ export default class extends Controller {
124128 // if (!this.selectEl || !this.selectAllBtnTarget) return
125129
126130 // const checkedStatus = this.selectEl.items.length === Object.keys(this.selectEl.options).length ? "all" : "not-all"
127-
131+
128132 // if (checkedStatus === "all") {
129133 // this.selectEl.clear()
130134 // this.selectAllBtnTarget.textContent = "Select all"
0 commit comments