Skip to content

Commit bb64336

Browse files
committed
fix(radio): able to focus disabled radio button via click
Fixes users being able to focus a disabled radio button by clicking on it. The issue comes from us preserving the -1 tabindex, even if the button is disabled. Fixes #15493.
1 parent 9091330 commit bb64336

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/material/radio/radio.spec.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ describe('MatRadio', () => {
769769
.toBe(4, 'Expected the tabindex to be set to "4".');
770770
});
771771

772-
it('should remove the tabindex from the host element', () => {
772+
it('should set the tabindex to -1 on the host element', () => {
773773
const predefinedFixture = TestBed.createComponent(RadioButtonWithPredefinedTabindex);
774774
predefinedFixture.detectChanges();
775775

@@ -791,6 +791,15 @@ describe('MatRadio', () => {
791791
expect(radioButtonEl.hasAttribute('aria-labelledby')).toBe(false);
792792
});
793793

794+
it('should remove the tabindex from the host element when disabled', () => {
795+
const radioButtonEl = fixture.debugElement.query(By.css('.mat-radio-button')).nativeElement;
796+
797+
fixture.componentInstance.disabled = true;
798+
fixture.detectChanges();
799+
800+
expect(radioButtonEl.hasAttribute('tabindex')).toBe(false);
801+
});
802+
794803
});
795804

796805
describe('group interspersed with other tags', () => {
@@ -956,11 +965,11 @@ class RadioGroupWithFormControl {
956965
formControl = new FormControl();
957966
}
958967

959-
@Component({
960-
template: `<mat-radio-button [tabIndex]="tabIndex"></mat-radio-button>`
961-
})
968+
@Component(
969+
{template: `<mat-radio-button [disabled]="disabled" [tabIndex]="tabIndex"></mat-radio-button>`})
962970
class FocusableRadioButton {
963971
tabIndex: number;
972+
disabled = false;
964973
}
965974

966975
@Component({

src/material/radio/radio.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ const _MatRadioButtonMixinBase:
346346
'[class.mat-accent]': 'color === "accent"',
347347
'[class.mat-warn]': 'color === "warn"',
348348
// Needs to be -1 so the `focus` event still fires.
349-
'[attr.tabindex]': '-1',
349+
'[attr.tabindex]': 'disabled ? null : -1',
350350
'[attr.id]': 'id',
351351
'[attr.aria-label]': 'null',
352352
'[attr.aria-labelledby]': 'null',

0 commit comments

Comments
 (0)