Skip to content

Commit 8a68ed4

Browse files
committed
fix(selection-list): tabIndex should respect disabled state
* Currently if the selection-list is disabled, the tabIndex may be still set to a valid value that allows tabbing to the element. The `mixinTabIndex` respects the disabled state of the selection list.
1 parent 3571f68 commit 8a68ed4

File tree

2 files changed

+68
-13
lines changed

2 files changed

+68
-13
lines changed

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,49 @@ describe('MdSelectionList', () => {
214214
});
215215
});
216216

217+
describe('with tabindex', () => {
218+
219+
beforeEach(async(() => {
220+
TestBed.configureTestingModule({
221+
imports: [MdListModule],
222+
declarations: [
223+
SelectionListWithTabindexAttr,
224+
SelectionListWithTabindexBinding,
225+
]
226+
});
227+
228+
TestBed.compileComponents();
229+
}));
230+
231+
it('should properly handle native tabindex attribute', () => {
232+
const fixture = TestBed.createComponent(SelectionListWithTabindexAttr);
233+
const selectionList = fixture.debugElement.query(By.directive(MdSelectionList));
234+
235+
expect(selectionList.componentInstance.tabIndex)
236+
.toBe(5, 'Expected the selection-list tabindex to be set to the attribute value.');
237+
});
238+
239+
it('should support changing the tabIndex through binding', () => {
240+
const fixture = TestBed.createComponent(SelectionListWithTabindexBinding);
241+
const selectionList = fixture.debugElement.query(By.directive(MdSelectionList));
242+
243+
expect(selectionList.componentInstance.tabIndex)
244+
.toBe(0, 'Expected the tabIndex to be set to "0" by default.');
245+
246+
fixture.componentInstance.tabIndex = 3;
247+
fixture.detectChanges();
248+
249+
expect(selectionList.componentInstance.tabIndex)
250+
.toBe(3, 'Expected the tabIndex to updated through binding.');
251+
252+
fixture.componentInstance.disabled = true;
253+
fixture.detectChanges();
254+
255+
expect(selectionList.componentInstance.tabIndex)
256+
.toBe(-1, 'Expected the tabIndex to be set to "-1" if selection list is disabled.');
257+
});
258+
});
259+
217260
describe('with single option', () => {
218261
let fixture: ComponentFixture<SelectionListWithOnlyOneOption>;
219262
let listOption: DebugElement;
@@ -458,3 +501,16 @@ class SelectionListWithDisabledOption {
458501
</mat-selection-list>`})
459502
class SelectionListWithOnlyOneOption {
460503
}
504+
505+
@Component({
506+
template: `<mat-selection-list tabindex="5"></mat-selection-list>`
507+
})
508+
class SelectionListWithTabindexAttr {}
509+
510+
@Component({
511+
template: `<mat-selection-list [tabIndex]="tabIndex" [disabled]="disabled"></mat-selection-list>`
512+
})
513+
class SelectionListWithTabindexBinding {
514+
tabIndex: number;
515+
disabled: boolean;
516+
}

src/lib/list/selection-list.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,27 @@ import {
2828
QueryList,
2929
Renderer2,
3030
ViewEncapsulation,
31+
Attribute,
3132
} from '@angular/core';
3233
import {
3334
CanDisable,
3435
CanDisableRipple,
36+
HasTabIndex,
3537
MATERIAL_COMPATIBILITY_MODE,
3638
MdLine,
3739
MdLineSetter,
3840
mixinDisabled,
3941
mixinDisableRipple,
42+
mixinTabIndex,
4043
} from '@angular/material/core';
4144
import {merge} from 'rxjs/observable/merge';
4245
import {Subscription} from 'rxjs/Subscription';
4346

4447

4548
/** @docs-private */
4649
export class MdSelectionListBase {}
47-
export const _MdSelectionListMixinBase = mixinDisableRipple(mixinDisabled(MdSelectionListBase));
50+
export const _MdSelectionListMixinBase =
51+
mixinTabIndex(mixinDisableRipple(mixinDisabled(MdSelectionListBase)));
4852

4953
/** @docs-private */
5054
export class MdListOptionBase {}
@@ -189,10 +193,10 @@ export class MdListOption extends _MdListOptionMixinBase
189193
@Component({
190194
moduleId: module.id,
191195
selector: 'md-selection-list, mat-selection-list',
192-
inputs: ['disabled', 'disableRipple'],
196+
inputs: ['disabled', 'disableRipple', 'tabIndex'],
193197
host: {
194198
'role': 'listbox',
195-
'[attr.tabindex]': '_tabIndex',
199+
'[tabIndex]': 'tabIndex',
196200
'class': 'mat-selection-list',
197201
'(focus)': 'focus()',
198202
'(keydown)': '_keydown($event)',
@@ -203,11 +207,8 @@ export class MdListOption extends _MdListOptionMixinBase
203207
preserveWhitespaces: false,
204208
changeDetection: ChangeDetectionStrategy.OnPush
205209
})
206-
export class MdSelectionList extends _MdSelectionListMixinBase
207-
implements FocusableOption, CanDisable, CanDisableRipple, AfterContentInit, OnDestroy {
208-
209-
/** Tab index for the selection-list. */
210-
_tabIndex = 0;
210+
export class MdSelectionList extends _MdSelectionListMixinBase implements FocusableOption,
211+
CanDisable, CanDisableRipple, HasTabIndex, AfterContentInit, OnDestroy {
211212

212213
/** Subscription to all list options' onFocus events */
213214
private _optionFocusSubscription = Subscription.EMPTY;
@@ -224,17 +225,15 @@ export class MdSelectionList extends _MdSelectionListMixinBase
224225
/** The currently selected options. */
225226
selectedOptions: SelectionModel<MdListOption> = new SelectionModel<MdListOption>(true);
226227

227-
constructor(private _element: ElementRef) {
228+
constructor(private _element: ElementRef, @Attribute('tabindex') tabIndex: string) {
228229
super();
230+
231+
this.tabIndex = parseInt(tabIndex) || 0;
229232
}
230233

231234
ngAfterContentInit(): void {
232235
this._keyManager = new FocusKeyManager<MdListOption>(this.options).withWrap();
233236

234-
if (this.disabled) {
235-
this._tabIndex = -1;
236-
}
237-
238237
this._optionFocusSubscription = this._onFocusSubscription();
239238
this._optionDestroyStream = this._onDestroySubscription();
240239
}

0 commit comments

Comments
 (0)