Skip to content

Commit

Permalink
Merge branch 'fix-clearChoices'
Browse files Browse the repository at this point in the history
  • Loading branch information
Xon committed Sep 4, 2024
2 parents 272ce75 + efcb764 commit a941e55
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 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
2 changes: 1 addition & 1 deletion test/scripts/choices.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,7 @@ describe('choices', () => {
});

it('dispatches clearChoices action', () => {
expect(storeResetStub.callCount).to.be.eq(1);
expect(storeResetStub.callCount).to.be.eq(0);
});
});

Expand Down

0 comments on commit a941e55

Please sign in to comment.