Skip to content

Commit f1e82da

Browse files
authored
fix(material/list): indexOf usage incorreect for active focus reset (#28531)
The buggy logic was using a truthiness check for checking for presence in a list, but this check needed to compare against -1 instead. This corrects a mismanagement of tabIndex on the items in the selection list: if the list of items changed, the selection list could not be tab selected until the selection list was mouse-focused.
1 parent a3b28df commit f1e82da

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/material/list/selection-list.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,21 @@ describe('MDC-based MatSelectionList with forms', () => {
13191319
.toBe(1);
13201320
}));
13211321

1322+
it('should focus the first option when the list items are changed', fakeAsync(() => {
1323+
fixture.componentInstance.options = ['first option', 'second option'];
1324+
fixture.detectChanges();
1325+
1326+
tick();
1327+
1328+
const listElements = fixture.debugElement
1329+
.queryAll(By.directive(MatListOption))
1330+
.map(optionDebugEl => optionDebugEl.nativeElement);
1331+
1332+
expect(listElements.length).toBe(2);
1333+
expect(listElements[0].tabIndex).toBe(0);
1334+
expect(listElements[1].tabIndex).toBe(-1);
1335+
}));
1336+
13221337
it('should not mark the model as touched when the list is blurred', fakeAsync(() => {
13231338
expect(ngModel.touched)
13241339
.withContext('Expected the selection-list to be untouched by default.')

src/material/list/selection-list.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ export class MatSelectionList
429429
this._items.changes.pipe(takeUntil(this._destroyed)).subscribe(() => {
430430
const activeItem = this._keyManager.activeItem;
431431

432-
if (!activeItem || !this._items.toArray().indexOf(activeItem)) {
432+
if (!activeItem || this._items.toArray().indexOf(activeItem) === -1) {
433433
this._resetActiveOption();
434434
}
435435
});

0 commit comments

Comments
 (0)