Skip to content

Commit 039c81d

Browse files
authored
Merge pull request #1283 from Choices-js/fix-clear-choices
Fix setChoices & clearChoices related regressions
2 parents 0acba2b + 51654a2 commit 039c81d

16 files changed

+384
-104
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

public/assets/scripts/choices.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3822,15 +3822,9 @@
38223822
if (clearSearchFlag) {
38233823
_this._isSearching = false;
38243824
}
3825-
var items = {};
3826-
if (!replaceItems) {
3827-
_this._store.items.forEach(function (item) {
3828-
items[item.value] = item;
3829-
});
3830-
}
38313825
// Clear choices if needed
38323826
if (replaceChoices) {
3833-
_this.clearChoices();
3827+
_this.clearChoices(true, replaceItems);
38343828
}
38353829
var isDefaultValue = value === 'value';
38363830
var isDefaultLabel = label === 'label';
@@ -3848,9 +3842,6 @@
38483842
choice = __assign(__assign({}, choice), { value: choice[value], label: choice[label] });
38493843
}
38503844
var choiceFull = mapInputToChoice(choice, false);
3851-
if (!replaceItems && choiceFull.value in items) {
3852-
choiceFull.selected = true;
3853-
}
38543845
_this._addChoice(choiceFull);
38553846
if (choiceFull.placeholder && !_this._hasNonChoicePlaceholder) {
38563847
_this._placeholderValue = unwrapStringForEscaped(choiceFull.label);
@@ -3936,22 +3927,38 @@
39363927
}
39373928
return this;
39383929
};
3939-
Choices.prototype.clearChoices = function (clearOptions) {
3930+
Choices.prototype.clearChoices = function (clearOptions, clearItems) {
3931+
var _this = this;
39403932
if (clearOptions === void 0) { clearOptions = true; }
3933+
if (clearItems === void 0) { clearItems = false; }
39413934
if (clearOptions) {
3942-
this.passedElement.element.replaceChildren('');
3935+
if (clearItems) {
3936+
this.passedElement.element.replaceChildren('');
3937+
}
3938+
else {
3939+
this.passedElement.element.querySelectorAll(':not([selected])').forEach(function (el) {
3940+
el.remove();
3941+
});
3942+
}
39433943
}
39443944
this.itemList.element.replaceChildren('');
39453945
this.choiceList.element.replaceChildren('');
39463946
this._clearNotice();
3947-
this._store.reset();
3947+
this._store.withTxn(function () {
3948+
var items = clearItems ? [] : _this._store.items;
3949+
_this._store.reset();
3950+
items.forEach(function (item) {
3951+
_this._store.dispatch(addChoice(item));
3952+
_this._store.dispatch(addItem(item));
3953+
});
3954+
});
39483955
// @todo integrate with Store
39493956
this._searcher.reset();
39503957
return this;
39513958
};
39523959
Choices.prototype.clearStore = function (clearOptions) {
39533960
if (clearOptions === void 0) { clearOptions = true; }
3954-
this.clearChoices(clearOptions);
3961+
this.clearChoices(clearOptions, true);
39553962
this._stopSearch();
39563963
this._lastAddedChoiceId = 0;
39573964
this._lastAddedGroupId = 0;

public/assets/scripts/choices.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/assets/scripts/choices.mjs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3816,15 +3816,9 @@ var Choices = /** @class */ (function () {
38163816
if (clearSearchFlag) {
38173817
_this._isSearching = false;
38183818
}
3819-
var items = {};
3820-
if (!replaceItems) {
3821-
_this._store.items.forEach(function (item) {
3822-
items[item.value] = item;
3823-
});
3824-
}
38253819
// Clear choices if needed
38263820
if (replaceChoices) {
3827-
_this.clearChoices();
3821+
_this.clearChoices(true, replaceItems);
38283822
}
38293823
var isDefaultValue = value === 'value';
38303824
var isDefaultLabel = label === 'label';
@@ -3842,9 +3836,6 @@ var Choices = /** @class */ (function () {
38423836
choice = __assign(__assign({}, choice), { value: choice[value], label: choice[label] });
38433837
}
38443838
var choiceFull = mapInputToChoice(choice, false);
3845-
if (!replaceItems && choiceFull.value in items) {
3846-
choiceFull.selected = true;
3847-
}
38483839
_this._addChoice(choiceFull);
38493840
if (choiceFull.placeholder && !_this._hasNonChoicePlaceholder) {
38503841
_this._placeholderValue = unwrapStringForEscaped(choiceFull.label);
@@ -3930,22 +3921,38 @@ var Choices = /** @class */ (function () {
39303921
}
39313922
return this;
39323923
};
3933-
Choices.prototype.clearChoices = function (clearOptions) {
3924+
Choices.prototype.clearChoices = function (clearOptions, clearItems) {
3925+
var _this = this;
39343926
if (clearOptions === void 0) { clearOptions = true; }
3927+
if (clearItems === void 0) { clearItems = false; }
39353928
if (clearOptions) {
3936-
this.passedElement.element.replaceChildren('');
3929+
if (clearItems) {
3930+
this.passedElement.element.replaceChildren('');
3931+
}
3932+
else {
3933+
this.passedElement.element.querySelectorAll(':not([selected])').forEach(function (el) {
3934+
el.remove();
3935+
});
3936+
}
39373937
}
39383938
this.itemList.element.replaceChildren('');
39393939
this.choiceList.element.replaceChildren('');
39403940
this._clearNotice();
3941-
this._store.reset();
3941+
this._store.withTxn(function () {
3942+
var items = clearItems ? [] : _this._store.items;
3943+
_this._store.reset();
3944+
items.forEach(function (item) {
3945+
_this._store.dispatch(addChoice(item));
3946+
_this._store.dispatch(addItem(item));
3947+
});
3948+
});
39423949
// @todo integrate with Store
39433950
this._searcher.reset();
39443951
return this;
39453952
};
39463953
Choices.prototype.clearStore = function (clearOptions) {
39473954
if (clearOptions === void 0) { clearOptions = true; }
3948-
this.clearChoices(clearOptions);
3955+
this.clearChoices(clearOptions, true);
39493956
this._stopSearch();
39503957
this._lastAddedChoiceId = 0;
39513958
this._lastAddedGroupId = 0;

public/assets/scripts/choices.search-basic.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3340,15 +3340,9 @@
33403340
if (clearSearchFlag) {
33413341
_this._isSearching = false;
33423342
}
3343-
var items = {};
3344-
if (!replaceItems) {
3345-
_this._store.items.forEach(function (item) {
3346-
items[item.value] = item;
3347-
});
3348-
}
33493343
// Clear choices if needed
33503344
if (replaceChoices) {
3351-
_this.clearChoices();
3345+
_this.clearChoices(true, replaceItems);
33523346
}
33533347
var isDefaultValue = value === 'value';
33543348
var isDefaultLabel = label === 'label';
@@ -3366,9 +3360,6 @@
33663360
choice = __assign(__assign({}, choice), { value: choice[value], label: choice[label] });
33673361
}
33683362
var choiceFull = mapInputToChoice(choice, false);
3369-
if (!replaceItems && choiceFull.value in items) {
3370-
choiceFull.selected = true;
3371-
}
33723363
_this._addChoice(choiceFull);
33733364
if (choiceFull.placeholder && !_this._hasNonChoicePlaceholder) {
33743365
_this._placeholderValue = unwrapStringForEscaped(choiceFull.label);
@@ -3454,22 +3445,38 @@
34543445
}
34553446
return this;
34563447
};
3457-
Choices.prototype.clearChoices = function (clearOptions) {
3448+
Choices.prototype.clearChoices = function (clearOptions, clearItems) {
3449+
var _this = this;
34583450
if (clearOptions === void 0) { clearOptions = true; }
3451+
if (clearItems === void 0) { clearItems = false; }
34593452
if (clearOptions) {
3460-
this.passedElement.element.replaceChildren('');
3453+
if (clearItems) {
3454+
this.passedElement.element.replaceChildren('');
3455+
}
3456+
else {
3457+
this.passedElement.element.querySelectorAll(':not([selected])').forEach(function (el) {
3458+
el.remove();
3459+
});
3460+
}
34613461
}
34623462
this.itemList.element.replaceChildren('');
34633463
this.choiceList.element.replaceChildren('');
34643464
this._clearNotice();
3465-
this._store.reset();
3465+
this._store.withTxn(function () {
3466+
var items = clearItems ? [] : _this._store.items;
3467+
_this._store.reset();
3468+
items.forEach(function (item) {
3469+
_this._store.dispatch(addChoice(item));
3470+
_this._store.dispatch(addItem(item));
3471+
});
3472+
});
34663473
// @todo integrate with Store
34673474
this._searcher.reset();
34683475
return this;
34693476
};
34703477
Choices.prototype.clearStore = function (clearOptions) {
34713478
if (clearOptions === void 0) { clearOptions = true; }
3472-
this.clearChoices(clearOptions);
3479+
this.clearChoices(clearOptions, true);
34733480
this._stopSearch();
34743481
this._lastAddedChoiceId = 0;
34753482
this._lastAddedGroupId = 0;

public/assets/scripts/choices.search-basic.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/assets/scripts/choices.search-basic.mjs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3334,15 +3334,9 @@ var Choices = /** @class */ (function () {
33343334
if (clearSearchFlag) {
33353335
_this._isSearching = false;
33363336
}
3337-
var items = {};
3338-
if (!replaceItems) {
3339-
_this._store.items.forEach(function (item) {
3340-
items[item.value] = item;
3341-
});
3342-
}
33433337
// Clear choices if needed
33443338
if (replaceChoices) {
3345-
_this.clearChoices();
3339+
_this.clearChoices(true, replaceItems);
33463340
}
33473341
var isDefaultValue = value === 'value';
33483342
var isDefaultLabel = label === 'label';
@@ -3360,9 +3354,6 @@ var Choices = /** @class */ (function () {
33603354
choice = __assign(__assign({}, choice), { value: choice[value], label: choice[label] });
33613355
}
33623356
var choiceFull = mapInputToChoice(choice, false);
3363-
if (!replaceItems && choiceFull.value in items) {
3364-
choiceFull.selected = true;
3365-
}
33663357
_this._addChoice(choiceFull);
33673358
if (choiceFull.placeholder && !_this._hasNonChoicePlaceholder) {
33683359
_this._placeholderValue = unwrapStringForEscaped(choiceFull.label);
@@ -3448,22 +3439,38 @@ var Choices = /** @class */ (function () {
34483439
}
34493440
return this;
34503441
};
3451-
Choices.prototype.clearChoices = function (clearOptions) {
3442+
Choices.prototype.clearChoices = function (clearOptions, clearItems) {
3443+
var _this = this;
34523444
if (clearOptions === void 0) { clearOptions = true; }
3445+
if (clearItems === void 0) { clearItems = false; }
34533446
if (clearOptions) {
3454-
this.passedElement.element.replaceChildren('');
3447+
if (clearItems) {
3448+
this.passedElement.element.replaceChildren('');
3449+
}
3450+
else {
3451+
this.passedElement.element.querySelectorAll(':not([selected])').forEach(function (el) {
3452+
el.remove();
3453+
});
3454+
}
34553455
}
34563456
this.itemList.element.replaceChildren('');
34573457
this.choiceList.element.replaceChildren('');
34583458
this._clearNotice();
3459-
this._store.reset();
3459+
this._store.withTxn(function () {
3460+
var items = clearItems ? [] : _this._store.items;
3461+
_this._store.reset();
3462+
items.forEach(function (item) {
3463+
_this._store.dispatch(addChoice(item));
3464+
_this._store.dispatch(addItem(item));
3465+
});
3466+
});
34603467
// @todo integrate with Store
34613468
this._searcher.reset();
34623469
return this;
34633470
};
34643471
Choices.prototype.clearStore = function (clearOptions) {
34653472
if (clearOptions === void 0) { clearOptions = true; }
3466-
this.clearChoices(clearOptions);
3473+
this.clearChoices(clearOptions, true);
34673474
this._stopSearch();
34683475
this._lastAddedChoiceId = 0;
34693476
this._lastAddedGroupId = 0;

public/assets/scripts/choices.search-prefix.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2182,15 +2182,9 @@
21822182
if (clearSearchFlag) {
21832183
_this._isSearching = false;
21842184
}
2185-
var items = {};
2186-
if (!replaceItems) {
2187-
_this._store.items.forEach(function (item) {
2188-
items[item.value] = item;
2189-
});
2190-
}
21912185
// Clear choices if needed
21922186
if (replaceChoices) {
2193-
_this.clearChoices();
2187+
_this.clearChoices(true, replaceItems);
21942188
}
21952189
var isDefaultValue = value === 'value';
21962190
var isDefaultLabel = label === 'label';
@@ -2208,9 +2202,6 @@
22082202
choice = __assign(__assign({}, choice), { value: choice[value], label: choice[label] });
22092203
}
22102204
var choiceFull = mapInputToChoice(choice, false);
2211-
if (!replaceItems && choiceFull.value in items) {
2212-
choiceFull.selected = true;
2213-
}
22142205
_this._addChoice(choiceFull);
22152206
if (choiceFull.placeholder && !_this._hasNonChoicePlaceholder) {
22162207
_this._placeholderValue = unwrapStringForEscaped(choiceFull.label);
@@ -2296,22 +2287,38 @@
22962287
}
22972288
return this;
22982289
};
2299-
Choices.prototype.clearChoices = function (clearOptions) {
2290+
Choices.prototype.clearChoices = function (clearOptions, clearItems) {
2291+
var _this = this;
23002292
if (clearOptions === void 0) { clearOptions = true; }
2293+
if (clearItems === void 0) { clearItems = false; }
23012294
if (clearOptions) {
2302-
this.passedElement.element.replaceChildren('');
2295+
if (clearItems) {
2296+
this.passedElement.element.replaceChildren('');
2297+
}
2298+
else {
2299+
this.passedElement.element.querySelectorAll(':not([selected])').forEach(function (el) {
2300+
el.remove();
2301+
});
2302+
}
23032303
}
23042304
this.itemList.element.replaceChildren('');
23052305
this.choiceList.element.replaceChildren('');
23062306
this._clearNotice();
2307-
this._store.reset();
2307+
this._store.withTxn(function () {
2308+
var items = clearItems ? [] : _this._store.items;
2309+
_this._store.reset();
2310+
items.forEach(function (item) {
2311+
_this._store.dispatch(addChoice(item));
2312+
_this._store.dispatch(addItem(item));
2313+
});
2314+
});
23082315
// @todo integrate with Store
23092316
this._searcher.reset();
23102317
return this;
23112318
};
23122319
Choices.prototype.clearStore = function (clearOptions) {
23132320
if (clearOptions === void 0) { clearOptions = true; }
2314-
this.clearChoices(clearOptions);
2321+
this.clearChoices(clearOptions, true);
23152322
this._stopSearch();
23162323
this._lastAddedChoiceId = 0;
23172324
this._lastAddedGroupId = 0;

public/assets/scripts/choices.search-prefix.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)