Skip to content

Commit

Permalink
Untangle clearStore and clearChoices
Browse files Browse the repository at this point in the history
- Fix `clearChoices()` would also remove items, and clearing the search flag.
- Fix `clearStore()` was wiping options
  • Loading branch information
Xon committed Sep 4, 2024
1 parent acd1db0 commit 4b0b4f5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Fix regression where webpack doesn't permit importing scss/css @tagliala [#1193](https://github.com/Choices-js/Choices/issues/1193)
* Fix regression "no choices to choose from"/"no results found" notice did not reliably trigger. [#1185](https://github.com/Choices-js/Choices/issues/1185) [#1191](https://github.com/Choices-js/Choices/issues/1191)
* Fix regression of UnhighlightItem event not firing [#1173](https://github.com/Choices-js/Choices/issues/1173)
* Fix `clearChoices()` would remove items, and clear the search flag.

### Chore
* Add e2e tests for "no choices" behavior to match v10
Expand Down
29 changes: 20 additions & 9 deletions src/scripts/choices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ class Choices {
this.containerOuter.unwrap(this.passedElement.element);

this._store._listeners = []; // prevents select/input value being wiped
this.clearStore();
this.clearStore(false);
this._stopSearch();

this._templates = Choices.defaults.templates;
Expand Down Expand Up @@ -782,7 +782,7 @@ class Choices {
});
}

this.clearStore();
this.clearStore(false);

choicesFromOptions.forEach((groupOrChoice) => {
if ('choices' in groupOrChoice) {
Expand Down Expand Up @@ -839,12 +839,24 @@ class Choices {
}

clearChoices(): this {
this.passedElement.element.replaceChildren('');
this._store.withTxn(() => {
this._store.choices.forEach((choice) => {
if (!choice.selected) {
this._store.dispatch(removeChoice(choice));
}
});
});

// @todo integrate with Store
this._searcher.reset();

return this.clearStore();
return this;
}

clearStore(): this {
this._stopSearch();

this.passedElement.element.replaceChildren('');
this.itemList.element.replaceChildren('');
this.choiceList.element.replaceChildren('');
this._stopSearch();
Expand Down Expand Up @@ -1454,11 +1466,10 @@ class Choices {
}

_stopSearch(): void {
const wasSearching = this._isSearching;
this._currentValue = '';
this._isSearching = false;
this._clearNotice();
if (wasSearching) {
if (this._isSearching) {
this._currentValue = '';
this._isSearching = false;
this._clearNotice();
this._store.dispatch(activateChoices(true));

this.passedElement.triggerEvent(EventType.search, {
Expand Down

0 comments on commit 4b0b4f5

Please sign in to comment.