Skip to content

fix(radio): update MatRadioButton disabled setter to trigger change detection #11056

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
May 4, 2018
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
46 changes: 45 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,38 @@ describe('MatRadio', () => {
});
});

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

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

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

it('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 +828,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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set disabled should trigger change detection with @Input()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not appears to be the case. Demo: https://stackblitz.com/edit/angular-material-feiydh?file=app%2Fapp.component.html.

@input appears to cause for the setter to trigger change detection when tripped during template binding, but it doesn't appear to rewrite the setter implementation to always call change detection, so direct accesses to the component instance don't trigger change detection. Sometimes (e.g., when using ng-content), direct instance access is unavoidable, and there needs to be some way to disable a radio button. If there's a better way that I missed, let me know.

Potential reference (unresolved issue in Angular): angular/angular#20611

if (this._disabled !== newDisabledState) {
this._disabled = newDisabledState;
this._changeDetector.markForCheck();
}
}

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