From f17d382fcb7392905049f2758bc840364509ea30 Mon Sep 17 00:00:00 2001 From: Josh Schmidt Date: Mon, 7 Jun 2021 18:53:58 -0600 Subject: [PATCH] updateOriginalInput() for duplicate items --- src/tom-select.ts | 42 +++++++++++++++++++++------------ test/tests/config-duplicates.js | 8 ++++--- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/tom-select.ts b/src/tom-select.ts index 57aac177..e97bde8f 100644 --- a/src/tom-select.ts +++ b/src/tom-select.ts @@ -1333,7 +1333,7 @@ export default class TomSelect extends MicroPlugin(MicroEvent){ groups_order.push(optgroup); } - // a child could only have one parent, so if you have more parents clone the child + // nodes can only have one parent, so if the option is in mutple groups, we need a clone if( j > 0 ){ option_el = option_el.cloneNode(true) as HTMLElement; setAttr(option_el,{id: option.$id+'-clone-'+j,'aria-selected':null}); @@ -2036,6 +2036,8 @@ export default class TomSelect extends MicroPlugin(MicroEvent){ if( self.is_select_tag ){ + const selected = document.createDocumentFragment(); + function AddSelected(option_el:HTMLOptionElement|null, value:string, label:string):HTMLOptionElement{ if( !option_el ){ @@ -2044,34 +2046,44 @@ export default class TomSelect extends MicroPlugin(MicroEvent){ option_el.selected = true; setAttr(option_el,{selected:'true'}); - self.input.prepend(option_el); + selected.append(option_el); return option_el; } - // remove selected attribute from options whose values are not in self.items + // unselect all selected options self.input.querySelectorAll('option[selected]').forEach((option_el:Element) => { - const _opt = option_el as HTMLOptionElement; - if( self.items.indexOf(_opt.value) == -1 ){ - setAttr(_opt,{selected:null}); - _opt.selected = false; - } + setAttr(option_el,{selected:null}); + (option_el).selected = false; }); - // order selected