Skip to content

fix(selection-list): tabIndex should respect disabled state #7039

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
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
56 changes: 56 additions & 0 deletions src/lib/list/selection-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,49 @@ describe('MatSelectionList', () => {
});
});

describe('with tabindex', () => {

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [MatListModule],
declarations: [
SelectionListWithTabindexAttr,
SelectionListWithTabindexBinding,
]
});

TestBed.compileComponents();
}));

it('should properly handle native tabindex attribute', () => {
const fixture = TestBed.createComponent(SelectionListWithTabindexAttr);
const selectionList = fixture.debugElement.query(By.directive(MatSelectionList));

expect(selectionList.componentInstance.tabIndex)
.toBe(5, 'Expected the selection-list tabindex to be set to the attribute value.');
});

it('should support changing the tabIndex through binding', () => {
const fixture = TestBed.createComponent(SelectionListWithTabindexBinding);
const selectionList = fixture.debugElement.query(By.directive(MatSelectionList));

expect(selectionList.componentInstance.tabIndex)
.toBe(0, 'Expected the tabIndex to be set to "0" by default.');

fixture.componentInstance.tabIndex = 3;
fixture.detectChanges();

expect(selectionList.componentInstance.tabIndex)
.toBe(3, 'Expected the tabIndex to updated through binding.');

fixture.componentInstance.disabled = true;
fixture.detectChanges();

expect(selectionList.componentInstance.tabIndex)
.toBe(-1, 'Expected the tabIndex to be set to "-1" if selection list is disabled.');
});
});

describe('with single option', () => {
let fixture: ComponentFixture<SelectionListWithOnlyOneOption>;
let listOption: DebugElement;
Expand Down Expand Up @@ -523,3 +566,16 @@ class SelectionListWithSelectedOption {
</mat-selection-list>`})
class SelectionListWithOnlyOneOption {
}

@Component({
template: `<mat-selection-list tabindex="5"></mat-selection-list>`
})
class SelectionListWithTabindexAttr {}

@Component({
template: `<mat-selection-list [tabIndex]="tabIndex" [disabled]="disabled"></mat-selection-list>`
})
class SelectionListWithTabindexBinding {
tabIndex: number;
disabled: boolean;
}
25 changes: 12 additions & 13 deletions src/lib/list/selection-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {SelectionModel} from '@angular/cdk/collections';
import {SPACE} from '@angular/cdk/keycodes';
import {
AfterContentInit,
Attribute,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
Expand All @@ -32,16 +33,19 @@ import {
import {
CanDisable,
CanDisableRipple,
HasTabIndex,
MatLine,
MatLineSetter,
mixinDisabled,
mixinDisableRipple,
mixinTabIndex,
} from '@angular/material/core';


/** @docs-private */
export class MatSelectionListBase {}
export const _MatSelectionListMixinBase = mixinDisableRipple(mixinDisabled(MatSelectionListBase));
export const _MatSelectionListMixinBase =
mixinTabIndex(mixinDisableRipple(mixinDisabled(MatSelectionListBase)));

/** @docs-private */
export class MatListOptionBase {}
Expand Down Expand Up @@ -188,10 +192,10 @@ export class MatListOption extends _MatListOptionMixinBase
moduleId: module.id,
selector: 'mat-selection-list',
exportAs: 'matSelectionList',
inputs: ['disabled', 'disableRipple'],
inputs: ['disabled', 'disableRipple', 'tabIndex'],
host: {
'role': 'listbox',
'[attr.tabindex]': '_tabIndex',
'[tabIndex]': 'tabIndex',
'class': 'mat-selection-list',
'(focus)': 'focus()',
'(keydown)': '_keydown($event)',
Expand All @@ -202,11 +206,8 @@ export class MatListOption extends _MatListOptionMixinBase
preserveWhitespaces: false,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class MatSelectionList extends _MatSelectionListMixinBase
implements FocusableOption, CanDisable, CanDisableRipple, AfterContentInit {

/** Tab index for the selection-list. */
_tabIndex = 0;
export class MatSelectionList extends _MatSelectionListMixinBase implements FocusableOption,
CanDisable, CanDisableRipple, HasTabIndex, AfterContentInit {

/** The FocusKeyManager which handles focus. */
_keyManager: FocusKeyManager<MatListOption>;
Expand All @@ -217,16 +218,14 @@ export class MatSelectionList extends _MatSelectionListMixinBase
/** The currently selected options. */
selectedOptions: SelectionModel<MatListOption> = new SelectionModel<MatListOption>(true);

constructor(private _element: ElementRef) {
constructor(private _element: ElementRef, @Attribute('tabindex') tabIndex: string) {
super();

this.tabIndex = parseInt(tabIndex) || 0;
}

ngAfterContentInit(): void {
this._keyManager = new FocusKeyManager<MatListOption>(this.options).withWrap();

if (this.disabled) {
this._tabIndex = -1;
}
}

/** Focus the selection-list. */
Expand Down