Skip to content

Commit 4fda4de

Browse files
committed
fix(button-toggle): able to focus disabled button via click
Along the same lines as #15499. Fixes users being able to focus a disabled button toggle by clicking on it. The issue comes from us preserving the -1 tabindex, even if the button is disabled.
1 parent 3b0f7fc commit 4fda4de

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/lib/button-toggle/button-toggle.spec.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ describe('MatButtonToggle without forms', () => {
746746
expect(button.getAttribute('tabindex')).toBe('3');
747747
});
748748

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

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

758+
it('should clear the tabindex from the host node when disabled', () => {
759+
const fixture = TestBed.createComponent(ButtonToggleWithTabindex);
760+
fixture.detectChanges();
761+
762+
const host = fixture.nativeElement.querySelector('.mat-button-toggle');
763+
764+
expect(host.getAttribute('tabindex')).toBeTruthy();
765+
766+
fixture.componentInstance.disabled = true;
767+
fixture.detectChanges();
768+
769+
expect(host.hasAttribute('tabindex')).toBe(false);
770+
});
771+
758772
it('should forward focus to the underlying button when the host is focused', () => {
759773
const fixture = TestBed.createComponent(ButtonToggleWithTabindex);
760774
fixture.detectChanges();
@@ -947,7 +961,9 @@ class RepeatedButtonTogglesWithPreselectedValue {
947961

948962

949963
@Component({
950-
template: `<mat-button-toggle tabindex="3"></mat-button-toggle>`
964+
template: `<mat-button-toggle tabindex="3" [disabled]="disabled"></mat-button-toggle>`
951965
})
952-
class ButtonToggleWithTabindex {}
966+
class ButtonToggleWithTabindex {
967+
disabled = false;
968+
}
953969

src/lib/button-toggle/button-toggle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ export const _MatButtonToggleMixinBase: CanDisableRippleCtor & typeof MatButtonT
370370
'class': 'mat-button-toggle',
371371
// Always reset the tabindex to -1 so it doesn't conflict with the one on the `button`,
372372
// but can still receive focus from things like cdkFocusInitial.
373-
'[attr.tabindex]': '-1',
373+
'[attr.tabindex]': 'disabled ? null : -1',
374374
'[attr.id]': 'id',
375375
'(focus)': 'focus()',
376376
}

0 commit comments

Comments
 (0)