From f00aedd9107589fcd9096c4550ccce86f91c1bad Mon Sep 17 00:00:00 2001 From: Xon <635541+Xon@users.noreply.github.com> Date: Thu, 29 Aug 2024 14:37:23 +0800 Subject: [PATCH 1/5] Avoid iterating the active choices list multiple times --- src/scripts/choices.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/scripts/choices.ts b/src/scripts/choices.ts index 2edc755f..df03ccbd 100644 --- a/src/scripts/choices.ts +++ b/src/scripts/choices.ts @@ -909,6 +909,7 @@ class Choices { } const { config, _isSearching: isSearching } = this; + const { activeGroups, activeChoices } = this._store; let renderLimit = 0; if (isSearching && config.searchResultLimit > 0) { @@ -918,7 +919,7 @@ class Choices { } if (this._isSelectElement) { - const backingOptions = this._store.activeChoices.filter((choice) => !choice.element); + const backingOptions = activeChoices.filter((choice) => !choice.element); if (backingOptions.length) { (this.passedElement as WrappedSelect).addOptions(backingOptions); } @@ -959,7 +960,7 @@ class Choices { }); }; - if (this._store.activeChoices.length) { + if (activeChoices.length) { if (config.resetScrollPosition) { requestAnimationFrame(() => this.choiceList.scrollToTop()); } @@ -967,19 +968,19 @@ class Choices { if (!this._hasNonChoicePlaceholder && !isSearching && this._isSelectOneElement) { // If we have a placeholder choice along with groups renderChoices( - this._store.activeChoices.filter((choice) => choice.placeholder && !choice.groupId), + activeChoices.filter((choice) => 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((group) => { + activeGroups.forEach((group) => { const groupChoices = renderableChoices(group.choices); if (groupChoices.length) { if (group.label) { @@ -992,7 +993,7 @@ class Choices { } }); } else { - renderChoices(renderableChoices(this._store.activeChoices), false, undefined); + renderChoices(renderableChoices(activeChoices), false, undefined); } } From daa67f4545b857358161d9bc26cfb3e11c171efb Mon Sep 17 00:00:00 2001 From: Xon <635541+Xon@users.noreply.github.com> Date: Thu, 29 Aug 2024 15:05:46 +0800 Subject: [PATCH 2/5] Add e2e test for reset button --- public/index.html | 2 +- test-e2e/tests/demo-page.spec.ts | 55 ++++++++++++++++++++++---------- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/public/index.html b/public/index.html index 9ab7f291..cfd91781 100644 --- a/public/index.html +++ b/public/index.html @@ -521,7 +521,7 @@
Change the values and press reset to restore to initial state.
-