Skip to content

Commit a65f0de

Browse files
committed
Preserve selected items by default in clearChoices
1 parent 1b969b4 commit a65f0de

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1168,11 +1168,13 @@ example.setChoices(
11681168
);
11691169
```
11701170
1171-
### clearChoices();
1171+
### clearChoices(clearOptions: boolean = true, clearItems: boolean = false);
11721172
11731173
**Input types affected:** `select-one`, `select-multiple`
11741174
11751175
**Usage:** Clear all choices from select including any selected items. Does **not** reset the search state.
1176+
- `clearOptions` If true, clears the backing options from the `<select>` element
1177+
- `clearItems` If false, preserves selected items instead of clearing them
11761178
11771179
### getValue(valueOnly?: boolean): string[] | EventChoice[] | EventChoice | string;
11781180

src/scripts/choices.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -727,15 +727,9 @@ class Choices {
727727
if (clearSearchFlag) {
728728
this._isSearching = false;
729729
}
730-
const items = {};
731-
if (!replaceItems) {
732-
this._store.items.forEach((item) => {
733-
items[item.value] = item;
734-
});
735-
}
736730
// Clear choices if needed
737731
if (replaceChoices) {
738-
this.clearChoices();
732+
this.clearChoices(true, replaceItems);
739733
}
740734
const isDefaultValue = value === 'value';
741735
const isDefaultLabel = label === 'label';
@@ -761,9 +755,6 @@ class Choices {
761755
} as InputChoice;
762756
}
763757
const choiceFull = mapInputToChoice<InputChoice>(choice, false);
764-
if (!replaceItems && choiceFull.value in items) {
765-
choiceFull.selected = true;
766-
}
767758
this._addChoice(choiceFull);
768759
if (choiceFull.placeholder && !this._hasNonChoicePlaceholder) {
769760
this._placeholderValue = unwrapStringForEscaped(choiceFull.label);
@@ -863,23 +854,35 @@ class Choices {
863854
return this;
864855
}
865856

866-
clearChoices(clearOptions: boolean = true): this {
857+
clearChoices(clearOptions: boolean = true, clearItems: boolean = false): this {
867858
if (clearOptions) {
868-
this.passedElement.element.replaceChildren('');
859+
if (clearItems) {
860+
this.passedElement.element.replaceChildren('');
861+
} else {
862+
this.passedElement.element.querySelectorAll(':not([selected])').forEach((el): void => {
863+
el.remove();
864+
});
865+
}
869866
}
870867
this.itemList.element.replaceChildren('');
871868
this.choiceList.element.replaceChildren('');
872869
this._clearNotice();
873-
this._store.reset();
874-
870+
this._store.withTxn(() => {
871+
const items = clearItems ? [] : this._store.items;
872+
this._store.reset();
873+
items.forEach((item: ChoiceFull): void => {
874+
this._store.dispatch(addChoice(item));
875+
this._store.dispatch(addItem(item));
876+
});
877+
});
875878
// @todo integrate with Store
876879
this._searcher.reset();
877880

878881
return this;
879882
}
880883

881884
clearStore(clearOptions: boolean = true): this {
882-
this.clearChoices(clearOptions);
885+
this.clearChoices(clearOptions, true);
883886
this._stopSearch();
884887
this._lastAddedChoiceId = 0;
885888
this._lastAddedGroupId = 0;

0 commit comments

Comments
 (0)