Skip to content

fix(material/radio): group selected button not set for preselected control value #21154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions src/material-experimental/mdc-radio/radio.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,32 +537,29 @@ describe('MDC-based MatRadio', () => {
});

describe('group with FormControl', () => {
let fixture: ComponentFixture<RadioGroupWithFormControl>;
let groupDebugElement: DebugElement;
let groupInstance: MatRadioGroup;
let testComponent: RadioGroupWithFormControl;

beforeEach(() => {
fixture = TestBed.createComponent(RadioGroupWithFormControl);
it('should toggle the disabled state', () => {
const fixture = TestBed.createComponent(RadioGroupWithFormControl);
fixture.detectChanges();

testComponent = fixture.debugElement.componentInstance;
groupDebugElement = fixture.debugElement.query(By.directive(MatRadioGroup))!;
groupInstance = groupDebugElement.injector.get<MatRadioGroup>(MatRadioGroup);
});
expect(fixture.componentInstance.group.disabled).toBeFalsy();

it('should toggle the disabled state', () => {
expect(groupInstance.disabled).toBeFalsy();
fixture.componentInstance.formControl.disable();
fixture.detectChanges();

testComponent.formControl.disable();
expect(fixture.componentInstance.group.disabled).toBeTruthy();

fixture.componentInstance.formControl.enable();
fixture.detectChanges();

expect(groupInstance.disabled).toBeTruthy();
expect(fixture.componentInstance.group.disabled).toBeFalsy();
});

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

expect(groupInstance.disabled).toBeFalsy();
expect(fixture.componentInstance.group.selected?.value).toBe('2');
});
});

Expand Down Expand Up @@ -999,12 +996,14 @@ class DisableableRadioButton {

@Component({
template: `
<mat-radio-group [formControl]="formControl">
<mat-radio-button value="1">One</mat-radio-button>
</mat-radio-group>
<mat-radio-group [formControl]="formControl">
<mat-radio-button value="1">One</mat-radio-button>
<mat-radio-button value="2">Two</mat-radio-button>
</mat-radio-group>
`
})
class RadioGroupWithFormControl {
@ViewChild(MatRadioGroup) group: MatRadioGroup;
formControl = new FormControl();
}

Expand Down
39 changes: 19 additions & 20 deletions src/material/radio/radio.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,32 +531,29 @@ describe('MatRadio', () => {
});

describe('group with FormControl', () => {
let fixture: ComponentFixture<RadioGroupWithFormControl>;
let groupDebugElement: DebugElement;
let groupInstance: MatRadioGroup;
let testComponent: RadioGroupWithFormControl;

beforeEach(() => {
fixture = TestBed.createComponent(RadioGroupWithFormControl);
it('should toggle the disabled state', () => {
const fixture = TestBed.createComponent(RadioGroupWithFormControl);
fixture.detectChanges();

testComponent = fixture.debugElement.componentInstance;
groupDebugElement = fixture.debugElement.query(By.directive(MatRadioGroup))!;
groupInstance = groupDebugElement.injector.get<MatRadioGroup>(MatRadioGroup);
});
expect(fixture.componentInstance.group.disabled).toBeFalsy();

it('should toggle the disabled state', () => {
expect(groupInstance.disabled).toBeFalsy();
fixture.componentInstance.formControl.disable();
fixture.detectChanges();

testComponent.formControl.disable();
expect(fixture.componentInstance.group.disabled).toBeTruthy();

fixture.componentInstance.formControl.enable();
fixture.detectChanges();

expect(groupInstance.disabled).toBeTruthy();
expect(fixture.componentInstance.group.disabled).toBeFalsy();
});

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

expect(groupInstance.disabled).toBeFalsy();
expect(fixture.componentInstance.group.selected?.value).toBe('2');
});
});

Expand Down Expand Up @@ -974,12 +971,14 @@ class DisableableRadioButton {

@Component({
template: `
<mat-radio-group [formControl]="formControl">
<mat-radio-button value="1">One</mat-radio-button>
</mat-radio-group>
<mat-radio-group [formControl]="formControl">
<mat-radio-button value="1">One</mat-radio-button>
<mat-radio-button value="2">Two</mat-radio-button>
</mat-radio-group>
`
})
class RadioGroupWithFormControl {
@ViewChild(MatRadioGroup) group: MatRadioGroup;
formControl = new FormControl();
}

Expand Down
5 changes: 5 additions & 0 deletions src/material/radio/radio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,11 @@ export abstract class _MatRadioButtonBase extends _MatRadioButtonMixinBase imple
if (this.radioGroup) {
// If the radio is inside a radio group, determine if it should be checked
this.checked = this.radioGroup.value === this._value;

if (this.checked) {
this.radioGroup.selected = this;
}

// Copy name from parent radio group
this.name = this.radioGroup.name;
}
Expand Down