Skip to content

Commit e199178

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 8436f8c commit e199178

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
@@ -26,15 +26,18 @@ import {
2626
QueryList,
2727
Renderer2,
2828
ViewEncapsulation,
29+
Attribute,
2930
} from '@angular/core';
3031
import {
3132
CanDisable,
3233
CanDisableRipple,
34+
HasTabIndex,
3335
MATERIAL_COMPATIBILITY_MODE,
3436
MdLine,
3537
MdLineSetter,
3638
mixinDisabled,
3739
mixinDisableRipple,
40+
mixinTabIndex,
3841
RxChain,
3942
SPACE,
4043
startWith,
@@ -46,7 +49,8 @@ import {Subscription} from 'rxjs/Subscription';
4649

4750
/** @docs-private */
4851
export class MdSelectionListBase {}
49-
export const _MdSelectionListMixinBase = mixinDisableRipple(mixinDisabled(MdSelectionListBase));
52+
export const _MdSelectionListMixinBase =
53+
mixinTabIndex(mixinDisableRipple(mixinDisabled(MdSelectionListBase)));
5054

5155
/** @docs-private */
5256
export class MdListOptionBase {}
@@ -191,10 +195,10 @@ export class MdListOption extends _MdListOptionMixinBase
191195
@Component({
192196
moduleId: module.id,
193197
selector: 'md-selection-list, mat-selection-list',
194-
inputs: ['disabled', 'disableRipple'],
198+
inputs: ['disabled', 'disableRipple', 'tabIndex'],
195199
host: {
196200
'role': 'listbox',
197-
'[attr.tabindex]': '_tabIndex',
201+
'[tabIndex]': 'tabIndex',
198202
'class': 'mat-selection-list',
199203
'(focus)': 'focus()',
200204
'(keydown)': '_keydown($event)',
@@ -205,11 +209,8 @@ export class MdListOption extends _MdListOptionMixinBase
205209
preserveWhitespaces: false,
206210
changeDetection: ChangeDetectionStrategy.OnPush
207211
})
208-
export class MdSelectionList extends _MdSelectionListMixinBase
209-
implements FocusableOption, CanDisable, CanDisableRipple, AfterContentInit, OnDestroy {
210-
211-
/** Tab index for the selection-list. */
212-
_tabIndex = 0;
212+
export class MdSelectionList extends _MdSelectionListMixinBase implements FocusableOption,
213+
CanDisable, CanDisableRipple, HasTabIndex, AfterContentInit, OnDestroy {
213214

214215
/** Subscription to all list options' onFocus events */
215216
private _optionFocusSubscription = Subscription.EMPTY;
@@ -226,17 +227,15 @@ export class MdSelectionList extends _MdSelectionListMixinBase
226227
/** The currently selected options. */
227228
selectedOptions: SelectionModel<MdListOption> = new SelectionModel<MdListOption>(true);
228229

229-
constructor(private _element: ElementRef) {
230+
constructor(private _element: ElementRef, @Attribute('tabindex') tabIndex: string) {
230231
super();
232+
233+
this.tabIndex = parseInt(tabIndex) || 0;
231234
}
232235

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

236-
if (this.disabled) {
237-
this._tabIndex = -1;
238-
}
239-
240239
this._optionFocusSubscription = this._onFocusSubscription();
241240
this._optionDestroyStream = this._onDestroySubscription();
242241
}

0 commit comments

Comments
 (0)