Skip to content

Commit df06dfc

Browse files
committed
fix(material/radio): clear selected radio button from group
In #18081 the radio group was changed so that deselected buttons receive `tabindex="-1"` when there's a selected button. The problem is that we weren't clearing the reference to the selected button so it gets removed, the deselected buttons become unfocusable using the keyboard. These changes clear the selected radio button on destroy. Fixes #27461.
1 parent e3e0c6b commit df06dfc

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/material/radio/radio.spec.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,17 @@ describe('MDC-based MatRadio', () => {
471471
}),
472472
).toEqual(['-1', '-1', '0']);
473473
});
474+
475+
it('should clear the selected radio button on destroy', () => {
476+
radioLabelElements[0].click();
477+
fixture.detectChanges();
478+
expect(groupInstance.selected).toBe(radioInstances[0]);
479+
480+
fixture.componentInstance.isFirstShown = false;
481+
fixture.detectChanges();
482+
483+
expect(groupInstance.selected).toBe(null);
484+
});
474485
});
475486

476487
describe('group with ngModel', () => {
@@ -995,7 +1006,7 @@ describe('MatRadioDefaultOverrides', () => {
9951006
[value]="groupValue"
9961007
name="test-name">
9971008
<mat-radio-button value="fire" [disableRipple]="disableRipple" [disabled]="isFirstDisabled"
998-
[color]="color">
1009+
[color]="color" *ngIf="isFirstShown">
9991010
Charmander
10001011
</mat-radio-button>
10011012
<mat-radio-button value="water" [disableRipple]="disableRipple" [color]="color">
@@ -1009,12 +1020,13 @@ describe('MatRadioDefaultOverrides', () => {
10091020
})
10101021
class RadiosInsideRadioGroup {
10111022
labelPos: 'before' | 'after';
1012-
isFirstDisabled: boolean = false;
1013-
isGroupDisabled: boolean = false;
1014-
isGroupRequired: boolean = false;
1023+
isFirstDisabled = false;
1024+
isGroupDisabled = false;
1025+
isGroupRequired = false;
10151026
groupValue: string | null = null;
1016-
disableRipple: boolean = false;
1027+
disableRipple = false;
10171028
color: string | null;
1029+
isFirstShown = true;
10181030
}
10191031

10201032
@Component({

src/material/radio/radio.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,12 @@ export abstract class _MatRadioButtonBase
568568
ngOnDestroy() {
569569
this._focusMonitor.stopMonitoring(this._elementRef);
570570
this._removeUniqueSelectionListener();
571+
572+
// Clear the button from the `selected` state since it
573+
// determines the `tabindex` of the remaining radio buttons.
574+
if (this.radioGroup?.selected === this) {
575+
this.radioGroup.selected = null;
576+
}
571577
}
572578

573579
/** Dispatch change event with current value. */

0 commit comments

Comments
 (0)