Skip to content

Commit

Permalink
fix(radio): update set disabled directly on MatRadioButton to trigger…
Browse files Browse the repository at this point in the history
… change detection

Fixes MatRadioButton.disabled setter to trigger change detection.  MatRadioButton uses
ChangeDetectionStrategy.OnPush, which works fine with disabled is bound in a template that uses
<mat-radio-button>.  However, if a parent directly accesses the MatRadioButton component instance
(e.g., using ViewChild, as in the provided test case), there is otherwise no way for the parent to
trigger change detection in the MatRadioButton due to the OnPush strategy.

OnPush was added in 97a9bdc, and this same technique was added to some but not all setters.

This commit also adds test coverage for directly setting disabled on MatRadioButton in general,
which previously was only indirectly covered in the MatRadioGroup tests.
  • Loading branch information
stevenyxu committed Apr 28, 2018
1 parent a647579 commit 4a15a68
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
47 changes: 46 additions & 1 deletion src/lib/radio/radio.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {async, ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing';
import {FormControl, FormsModule, NgModel, ReactiveFormsModule} from '@angular/forms';
import {Component, DebugElement} from '@angular/core';
import {Component, DebugElement, ViewChild} from '@angular/core';
import {By} from '@angular/platform-browser';
import {dispatchFakeEvent} from '@angular/cdk/testing';
import {defaultRippleAnimationConfig} from '@angular/material/core';
Expand All @@ -12,6 +12,7 @@ describe('MatRadio', () => {
TestBed.configureTestingModule({
imports: [MatRadioModule, FormsModule, ReactiveFormsModule],
declarations: [
DisableableRadioButton,
FocusableRadioButton,
RadiosInsideRadioGroup,
RadioGroupWithNgModel,
Expand Down Expand Up @@ -515,6 +516,39 @@ describe('MatRadio', () => {
});
});

describe('disableable', () => {
let fixture: ComponentFixture<DisableableRadioButton>;
let radioDebugElement: DebugElement;
let radioInstance: MatRadioButton;
let radioNativeElement; HTMLInputElement;
let testComponent: DisableableRadioButton;

beforeEach(() => {
fixture = TestBed.createComponent(DisableableRadioButton);
fixture.detectChanges();

testComponent = fixture.debugElement.componentInstance;
radioDebugElement = fixture.debugElement.query(By.directive(MatRadioButton));
radioInstance = radioDebugElement.injector.get<MatRadioButton>(MatRadioButton);
radioNativeElement = radioDebugElement.nativeElement.querySelector('input');
});

fit('should toggle the disabled state', () => {
expect(radioInstance.disabled).toBeFalsy();
expect(radioNativeElement.disabled).toBeFalsy();

testComponent.disabled = true;
fixture.detectChanges();
expect(radioInstance.disabled).toBeTruthy();
expect(radioNativeElement.disabled).toBeTruthy();

testComponent.disabled = false;
fixture.detectChanges();
expect(radioInstance.disabled).toBeFalsy();
expect(radioNativeElement.disabled).toBeFalsy();
});
});

describe('as standalone', () => {
let fixture: ComponentFixture<StandaloneRadioButtons>;
let radioDebugElements: DebugElement[];
Expand Down Expand Up @@ -795,6 +829,17 @@ class RadioGroupWithNgModel {
lastEvent: MatRadioChange;
}

@Component({
template: `<mat-radio-button>One</mat-radio-button>`
})
class DisableableRadioButton {
@ViewChild(MatRadioButton) matRadioButton;

set disabled(value: boolean) {
this.matRadioButton.disabled = value;
}
}

@Component({
template: `
<mat-radio-group [formControl]="formControl">
Expand Down
6 changes: 5 additions & 1 deletion src/lib/radio/radio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,11 @@ export class MatRadioButton extends _MatRadioButtonMixinBase
return this._disabled || (this.radioGroup !== null && this.radioGroup.disabled);
}
set disabled(value: boolean) {
this._disabled = coerceBooleanProperty(value);
const newDisabledState = coerceBooleanProperty(value);
if (this._disabled !== newDisabledState) {
this._disabled = newDisabledState;
this._changeDetector.markForCheck();
}
}

/** Whether the radio button is required. */
Expand Down

0 comments on commit 4a15a68

Please sign in to comment.