From e06b765433e22cf07cb88f19ed2cc3c003fd8609 Mon Sep 17 00:00:00 2001 From: Dan Gealow Date: Wed, 9 Oct 2024 18:58:44 -0400 Subject: [PATCH 1/2] Add new param to clearOptions and fix its behavior --- src/selectize.js | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/selectize.js b/src/selectize.js index 054553f2a..dbbcc5e97 100644 --- a/src/selectize.js +++ b/src/selectize.js @@ -1486,26 +1486,33 @@ $.extend(Selectize.prototype, { }, /** - * Clears all options, including all selected items + * Removes all options, by default including all selected items * - * @param {boolean} silent + * @param {boolean} [silent = false] If truthy, no change event will be fired on the original input. No effect if removeCurrentItems is false. + * @param {boolean} [removeCurrentItems = true] If truthy, deselect and remove currently-selected items. */ - clearOptions: function(silent) { + clearOptions: function(silent = false, removeCurrentItems = true) { var self = this; self.loadedSearches = {}; self.userOptions = {}; self.renderCache = {}; - var options = self.options; - $.each(self.options, function(key, value) { - if(self.items.indexOf(key) == -1) { - delete options[key]; - } - }); - self.options = self.sifter.items = options; + if (removeCurrentItems) { + self.options = self.sifter.items = {}; + } else { + var options = self.options; + $.each(self.options, function(key, value) { + if(self.items.indexOf(key) == -1) { + delete options[key]; + } + }); + self.options = self.sifter.items = options; + } self.lastQuery = null; self.trigger('option_clear'); - self.clear(silent); + if (removeCurrentItems) { + self.clear(silent); + } }, /** From 63cbc80febbecdcaff701f26ccd6a9b078be277e Mon Sep 17 00:00:00 2001 From: Dan Gealow Date: Wed, 9 Oct 2024 19:24:07 -0400 Subject: [PATCH 2/2] Add entry to CHANGELOG.md --- CHANGELOG.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f705493c..25305fc2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,21 @@ +## v0.16.0 [In development] + +### Breaking changes + +- `clearOptions()` now, by default, removes currently-selected items in addition to deselecting them, + as it did prior to selectize v0.12.5. + - This behavior is controlled by a new parameter `removeCurrentItems`, default `true`. + - If `removeCurrentItems` is set `false`, the function will instead leave currently-selected items + available *and selected*, as was the case from selectize v0.12.5, until v0.13.1 introduced + [#2146](https://github.com/selectize/selectize.js/issues/2146) (which this change fixes). + - For consistency, `silent` also has a (backwards-compatible) default value of `false` now. + - Thus, the full signature is now `clearOptions(silent = false, removeCurrentItems = true)` + + _@dvg-p4_ + ## v0.15.1 ยท 17 11 2022 - New feature: dynamically add option groups