Skip to content

Commit

Permalink
Fixing indexOf bug when selection-list handles changes in items.
Browse files Browse the repository at this point in the history
The buggy logic was using a truthiness check for checking for presence
in a list; this check needs to compare against -1 instead.
  • Loading branch information
dyoo committed Feb 3, 2024
1 parent 8fab892 commit 6f46368
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/material/list/selection-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,21 @@ describe('MDC-based MatSelectionList with forms', () => {
.toBe(1);
}));

it('should focus the first option when the list items are changed', fakeAsync(() => {
fixture.componentInstance.options = ['first option', 'second option'];
fixture.detectChanges();

tick();

const listElements = fixture.debugElement
.queryAll(By.directive(MatListOption))
.map(optionDebugEl => optionDebugEl.nativeElement);

expect(listElements.length).toBe(2);
expect(listElements[0].tabIndex).toBe(0);
expect(listElements[1].tabIndex).toBe(-1);
}));

it('should not mark the model as touched when the list is blurred', fakeAsync(() => {
expect(ngModel.touched)
.withContext('Expected the selection-list to be untouched by default.')
Expand Down
2 changes: 1 addition & 1 deletion src/material/list/selection-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ export class MatSelectionList
this._items.changes.pipe(takeUntil(this._destroyed)).subscribe(() => {
const activeItem = this._keyManager.activeItem;

if (!activeItem || !this._items.toArray().indexOf(activeItem)) {
if (!activeItem || this._items.toArray().indexOf(activeItem) === -1) {
this._resetActiveOption();
}
});
Expand Down

0 comments on commit 6f46368

Please sign in to comment.