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

Fix form reset/clearStore #1181

Merged
merged 5 commits into from
Aug 29, 2024
Merged
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
32 changes: 15 additions & 17 deletions public/assets/scripts/choices.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@
active: active,
});
};
var clearChoices = function () { return ({
type: ActionType.CLEAR_CHOICES,
}); };

var addGroup = function (group) { return ({
type: ActionType.ADD_GROUP,
Expand Down Expand Up @@ -3849,19 +3846,19 @@
}
});
}
_this.clearStore();
choicesFromOptions.forEach(function (groupOrChoice) {
if ('choices' in groupOrChoice) {
return;
}
var choice = groupOrChoice;
if (deselectAll) {
choice.selected = false;
_this._store.dispatch(removeItem$1(choice));
}
else if (existingItems[choice.value]) {
choice.selected = true;
}
});
_this.clearStore();
/* @todo only generate add events for the added options instead of all
if (withEvents) {
items.forEach((choice) => {
Expand Down Expand Up @@ -3897,13 +3894,13 @@
return this;
};
Choices.prototype.clearChoices = function () {
this.passedElement.element.innerHTML = '';
this._store.dispatch(clearChoices());
// @todo integrate with Store
this._searcher.reset();
return this;
this.passedElement.element.replaceChildren('');
return this.clearStore();
};
Choices.prototype.clearStore = function () {
this.itemList.element.replaceChildren('');
this.choiceList.element.replaceChildren('');
this._clearNotice();
this._store.reset();
this._lastAddedChoiceId = 0;
this._lastAddedGroupId = 0;
Expand Down Expand Up @@ -3955,6 +3952,7 @@
return; // block rendering choices if the input limit is reached.
}
var _a = this, config = _a.config, isSearching = _a._isSearching;
var _b = this._store, activeGroups = _b.activeGroups, activeChoices = _b.activeChoices;
var renderLimit = 0;
if (isSearching && config.searchResultLimit > 0) {
renderLimit = config.searchResultLimit;
Expand All @@ -3963,7 +3961,7 @@
renderLimit = config.renderChoiceLimit;
}
if (this._isSelectElement) {
var backingOptions = this._store.activeChoices.filter(function (choice) { return !choice.element; });
var backingOptions = activeChoices.filter(function (choice) { return !choice.element; });
if (backingOptions.length) {
this.passedElement.addOptions(backingOptions);
}
Expand Down Expand Up @@ -3998,20 +3996,20 @@
return index < choiceLimit;
});
};
if (this._store.activeChoices.length) {
if (activeChoices.length) {
if (config.resetScrollPosition) {
requestAnimationFrame(function () { return _this.choiceList.scrollToTop(); });
}
if (!this._hasNonChoicePlaceholder && !isSearching && this._isSelectOneElement) {
// If we have a placeholder choice along with groups
renderChoices(this._store.activeChoices.filter(function (choice) { return choice.placeholder && !choice.groupId; }), false, undefined);
renderChoices(activeChoices.filter(function (choice) { return choice.placeholder && !choice.groupId; }), false, undefined);
}
// If we have grouped options
if (this._store.activeGroups.length && !isSearching) {
if (activeGroups.length && !isSearching) {
if (config.shouldSort) {
this._store.activeGroups.sort(config.sorter);
activeGroups.sort(config.sorter);
}
this._store.activeGroups.forEach(function (group) {
activeGroups.forEach(function (group) {
var groupChoices = renderableChoices(group.choices);
if (groupChoices.length) {
if (group.label) {
Expand All @@ -4025,7 +4023,7 @@
});
}
else {
renderChoices(renderableChoices(this._store.activeChoices), false, undefined);
renderChoices(renderableChoices(activeChoices), false, undefined);
}
}
var notice = this._notice;
Expand Down
2 changes: 1 addition & 1 deletion public/assets/scripts/choices.min.js

Large diffs are not rendered by default.

32 changes: 15 additions & 17 deletions public/assets/scripts/choices.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,6 @@ var activateChoices = function (active) {
active: active,
});
};
var clearChoices = function () { return ({
type: ActionType.CLEAR_CHOICES,
}); };

var addGroup = function (group) { return ({
type: ActionType.ADD_GROUP,
Expand Down Expand Up @@ -3843,19 +3840,19 @@ var Choices = /** @class */ (function () {
}
});
}
_this.clearStore();
choicesFromOptions.forEach(function (groupOrChoice) {
if ('choices' in groupOrChoice) {
return;
}
var choice = groupOrChoice;
if (deselectAll) {
choice.selected = false;
_this._store.dispatch(removeItem$1(choice));
}
else if (existingItems[choice.value]) {
choice.selected = true;
}
});
_this.clearStore();
/* @todo only generate add events for the added options instead of all
if (withEvents) {
items.forEach((choice) => {
Expand Down Expand Up @@ -3891,13 +3888,13 @@ var Choices = /** @class */ (function () {
return this;
};
Choices.prototype.clearChoices = function () {
this.passedElement.element.innerHTML = '';
this._store.dispatch(clearChoices());
// @todo integrate with Store
this._searcher.reset();
return this;
this.passedElement.element.replaceChildren('');
return this.clearStore();
};
Choices.prototype.clearStore = function () {
this.itemList.element.replaceChildren('');
this.choiceList.element.replaceChildren('');
this._clearNotice();
this._store.reset();
this._lastAddedChoiceId = 0;
this._lastAddedGroupId = 0;
Expand Down Expand Up @@ -3949,6 +3946,7 @@ var Choices = /** @class */ (function () {
return; // block rendering choices if the input limit is reached.
}
var _a = this, config = _a.config, isSearching = _a._isSearching;
var _b = this._store, activeGroups = _b.activeGroups, activeChoices = _b.activeChoices;
var renderLimit = 0;
if (isSearching && config.searchResultLimit > 0) {
renderLimit = config.searchResultLimit;
Expand All @@ -3957,7 +3955,7 @@ var Choices = /** @class */ (function () {
renderLimit = config.renderChoiceLimit;
}
if (this._isSelectElement) {
var backingOptions = this._store.activeChoices.filter(function (choice) { return !choice.element; });
var backingOptions = activeChoices.filter(function (choice) { return !choice.element; });
if (backingOptions.length) {
this.passedElement.addOptions(backingOptions);
}
Expand Down Expand Up @@ -3992,20 +3990,20 @@ var Choices = /** @class */ (function () {
return index < choiceLimit;
});
};
if (this._store.activeChoices.length) {
if (activeChoices.length) {
if (config.resetScrollPosition) {
requestAnimationFrame(function () { return _this.choiceList.scrollToTop(); });
}
if (!this._hasNonChoicePlaceholder && !isSearching && this._isSelectOneElement) {
// If we have a placeholder choice along with groups
renderChoices(this._store.activeChoices.filter(function (choice) { return choice.placeholder && !choice.groupId; }), false, undefined);
renderChoices(activeChoices.filter(function (choice) { return choice.placeholder && !choice.groupId; }), false, undefined);
}
// If we have grouped options
if (this._store.activeGroups.length && !isSearching) {
if (activeGroups.length && !isSearching) {
if (config.shouldSort) {
this._store.activeGroups.sort(config.sorter);
activeGroups.sort(config.sorter);
}
this._store.activeGroups.forEach(function (group) {
activeGroups.forEach(function (group) {
var groupChoices = renderableChoices(group.choices);
if (groupChoices.length) {
if (group.label) {
Expand All @@ -4019,7 +4017,7 @@ var Choices = /** @class */ (function () {
});
}
else {
renderChoices(renderableChoices(this._store.activeChoices), false, undefined);
renderChoices(renderableChoices(activeChoices), false, undefined);
}
}
var notice = this._notice;
Expand Down
32 changes: 15 additions & 17 deletions public/assets/scripts/choices.search-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@
active: active,
});
};
var clearChoices = function () { return ({
type: ActionType.CLEAR_CHOICES,
}); };

var addGroup = function (group) { return ({
type: ActionType.ADD_GROUP,
Expand Down Expand Up @@ -3367,19 +3364,19 @@
}
});
}
_this.clearStore();
choicesFromOptions.forEach(function (groupOrChoice) {
if ('choices' in groupOrChoice) {
return;
}
var choice = groupOrChoice;
if (deselectAll) {
choice.selected = false;
_this._store.dispatch(removeItem$1(choice));
}
else if (existingItems[choice.value]) {
choice.selected = true;
}
});
_this.clearStore();
/* @todo only generate add events for the added options instead of all
if (withEvents) {
items.forEach((choice) => {
Expand Down Expand Up @@ -3415,13 +3412,13 @@
return this;
};
Choices.prototype.clearChoices = function () {
this.passedElement.element.innerHTML = '';
this._store.dispatch(clearChoices());
// @todo integrate with Store
this._searcher.reset();
return this;
this.passedElement.element.replaceChildren('');
return this.clearStore();
};
Choices.prototype.clearStore = function () {
this.itemList.element.replaceChildren('');
this.choiceList.element.replaceChildren('');
this._clearNotice();
this._store.reset();
this._lastAddedChoiceId = 0;
this._lastAddedGroupId = 0;
Expand Down Expand Up @@ -3473,6 +3470,7 @@
return; // block rendering choices if the input limit is reached.
}
var _a = this, config = _a.config, isSearching = _a._isSearching;
var _b = this._store, activeGroups = _b.activeGroups, activeChoices = _b.activeChoices;
var renderLimit = 0;
if (isSearching && config.searchResultLimit > 0) {
renderLimit = config.searchResultLimit;
Expand All @@ -3481,7 +3479,7 @@
renderLimit = config.renderChoiceLimit;
}
if (this._isSelectElement) {
var backingOptions = this._store.activeChoices.filter(function (choice) { return !choice.element; });
var backingOptions = activeChoices.filter(function (choice) { return !choice.element; });
if (backingOptions.length) {
this.passedElement.addOptions(backingOptions);
}
Expand Down Expand Up @@ -3516,20 +3514,20 @@
return index < choiceLimit;
});
};
if (this._store.activeChoices.length) {
if (activeChoices.length) {
if (config.resetScrollPosition) {
requestAnimationFrame(function () { return _this.choiceList.scrollToTop(); });
}
if (!this._hasNonChoicePlaceholder && !isSearching && this._isSelectOneElement) {
// If we have a placeholder choice along with groups
renderChoices(this._store.activeChoices.filter(function (choice) { return choice.placeholder && !choice.groupId; }), false, undefined);
renderChoices(activeChoices.filter(function (choice) { return choice.placeholder && !choice.groupId; }), false, undefined);
}
// If we have grouped options
if (this._store.activeGroups.length && !isSearching) {
if (activeGroups.length && !isSearching) {
if (config.shouldSort) {
this._store.activeGroups.sort(config.sorter);
activeGroups.sort(config.sorter);
}
this._store.activeGroups.forEach(function (group) {
activeGroups.forEach(function (group) {
var groupChoices = renderableChoices(group.choices);
if (groupChoices.length) {
if (group.label) {
Expand All @@ -3543,7 +3541,7 @@
});
}
else {
renderChoices(renderableChoices(this._store.activeChoices), false, undefined);
renderChoices(renderableChoices(activeChoices), false, undefined);
}
}
var notice = this._notice;
Expand Down
2 changes: 1 addition & 1 deletion public/assets/scripts/choices.search-basic.min.js

Large diffs are not rendered by default.

Loading
Loading