Skip to content

Commit cb6c621

Browse files
crisbetojelbourn
authored andcommitted
fix(button): unable to set a custom tabindex on a link button (#12042)
Fixes the consumer's `tabIndex` being overwritten to always be 0 on button links. Fixes #12041.
1 parent c134026 commit cb6c621

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/lib/button/button.spec.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,24 @@ describe('MatButton', () => {
182182
expect(buttonDebugElement.nativeElement.getAttribute('disabled'))
183183
.toBeNull('Expect no disabled');
184184
});
185+
186+
it('should be able to set a custom tabindex', () => {
187+
let fixture = TestBed.createComponent(TestApp);
188+
let testComponent = fixture.debugElement.componentInstance;
189+
let buttonElement = fixture.debugElement.query(By.css('a')).nativeElement;
190+
191+
fixture.componentInstance.tabIndex = 3;
192+
fixture.detectChanges();
193+
194+
expect(buttonElement.getAttribute('tabIndex'))
195+
.toBe('3', 'Expected custom tabindex to be set');
196+
197+
testComponent.isDisabled = true;
198+
fixture.detectChanges();
199+
200+
expect(buttonElement.getAttribute('tabIndex'))
201+
.toBe('-1', 'Expected custom tabindex to be overwritten when disabled.');
202+
});
185203
});
186204

187205
// Ripple tests.
@@ -244,11 +262,12 @@ describe('MatButton', () => {
244262
@Component({
245263
selector: 'test-app',
246264
template: `
247-
<button mat-button type="button" (click)="increment()"
265+
<button [tabIndex]="tabIndex" mat-button type="button" (click)="increment()"
248266
[disabled]="isDisabled" [color]="buttonColor" [disableRipple]="rippleDisabled">
249267
Go
250268
</button>
251-
<a href="http://www.google.com" mat-button [disabled]="isDisabled" [color]="buttonColor">
269+
<a [tabIndex]="tabIndex" href="http://www.google.com" mat-button [disabled]="isDisabled"
270+
[color]="buttonColor">
252271
Link
253272
</a>
254273
<button mat-fab>Fab Button</button>
@@ -260,6 +279,7 @@ class TestApp {
260279
isDisabled: boolean = false;
261280
rippleDisabled: boolean = false;
262281
buttonColor: ThemePalette;
282+
tabIndex: number;
263283

264284
increment() {
265285
this.clickCount++;

src/lib/button/button.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
ViewEncapsulation,
1818
Optional,
1919
Inject,
20+
Input,
2021
} from '@angular/core';
2122
import {
2223
CanColor,
@@ -149,7 +150,10 @@ export class MatButton extends _MatButtonMixinBase
149150
a[mat-mini-fab], a[mat-stroked-button], a[mat-flat-button]`,
150151
exportAs: 'matButton, matAnchor',
151152
host: {
152-
'[attr.tabindex]': 'disabled ? -1 : 0',
153+
// Note that we ignore the user-specified tabindex when it's disabled for
154+
// consistency with the `mat-button` applied on native buttons where even
155+
// though they have an index, they're not tabbable.
156+
'[attr.tabindex]': 'disabled ? -1 : (tabIndex || 0)',
153157
'[attr.disabled]': 'disabled || null',
154158
'[attr.aria-disabled]': 'disabled.toString()',
155159
'(click)': '_haltDisabledEvents($event)',
@@ -162,6 +166,8 @@ export class MatButton extends _MatButtonMixinBase
162166
changeDetection: ChangeDetectionStrategy.OnPush,
163167
})
164168
export class MatAnchor extends MatButton {
169+
/** Tabindex of the button. */
170+
@Input() tabIndex: number;
165171

166172
constructor(
167173
platform: Platform,

0 commit comments

Comments
 (0)