Skip to content

Commit 0792d7d

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 d29df38 commit 0792d7d

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/material/radio/radio.spec.ts

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

766-
it('should remove the tabindex from the host element', () => {
766+
it('should set the tabindex to -1 on the host element', () => {
767767
const predefinedFixture = TestBed.createComponent(RadioButtonWithPredefinedTabindex);
768768
predefinedFixture.detectChanges();
769769

@@ -773,6 +773,15 @@ describe('MatRadio', () => {
773773
expect(radioButtonEl.getAttribute('tabindex')).toBe('-1');
774774
});
775775

776+
it('should remove the tabindex from the host element when disabled', () => {
777+
const radioButtonEl = fixture.debugElement.query(By.css('.mat-radio-button')).nativeElement;
778+
779+
fixture.componentInstance.disabled = true;
780+
fixture.detectChanges();
781+
782+
expect(radioButtonEl.hasAttribute('tabindex')).toBe(false);
783+
});
784+
776785
});
777786

778787
describe('group interspersed with other tags', () => {
@@ -938,11 +947,11 @@ class RadioGroupWithFormControl {
938947
formControl = new FormControl();
939948
}
940949

941-
@Component({
942-
template: `<mat-radio-button [tabIndex]="tabIndex"></mat-radio-button>`
943-
})
950+
@Component(
951+
{template: `<mat-radio-button [disabled]="disabled" [tabIndex]="tabIndex"></mat-radio-button>`})
944952
class FocusableRadioButton {
945953
tabIndex: number;
954+
disabled = false;
946955
}
947956

948957
@Component({

src/material/radio/radio.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ const _MatRadioButtonMixinBase:
344344
'[class.mat-accent]': 'color === "accent"',
345345
'[class.mat-warn]': 'color === "warn"',
346346
// Needs to be -1 so the `focus` event still fires.
347-
'[attr.tabindex]': '-1',
347+
'[attr.tabindex]': 'disabled ? null : -1',
348348
'[attr.id]': 'id',
349349
// Note: under normal conditions focus shouldn't land on this element, however it may be
350350
// programmatically set, for example inside of a focus trap, in this case we want to forward
@@ -591,5 +591,4 @@ export class MatRadioButton extends _MatRadioButtonMixinBase
591591
}
592592
}
593593
}
594-
595594
}

0 commit comments

Comments
 (0)