Skip to content

Commit 25d0127

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 d78a370 commit 25d0127

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
@@ -1035,10 +1035,17 @@ describe('MdSelect', () => {
10351035
expect(select.getAttribute('aria-label')).toEqual('Food');
10361036
});
10371037

1038-
it('should set the tabindex of the select to 0', () => {
1038+
it('should set the tabindex of the select to 0 by default', () => {
10391039
expect(select.getAttribute('tabindex')).toEqual('0');
10401040
});
10411041

1042+
it('should be able to override the tabindex', () => {
1043+
fixture.componentInstance.tabIndexOverride = 3;
1044+
fixture.detectChanges();
1045+
1046+
expect(select.getAttribute('tabindex')).toBe('3');
1047+
});
1048+
10421049
it('should set aria-required for required selects', () => {
10431050
expect(select.getAttribute('aria-required'))
10441051
.toEqual('false', `Expected aria-required attr to be false for normal selects.`);
@@ -1362,7 +1369,8 @@ describe('MdSelect', () => {
13621369
selector: 'basic-select',
13631370
template: `
13641371
<div [style.height.px]="heightAbove"></div>
1365-
<md-select placeholder="Food" [formControl]="control" [required]="isRequired">
1372+
<md-select placeholder="Food" [formControl]="control" [required]="isRequired"
1373+
[tabIndex]="tabIndexOverride">
13661374
<md-option *ngFor="let food of foods" [value]="food.value" [disabled]="food.disabled">
13671375
{{ food.viewValue }}
13681376
</md-option>
@@ -1385,6 +1393,7 @@ class BasicSelect {
13851393
isRequired: boolean;
13861394
heightAbove = 0;
13871395
heightBelow = 0;
1396+
tabIndexOverride: number;
13881397

13891398
@ViewChild(MdSelect) select: MdSelect;
13901399
@ViewChildren(MdOption) options: QueryList<MdOption>;

src/lib/select/select.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export type MdSelectFloatPlaceholderType = 'always' | 'never' | 'auto';
8484
encapsulation: ViewEncapsulation.None,
8585
host: {
8686
'role': 'listbox',
87-
'[attr.tabindex]': '_getTabIndex()',
87+
'[attr.tabindex]': 'tabIndex',
8888
'[attr.aria-label]': 'placeholder',
8989
'[attr.aria-required]': 'required.toString()',
9090
'[attr.aria-disabled]': 'disabled.toString()',
@@ -133,6 +133,9 @@ export class MdSelect implements AfterContentInit, ControlValueAccessor, OnDestr
133133
/** The animation state of the placeholder. */
134134
private _placeholderState = '';
135135

136+
/** Tab index for the element. */
137+
private _tabIndex: number = 0;
138+
136139
/**
137140
* The width of the trigger. Must be saved to set the min width of the overlay panel
138141
* and the width of the selected value.
@@ -237,6 +240,15 @@ export class MdSelect implements AfterContentInit, ControlValueAccessor, OnDestr
237240
}
238241
private _floatPlaceholder: MdSelectFloatPlaceholderType = 'auto';
239242

243+
/** Tab index for the select element. */
244+
@Input()
245+
get tabIndex(): number { return this._disabled ? -1 : this._tabIndex; }
246+
set tabIndex(value: number) {
247+
if (typeof value !== 'undefined') {
248+
this._tabIndex = value;
249+
}
250+
}
251+
240252
/** Event emitted when the select has been opened. */
241253
@Output() onOpen: EventEmitter<void> = new EventEmitter<void>();
242254

@@ -407,12 +419,6 @@ export class MdSelect implements AfterContentInit, ControlValueAccessor, OnDestr
407419
}
408420
}
409421

410-
/** Returns the correct tabindex for the select depending on disabled state. */
411-
_getTabIndex() {
412-
return this.disabled ? '-1' : '0';
413-
}
414-
415-
416422
/**
417423
* Sets the scroll position of the scroll container. This must be called after
418424
* the overlay pane is attached or the scroll container element will not yet be

0 commit comments

Comments
 (0)