Skip to content

fix(button-toggle): able to focus disabled button via click #15521

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

Closed
Closed
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
22 changes: 19 additions & 3 deletions src/material/button-toggle/button-toggle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ describe('MatButtonToggle without forms', () => {
expect(button.getAttribute('tabindex')).toBe('3');
});

it('should clear the tabindex from the host element', () => {
it('should set the tabindex on the host element to -1', () => {
const fixture = TestBed.createComponent(ButtonToggleWithTabindex);
fixture.detectChanges();

Expand All @@ -755,6 +755,20 @@ describe('MatButtonToggle without forms', () => {
expect(host.getAttribute('tabindex')).toBe('-1');
});

it('should clear the tabindex from the host node when disabled', () => {
const fixture = TestBed.createComponent(ButtonToggleWithTabindex);
fixture.detectChanges();

const host = fixture.nativeElement.querySelector('.mat-button-toggle');

expect(host.getAttribute('tabindex')).toBeTruthy();

fixture.componentInstance.disabled = true;
fixture.detectChanges();

expect(host.hasAttribute('tabindex')).toBe(false);
});

it('should forward focus to the underlying button when the host is focused', () => {
const fixture = TestBed.createComponent(ButtonToggleWithTabindex);
fixture.detectChanges();
Expand Down Expand Up @@ -947,7 +961,9 @@ class RepeatedButtonTogglesWithPreselectedValue {


@Component({
template: `<mat-button-toggle tabindex="3"></mat-button-toggle>`
template: `<mat-button-toggle tabindex="3" [disabled]="disabled"></mat-button-toggle>`
})
class ButtonToggleWithTabindex {}
class ButtonToggleWithTabindex {
disabled = false;
}

2 changes: 1 addition & 1 deletion src/material/button-toggle/button-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ const _MatButtonToggleMixinBase: CanDisableRippleCtor & typeof MatButtonToggleBa
'class': 'mat-button-toggle',
// Always reset the tabindex to -1 so it doesn't conflict with the one on the `button`,
// but can still receive focus from things like cdkFocusInitial.
'[attr.tabindex]': '-1',
'[attr.tabindex]': 'disabled ? null : -1',
'[attr.id]': 'id',
'(focus)': 'focus()',
}
Expand Down