Skip to content

Commit 2249e16

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 876727d commit 2249e16

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/lib/radio/radio.spec.ts

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

741-
it('should remove the tabindex from the host element', () => {
741+
it('should set the tabindex to -1 on the host element', () => {
742742
const predefinedFixture = TestBed.createComponent(RadioButtonWithPredefinedTabindex);
743743
predefinedFixture.detectChanges();
744744

@@ -748,6 +748,15 @@ describe('MatRadio', () => {
748748
expect(radioButtonEl.getAttribute('tabindex')).toBe('-1');
749749
});
750750

751+
it('should remove the tabindex from the host element when disabled', () => {
752+
const radioButtonEl = fixture.debugElement.query(By.css('.mat-radio-button')).nativeElement;
753+
754+
fixture.componentInstance.disabled = true;
755+
fixture.detectChanges();
756+
757+
expect(radioButtonEl.hasAttribute('tabindex')).toBe(false);
758+
});
759+
751760
});
752761

753762
describe('group interspersed with other tags', () => {
@@ -876,11 +885,11 @@ class RadioGroupWithFormControl {
876885
formControl = new FormControl();
877886
}
878887

879-
@Component({
880-
template: `<mat-radio-button [tabIndex]="tabIndex"></mat-radio-button>`
881-
})
888+
@Component(
889+
{template: `<mat-radio-button [disabled]="disabled" [tabIndex]="tabIndex"></mat-radio-button>`})
882890
class FocusableRadioButton {
883891
tabIndex: number;
892+
disabled = false;
884893
}
885894

886895
@Component({

src/lib/radio/radio.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ export const _MatRadioButtonMixinBase:
323323
'[class.mat-radio-disabled]': 'disabled',
324324
'[class._mat-animation-noopable]': '_animationMode === "NoopAnimations"',
325325
// Needs to be -1 so the `focus` event still fires.
326-
'[attr.tabindex]': '-1',
326+
'[attr.tabindex]': 'disabled ? null : -1',
327327
'[attr.id]': 'id',
328328
// Note: under normal conditions focus shouldn't land on this element, however it may be
329329
// programmatically set, for example inside of a focus trap, in this case we want to forward
@@ -332,9 +332,10 @@ export const _MatRadioButtonMixinBase:
332332
},
333333
changeDetection: ChangeDetectionStrategy.OnPush,
334334
})
335-
export class MatRadioButton extends _MatRadioButtonMixinBase
336-
implements OnInit, AfterViewInit, OnDestroy, CanColor, CanDisableRipple, HasTabIndex {
337-
335+
export class MatRadioButton extends _MatRadioButtonMixinBase implements OnInit, AfterViewInit,
336+
OnDestroy, CanColor,
337+
CanDisableRipple,
338+
HasTabIndex {
338339
private _uniqueId: string = `mat-radio-${++nextUniqueId}`;
339340

340341
/** The unique ID for the radio button. */
@@ -558,5 +559,4 @@ export class MatRadioButton extends _MatRadioButtonMixinBase
558559
}
559560
}
560561
}
561-
562562
}

0 commit comments

Comments
 (0)