From cde3cf2475fbf4111868472462039a06a75baf59 Mon Sep 17 00:00:00 2001 From: "Nichols, Kieran" Date: Sat, 21 Oct 2023 12:12:51 -0400 Subject: [PATCH] fix(select): fixed a bug where options that contain leading whitespace could not be selected via keyboard filtering --- src/lib/select/core/base-select-foundation.ts | 4 +++- src/test/spec/select/select.spec.ts | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/lib/select/core/base-select-foundation.ts b/src/lib/select/core/base-select-foundation.ts index 014e69eae..d5a127160 100644 --- a/src/lib/select/core/base-select-foundation.ts +++ b/src/lib/select/core/base-select-foundation.ts @@ -524,7 +524,9 @@ export abstract class BaseSelectFoundation extends }, 300); this._options = this._adapter.getOptions(); // TODO: Enhance this to cycle through closest matches (see the native select) - const matchedOption = this._flatOptions.find(option => !option.disabled && option.label.toLowerCase().startsWith(this._filterString.toLowerCase())); + const matchedOption = this._flatOptions.find(({ disabled, label }) => { + return !disabled && label.trim().toLowerCase().startsWith(this._filterString.trim().toLowerCase()); + }); if (matchedOption) { const optionIndex = this._flatOptions.indexOf(matchedOption); if (this._open) { diff --git a/src/test/spec/select/select.spec.ts b/src/test/spec/select/select.spec.ts index 9558eb721..9881453d2 100644 --- a/src/test/spec/select/select.spec.ts +++ b/src/test/spec/select/select.spec.ts @@ -833,6 +833,21 @@ describe('SelectComponent', function(this: ITestContext) { _expectPopupVisibility(this.context.component.popupElement, true); }); + it('should highlight first match when filtering while popup is open with leading whitespace', async function(this: ITestContext) { + this.context = setupTestContext(true); + const firstOption = this.context.component.querySelector('forge-option:first-child') as IOptionComponent; + firstOption.textContent = ' With Whitespace '; + await tick(); + + await _triggerPopupOpen(this.context.component); + + _sendFilterKey(this.context.component, 'w', 87); + await timer(); + + _expectActiveOption(this.context.component.popupElement, 0); + _expectPopupVisibility(this.context.component.popupElement, true); + }); + it('should highlight first match when filtering with uppercase characters', async function(this: ITestContext) { this.context = setupTestContext(true); await tick();