Skip to content

fix(button): unable to set a custom tabindex on a link button #12042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/lib/button/button.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,24 @@ describe('MatButton', () => {
expect(buttonDebugElement.nativeElement.getAttribute('disabled'))
.toBeNull('Expect no disabled');
});

it('should be able to set a custom tabindex', () => {
let fixture = TestBed.createComponent(TestApp);
let testComponent = fixture.debugElement.componentInstance;
let buttonElement = fixture.debugElement.query(By.css('a')).nativeElement;

fixture.componentInstance.tabIndex = 3;
fixture.detectChanges();

expect(buttonElement.getAttribute('tabIndex'))
.toBe('3', 'Expected custom tabindex to be set');

testComponent.isDisabled = true;
fixture.detectChanges();

expect(buttonElement.getAttribute('tabIndex'))
.toBe('-1', 'Expected custom tabindex to be overwritten when disabled.');
});
});

// Ripple tests.
Expand Down Expand Up @@ -244,11 +262,12 @@ describe('MatButton', () => {
@Component({
selector: 'test-app',
template: `
<button mat-button type="button" (click)="increment()"
<button [tabIndex]="tabIndex" mat-button type="button" (click)="increment()"
[disabled]="isDisabled" [color]="buttonColor" [disableRipple]="rippleDisabled">
Go
</button>
<a href="http://www.google.com" mat-button [disabled]="isDisabled" [color]="buttonColor">
<a [tabIndex]="tabIndex" href="http://www.google.com" mat-button [disabled]="isDisabled"
[color]="buttonColor">
Link
</a>
<button mat-fab>Fab Button</button>
Expand All @@ -260,6 +279,7 @@ class TestApp {
isDisabled: boolean = false;
rippleDisabled: boolean = false;
buttonColor: ThemePalette;
tabIndex: number;

increment() {
this.clickCount++;
Expand Down
8 changes: 7 additions & 1 deletion src/lib/button/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
ViewEncapsulation,
Optional,
Inject,
Input,
} from '@angular/core';
import {
CanColor,
Expand Down Expand Up @@ -149,7 +150,10 @@ export class MatButton extends _MatButtonMixinBase
a[mat-mini-fab], a[mat-stroked-button], a[mat-flat-button]`,
exportAs: 'matButton, matAnchor',
host: {
'[attr.tabindex]': 'disabled ? -1 : 0',
// Note that we ignore the user-specified tabindex when it's disabled for
// consistency with the `mat-button` applied on native buttons where even
// though they have an index, they're not tabbable.
'[attr.tabindex]': 'disabled ? -1 : (tabIndex || 0)',
'[attr.disabled]': 'disabled || null',
'[attr.aria-disabled]': 'disabled.toString()',
'(click)': '_haltDisabledEvents($event)',
Expand All @@ -162,6 +166,8 @@ export class MatButton extends _MatButtonMixinBase
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MatAnchor extends MatButton {
/** Tabindex of the button. */
@Input() tabIndex: number;

constructor(
platform: Platform,
Expand Down