Skip to content

Commit 934be6f

Browse files
committed
fix(select): unable to set a tabindex
Fixes users not being able to override the `tabIndex` on `md-select`. Fixes #3474.
1 parent f27617a commit 934be6f

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/lib/select/select.spec.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,10 +1081,17 @@ describe('MdSelect', () => {
10811081
expect(select.getAttribute('aria-label')).toEqual('Food');
10821082
});
10831083

1084-
it('should set the tabindex of the select to 0', () => {
1084+
it('should set the tabindex of the select to 0 by default', () => {
10851085
expect(select.getAttribute('tabindex')).toEqual('0');
10861086
});
10871087

1088+
it('should be able to override the tabindex', () => {
1089+
fixture.componentInstance.tabIndexOverride = 3;
1090+
fixture.detectChanges();
1091+
1092+
expect(select.getAttribute('tabindex')).toBe('3');
1093+
});
1094+
10881095
it('should set aria-required for required selects', () => {
10891096
expect(select.getAttribute('aria-required'))
10901097
.toEqual('false', `Expected aria-required attr to be false for normal selects.`);
@@ -1583,7 +1590,8 @@ describe('MdSelect', () => {
15831590
selector: 'basic-select',
15841591
template: `
15851592
<div [style.height.px]="heightAbove"></div>
1586-
<md-select placeholder="Food" [formControl]="control" [required]="isRequired">
1593+
<md-select placeholder="Food" [formControl]="control" [required]="isRequired"
1594+
[tabIndex]="tabIndexOverride">
15871595
<md-option *ngFor="let food of foods" [value]="food.value" [disabled]="food.disabled">
15881596
{{ food.viewValue }}
15891597
</md-option>
@@ -1606,6 +1614,7 @@ class BasicSelect {
16061614
isRequired: boolean;
16071615
heightAbove = 0;
16081616
heightBelow = 0;
1617+
tabIndexOverride: number;
16091618

16101619
@ViewChild(MdSelect) select: MdSelect;
16111620
@ViewChildren(MdOption) options: QueryList<MdOption>;

src/lib/select/select.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export type MdSelectFloatPlaceholderType = 'always' | 'never' | 'auto';
9999
encapsulation: ViewEncapsulation.None,
100100
host: {
101101
'role': 'listbox',
102-
'[attr.tabindex]': '_getTabIndex()',
102+
'[attr.tabindex]': 'tabIndex',
103103
'[attr.aria-label]': 'placeholder',
104104
'[attr.aria-required]': 'required.toString()',
105105
'[attr.aria-disabled]': 'disabled.toString()',
@@ -151,6 +151,9 @@ export class MdSelect implements AfterContentInit, ControlValueAccessor, OnDestr
151151
/** The animation state of the placeholder. */
152152
private _placeholderState = '';
153153

154+
/** Tab index for the element. */
155+
private _tabIndex: number = 0;
156+
154157
/**
155158
* The width of the trigger. Must be saved to set the min width of the overlay panel
156159
* and the width of the selected value.
@@ -266,6 +269,15 @@ export class MdSelect implements AfterContentInit, ControlValueAccessor, OnDestr
266269
}
267270
private _floatPlaceholder: MdSelectFloatPlaceholderType = 'auto';
268271

272+
/** Tab index for the select element. */
273+
@Input()
274+
get tabIndex(): number { return this._disabled ? -1 : this._tabIndex; }
275+
set tabIndex(value: number) {
276+
if (typeof value !== 'undefined') {
277+
this._tabIndex = value;
278+
}
279+
}
280+
269281
/** Combined stream of all of the child options' change events. */
270282
get optionSelectionChanges(): Observable<MdOptionSelectionChange> {
271283
return Observable.merge(...this.options.map(option => option.onSelectionChange));
@@ -452,12 +464,6 @@ export class MdSelect implements AfterContentInit, ControlValueAccessor, OnDestr
452464
}
453465
}
454466

455-
/** Returns the correct tabindex for the select depending on disabled state. */
456-
_getTabIndex() {
457-
return this.disabled ? '-1' : '0';
458-
}
459-
460-
461467
/**
462468
* Sets the scroll position of the scroll container. This must be called after
463469
* the overlay pane is attached or the scroll container element will not yet be

0 commit comments

Comments
 (0)