Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a removeCurrentItems parameter to clearOptions() to allow for either intended behavior #2147

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@
<!-- Feel free to put either your handle and/or full name, according to
your privacy needs -->

## 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
Expand Down
29 changes: 18 additions & 11 deletions src/selectize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
},

/**
Expand Down