Skip to content

fix(material/list): fix tabindex="-1" not being maintained when disabled #25860

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/material/list/selection-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -806,8 +806,9 @@ describe('MDC-based MatSelectionList without forms', () => {
});

// when the entire list is disabled, its listitems should always have tabindex="-1"
it('should not put listitems in the tab order', () => {
it('should remove all listitems from the tab order when disabled state is enabled', () => {
fixture.componentInstance.disabled = false;
fixture.detectChanges();
let testListItem = listOption[2].injector.get<MatListOption>(MatListOption);
testListItem.focus();
fixture.detectChanges();
Expand All @@ -827,6 +828,26 @@ describe('MDC-based MatSelectionList without forms', () => {
.withContext('Expected all list options to be excluded from the tab order')
.toBe(0);
});

it('should not allow focusin event to change the tabindex', () => {
fixture.componentInstance.disabled = true;
fixture.detectChanges();

expect(
listOption.filter(option => option.nativeElement.getAttribute('tabindex') !== '-1').length,
)
.withContext('Expected all list options to be excluded from the tab order')
.toBe(0);

listOption[1].triggerEventHandler('focusin', {target: listOption[1].nativeElement});
fixture.detectChanges();

expect(
listOption.filter(option => option.nativeElement.getAttribute('tabindex') !== '-1').length,
)
.withContext('Expected all list options to be excluded from the tab order')
.toBe(0);
});
});

describe('with checkbox position after', () => {
Expand Down
16 changes: 14 additions & 2 deletions src/material/list/selection-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,10 @@ export class MatSelectionList

/** Handles focusin events within the list. */
private _handleFocusin = (event: FocusEvent) => {
if (this.disabled) {
return;
}

const activeIndex = this._items
.toArray()
.findIndex(item => item._elementRef.nativeElement.contains(event.target as HTMLElement));
Expand Down Expand Up @@ -402,7 +406,7 @@ export class MatSelectionList
.withHomeAndEnd()
.withTypeAhead()
.withWrap()
.skipPredicate(() => false);
.skipPredicate(() => this.disabled);

// Set the initial focus.
this._resetActiveOption();
Expand All @@ -429,8 +433,16 @@ export class MatSelectionList
this._keyManager.updateActiveItem(index);
}

/** Resets the active option to the first selected option. */
/**
* Resets the active option. When the list is disabled, remove all options from to the tab order.
* Otherwise, focus the first selected option.
*/
private _resetActiveOption() {
if (this.disabled) {
this._setActiveOption(-1);
return;
}

const activeItem =
this._items.find(item => item.selected && !item.disabled) || this._items.first;
this._setActiveOption(activeItem ? this._items.toArray().indexOf(activeItem) : -1);
Expand Down