Skip to content

Commit 6cd1dbe

Browse files
authored
fix(material/radio): group selected button not set for preselected control value (#21154)
Fixes that `MatRadioGroup.selected` wasn't set if the value is assigned on init through a `ControlValueAccessor`. Fixes #21148.
1 parent 5e5f87a commit 6cd1dbe

File tree

3 files changed

+43
-40
lines changed

3 files changed

+43
-40
lines changed

src/material-experimental/mdc-radio/radio.spec.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -537,32 +537,29 @@ describe('MDC-based MatRadio', () => {
537537
});
538538

539539
describe('group with FormControl', () => {
540-
let fixture: ComponentFixture<RadioGroupWithFormControl>;
541-
let groupDebugElement: DebugElement;
542-
let groupInstance: MatRadioGroup;
543-
let testComponent: RadioGroupWithFormControl;
544-
545-
beforeEach(() => {
546-
fixture = TestBed.createComponent(RadioGroupWithFormControl);
540+
it('should toggle the disabled state', () => {
541+
const fixture = TestBed.createComponent(RadioGroupWithFormControl);
547542
fixture.detectChanges();
548543

549-
testComponent = fixture.debugElement.componentInstance;
550-
groupDebugElement = fixture.debugElement.query(By.directive(MatRadioGroup))!;
551-
groupInstance = groupDebugElement.injector.get<MatRadioGroup>(MatRadioGroup);
552-
});
544+
expect(fixture.componentInstance.group.disabled).toBeFalsy();
553545

554-
it('should toggle the disabled state', () => {
555-
expect(groupInstance.disabled).toBeFalsy();
546+
fixture.componentInstance.formControl.disable();
547+
fixture.detectChanges();
556548

557-
testComponent.formControl.disable();
549+
expect(fixture.componentInstance.group.disabled).toBeTruthy();
550+
551+
fixture.componentInstance.formControl.enable();
558552
fixture.detectChanges();
559553

560-
expect(groupInstance.disabled).toBeTruthy();
554+
expect(fixture.componentInstance.group.disabled).toBeFalsy();
555+
});
561556

562-
testComponent.formControl.enable();
557+
it('should have a selected button when one matches the initial value', () => {
558+
const fixture = TestBed.createComponent(RadioGroupWithFormControl);
559+
fixture.componentInstance.formControl.setValue('2');
563560
fixture.detectChanges();
564561

565-
expect(groupInstance.disabled).toBeFalsy();
562+
expect(fixture.componentInstance.group.selected?.value).toBe('2');
566563
});
567564
});
568565

@@ -999,12 +996,14 @@ class DisableableRadioButton {
999996

1000997
@Component({
1001998
template: `
1002-
<mat-radio-group [formControl]="formControl">
1003-
<mat-radio-button value="1">One</mat-radio-button>
1004-
</mat-radio-group>
999+
<mat-radio-group [formControl]="formControl">
1000+
<mat-radio-button value="1">One</mat-radio-button>
1001+
<mat-radio-button value="2">Two</mat-radio-button>
1002+
</mat-radio-group>
10051003
`
10061004
})
10071005
class RadioGroupWithFormControl {
1006+
@ViewChild(MatRadioGroup) group: MatRadioGroup;
10081007
formControl = new FormControl();
10091008
}
10101009

src/material/radio/radio.spec.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -531,32 +531,29 @@ describe('MatRadio', () => {
531531
});
532532

533533
describe('group with FormControl', () => {
534-
let fixture: ComponentFixture<RadioGroupWithFormControl>;
535-
let groupDebugElement: DebugElement;
536-
let groupInstance: MatRadioGroup;
537-
let testComponent: RadioGroupWithFormControl;
538-
539-
beforeEach(() => {
540-
fixture = TestBed.createComponent(RadioGroupWithFormControl);
534+
it('should toggle the disabled state', () => {
535+
const fixture = TestBed.createComponent(RadioGroupWithFormControl);
541536
fixture.detectChanges();
542537

543-
testComponent = fixture.debugElement.componentInstance;
544-
groupDebugElement = fixture.debugElement.query(By.directive(MatRadioGroup))!;
545-
groupInstance = groupDebugElement.injector.get<MatRadioGroup>(MatRadioGroup);
546-
});
538+
expect(fixture.componentInstance.group.disabled).toBeFalsy();
547539

548-
it('should toggle the disabled state', () => {
549-
expect(groupInstance.disabled).toBeFalsy();
540+
fixture.componentInstance.formControl.disable();
541+
fixture.detectChanges();
550542

551-
testComponent.formControl.disable();
543+
expect(fixture.componentInstance.group.disabled).toBeTruthy();
544+
545+
fixture.componentInstance.formControl.enable();
552546
fixture.detectChanges();
553547

554-
expect(groupInstance.disabled).toBeTruthy();
548+
expect(fixture.componentInstance.group.disabled).toBeFalsy();
549+
});
555550

556-
testComponent.formControl.enable();
551+
it('should have a selected button when one matches the initial value', () => {
552+
const fixture = TestBed.createComponent(RadioGroupWithFormControl);
553+
fixture.componentInstance.formControl.setValue('2');
557554
fixture.detectChanges();
558555

559-
expect(groupInstance.disabled).toBeFalsy();
556+
expect(fixture.componentInstance.group.selected?.value).toBe('2');
560557
});
561558
});
562559

@@ -974,12 +971,14 @@ class DisableableRadioButton {
974971

975972
@Component({
976973
template: `
977-
<mat-radio-group [formControl]="formControl">
978-
<mat-radio-button value="1">One</mat-radio-button>
979-
</mat-radio-group>
974+
<mat-radio-group [formControl]="formControl">
975+
<mat-radio-button value="1">One</mat-radio-button>
976+
<mat-radio-button value="2">Two</mat-radio-button>
977+
</mat-radio-group>
980978
`
981979
})
982980
class RadioGroupWithFormControl {
981+
@ViewChild(MatRadioGroup) group: MatRadioGroup;
983982
formControl = new FormControl();
984983
}
985984

src/material/radio/radio.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,11 @@ export abstract class _MatRadioButtonBase extends _MatRadioButtonMixinBase imple
539539
if (this.radioGroup) {
540540
// If the radio is inside a radio group, determine if it should be checked
541541
this.checked = this.radioGroup.value === this._value;
542+
543+
if (this.checked) {
544+
this.radioGroup.selected = this;
545+
}
546+
542547
// Copy name from parent radio group
543548
this.name = this.radioGroup.name;
544549
}

0 commit comments

Comments
 (0)