Skip to content

Commit

Permalink
fix(select): fixed a bug where options that contain leading whitespac…
Browse files Browse the repository at this point in the history
…e could not be selected via keyboard filtering
  • Loading branch information
DRiFTy17 committed Oct 21, 2023
1 parent 11d573d commit cde3cf2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/lib/select/core/base-select-foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,9 @@ export abstract class BaseSelectFoundation<T extends IBaseSelectAdapter> 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) {
Expand Down
15 changes: 15 additions & 0 deletions src/test/spec/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit cde3cf2

Please sign in to comment.