Skip to content

Commit 0800665

Browse files
committed
feat: add update and getValue methods
1 parent dd9ab93 commit 0800665

File tree

1 file changed

+45
-49
lines changed

1 file changed

+45
-49
lines changed

js/src/multi-select.js

Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ const Default = {
8080
searchPlaceholder: 'Select...',
8181
selection: true,
8282
selectionType: 'counter',
83-
selectionTypeCounterText: 'item(s) selected',
84-
selected: []
83+
selectionTypeCounterText: 'item(s) selected'
8584
}
8685

8786
const DefaultType = {
@@ -93,8 +92,7 @@ const DefaultType = {
9392
searchPlaceholder: 'string',
9493
selection: 'boolean',
9594
selectionType: 'string',
96-
selectionTypeCounterText: 'string',
97-
selected: 'array'
95+
selectionTypeCounterText: 'string'
9896
}
9997

10098
/**
@@ -107,7 +105,6 @@ class MultiSelect extends BaseComponent {
107105
constructor(element, config) {
108106
super(element)
109107

110-
this._element = element
111108
this._selectionElement = null
112109
this._selectionCleanerElement = null
113110
this._searchElement = null
@@ -143,10 +140,6 @@ class MultiSelect extends BaseComponent {
143140

144141
// Public
145142

146-
update(config) { // public method
147-
this._getConfig(config)
148-
}
149-
150143
show() {
151144
EventHandler.trigger(this._element, EVENT_SHOW)
152145
this._clone.classList.add(CLASS_NAME_SHOW)
@@ -170,6 +163,21 @@ class MultiSelect extends BaseComponent {
170163
EventHandler.trigger(this._element, EVENT_SEARCH)
171164
}
172165

166+
update(config) {
167+
this._config = this._getConfig(config)
168+
this._options = this._getOptions()
169+
this._selection = this._getSelectedOptions(this._options)
170+
this._clone.remove()
171+
this._element.innerHTML = ''
172+
this._createNativeOptions(this._element, this._options)
173+
this._createSelect()
174+
this._addEventListeners()
175+
}
176+
177+
getValue() {
178+
return this._selection
179+
}
180+
173181
// Private
174182

175183
_addEventListeners() {
@@ -215,20 +223,13 @@ class MultiSelect extends BaseComponent {
215223
})
216224
}
217225

218-
_getConfig(config, update) {
219-
if (update !== true) {
220-
config = {
221-
...this.constructor.Default,
222-
...Manipulator.getDataAttributes(this._element),
223-
...config
224-
}
226+
_getConfig(config) {
227+
config = {
228+
...Default,
229+
...Manipulator.getDataAttributes(this._element),
230+
...config
225231
}
226-
227-
typeCheckConfig(
228-
NAME,
229-
config,
230-
this.constructor.DefaultType
231-
)
232+
typeCheckConfig(NAME, config, DefaultType)
232233

233234
return config
234235
}
@@ -304,7 +305,7 @@ class MultiSelect extends BaseComponent {
304305

305306
this._createNativeOptions(select, data)
306307

307-
this._element.parentNode.replaceChild(select, this._element)
308+
this._element.replaceWith(select)
308309
this._element = select
309310
}
310311

@@ -320,7 +321,7 @@ class MultiSelect extends BaseComponent {
320321
const opt = document.createElement('OPTION')
321322
opt.value = option.value
322323
if (option.selected === true) {
323-
opt.selected = true
324+
opt.setAttribute('selected', 'selected')
324325
}
325326

326327
opt.innerHTML = option.text
@@ -732,9 +733,19 @@ class MultiSelect extends BaseComponent {
732733

733734
// Static
734735

735-
static _multiSelectInterface(element, config) {
736+
static multiSelectInterface(element, config) {
736737
let data = Data.getData(element, DATA_KEY)
737-
const _config = typeof config === 'object' && config
738+
let _config = {
739+
...Default,
740+
...Manipulator.getDataAttributes(element)
741+
}
742+
743+
if (typeof config === 'object') {
744+
_config = {
745+
..._config,
746+
...config
747+
}
748+
}
738749

739750
if (!data) {
740751
data = new MultiSelect(element, _config)
@@ -751,25 +762,7 @@ class MultiSelect extends BaseComponent {
751762

752763
static jQueryInterface(config) {
753764
return this.each(function () {
754-
let data = Data.getData(this, DATA_KEY)
755-
756-
if (!data) {
757-
data = new MultiSelect(this)
758-
}
759-
760-
// eslint-disable-next-line default-case
761-
switch (config) {
762-
// case 'update':
763-
// data[config](this, par)
764-
// break
765-
case 'dispose':
766-
case 'open':
767-
case 'close':
768-
case 'search':
769-
case 'value':
770-
data[config](this)
771-
break
772-
}
765+
MultiSelect.multiSelectInterface(this, config)
773766
})
774767
}
775768

@@ -816,11 +809,14 @@ class MultiSelect extends BaseComponent {
816809
* ------------------------------------------------------------------------
817810
*/
818811
EventHandler.on(window, EVENT_LOAD_DATA_API, () => {
819-
// eslint-disable-next-line unicorn/prefer-spread
820-
Array.from(document.querySelectorAll(SELECTOR_SELECT)).forEach(element => {
821-
MultiSelect._multiSelectInterface(element)
822-
})
812+
SelectorEngine.find(SELECTOR_SELECT)
813+
.forEach(ms => {
814+
if (ms.tabIndex !== -1) {
815+
MultiSelect.multiSelectInterface(ms)
816+
}
817+
})
823818
})
819+
824820
EventHandler.on(document, EVENT_CLICK_DATA_API, MultiSelect.clearMenus)
825821
EventHandler.on(document, EVENT_KEYUP_DATA_API, MultiSelect.clearMenus)
826822

0 commit comments

Comments
 (0)