From b1c05eed8e6034f092038b0a0ac65db2b7477737 Mon Sep 17 00:00:00 2001 From: William Karsten Date: Tue, 20 Sep 2022 15:19:48 -0500 Subject: [PATCH] Backport unreleased fix for Tom select github.com/orchidjs/tom-select/issues/455 I found the bug in Tom Select. Connected with the author and got it fixed. It is unknown when the next release will be so this is the current fix. --- vendor/assets/javascripts/tom-select.base.js | 34 +++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/vendor/assets/javascripts/tom-select.base.js b/vendor/assets/javascripts/tom-select.base.js index 31b0c4f1..8fd991b1 100644 --- a/vendor/assets/javascripts/tom-select.base.js +++ b/vendor/assets/javascripts/tom-select.base.js @@ -2849,22 +2849,27 @@ refreshOptions(triggerDropdown = true) { - var i, j, k, n, optgroup, optgroups, html, has_create_option, active_value, active_group; + var i, j, k, n, optgroup, optgroups, html, has_create_option, active_group; var create; const groups = {}; const groups_order = []; var self = this; var query = self.inputValue(); + const same_query = query === self.lastQuery || (query == '' && self.lastQuery == null); var results = self.search(query); - var active_option = null; //self.activeOption; + var active_option = null; var show_dropdown = self.settings.shouldOpen || false; var dropdown_content = self.dropdown_content; - if (self.activeOption) { - active_value = self.activeOption.dataset.value; - active_group = self.activeOption.closest('[data-group]'); - } // build markup + if( same_query ){ + active_option = self.activeOption; + if( active_option ){ + active_group = active_option.closest('[data-group]'); + } + } + + // build markup n = results.items.length; @@ -2912,18 +2917,15 @@ }); option_el.classList.add('ts-cloned'); removeClasses(option_el, 'active'); - } // make sure we keep the activeOption in the same group + // make sure we keep the activeOption in the same group - if (!active_option && active_value == opt_value) { - if (active_group) { - if (active_group.dataset.group === optgroup) { - active_option = option_el; - } - } else { - active_option = option_el; - } - } + if (self.activeOption && self.activeOption.dataset.value == opt_value) { + if (active_group && active_group.dataset.group === optgroup.toString()) { + active_option = option_el; + } + } + } groups[optgroup].appendChild(option_el); }