Skip to content
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

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

Merged
merged 1 commit into from
May 4, 2018

Conversation

stevenyxu
Copy link
Contributor

@stevenyxu stevenyxu commented Apr 28, 2018

Fixes MatRadioButton.disabled setter to trigger change detection. MatRadioButton uses
ChangeDetectionStrategy.OnPush, which works fine with disabled is bound in a template that uses
. 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.

Related open Angular bug: angular/angular#20611

@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Apr 28, 2018
@@ -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

@tinayuangao tinayuangao added the G This is is related to a Google internal issue label Apr 30, 2018
Copy link
Contributor

@tinayuangao tinayuangao left a comment

Choose a reason for hiding this comment

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

LGTM

@stevenyxu stevenyxu force-pushed the master branch 2 times, most recently from 02587e5 to 5c47d56 Compare May 1, 2018 17:30
@stevenyxu stevenyxu changed the title fix(radio): update set disabled directly on MatRadioButton to trigger change detection fix(radio): update MatRadioButton disabled setter to trigger change detection May 1, 2018
stevenyxu added a commit to stevenyxu/components that referenced this pull request May 1, 2018
…etection

This is related to angular#11056, which is a similar fix for
MatRadioButton.

The MatCheckbox component instance may be accessed directly by a parent component using
ViewChild/ContentChild. Since MatCheckbox uses OnPush change detection setting the disabled property
directly currently doesn't cause change detection leading to a stale or broken state. Accessing it
through a template binding does correctly trigger change detection on MatCheckbox (there are already
test cases for this method). We need to add a manual call to markForCheck, or else MatCheckbox will
never get kicked if disabled is set directly on the component instance.

Related open Angular bug: angular/angular#20611
stevenyxu added a commit to stevenyxu/components that referenced this pull request May 1, 2018
…etection

This is related to angular#11056, which is a similar fix for
MatRadioButton.

The MatCheckbox component instance may be accessed directly by a parent component using
ViewChild/ContentChild. Since MatCheckbox uses OnPush change detection setting the disabled property
directly currently doesn't cause change detection leading to a stale or broken state. Accessing it
through a template binding does correctly trigger change detection on MatCheckbox (there are already
test cases for this method). We need to add a manual call to markForCheck, or else MatCheckbox will
never get kicked if disabled is set directly on the component instance.

Related open Angular bug: angular/angular#20611
@stevenyxu stevenyxu closed this May 1, 2018
… 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.
@stevenyxu
Copy link
Contributor Author

I didn't mean to close this. Got my branches mixed up.

@stevenyxu stevenyxu reopened this May 1, 2018
@tinayuangao tinayuangao added pr: lgtm action: merge The PR is ready for merge by the caretaker labels May 2, 2018
@ngbot
Copy link

ngbot bot commented May 2, 2018

I see that you just added the pr: merge ready label, but the following checks are still failing:
    failure missing required label: "target: *"
    pending 1 pending code review

If you want your PR to be merged, it has to pass all the CI checks.

If you can't get the PR to a green state due to flakes or broken master, please try rebasing to master and/or restarting the CI job. If that fails and you believe that the issue is not due to your change, please contact the caretaker and ask for help.

@tinayuangao tinayuangao added the target: patch This PR is targeted for the next patch release label May 2, 2018
josephperrott pushed a commit that referenced this pull request May 2, 2018
…etection

This is related to #11056, which is a similar fix for
MatRadioButton.

The MatCheckbox component instance may be accessed directly by a parent component using
ViewChild/ContentChild. Since MatCheckbox uses OnPush change detection setting the disabled property
directly currently doesn't cause change detection leading to a stale or broken state. Accessing it
through a template binding does correctly trigger change detection on MatCheckbox (there are already
test cases for this method). We need to add a manual call to markForCheck, or else MatCheckbox will
never get kicked if disabled is set directly on the component instance.

Related open Angular bug: angular/angular#20611
josephperrott pushed a commit that referenced this pull request May 2, 2018
…etection

This is related to #11056, which is a similar fix for
MatRadioButton.

The MatCheckbox component instance may be accessed directly by a parent component using
ViewChild/ContentChild. Since MatCheckbox uses OnPush change detection setting the disabled property
directly currently doesn't cause change detection leading to a stale or broken state. Accessing it
through a template binding does correctly trigger change detection on MatCheckbox (there are already
test cases for this method). We need to add a manual call to markForCheck, or else MatCheckbox will
never get kicked if disabled is set directly on the component instance.

Related open Angular bug: angular/angular#20611
@josephperrott josephperrott merged commit 860ce13 into angular:master May 4, 2018
josephperrott pushed a commit that referenced this pull request May 4, 2018
jelbourn pushed a commit that referenced this pull request May 8, 2018
…etection (#11098)

This is related to #11056, which is a similar fix for
MatRadioButton.

The MatCheckbox component instance may be accessed directly by a parent component using
ViewChild/ContentChild. Since MatCheckbox uses OnPush change detection setting the disabled property
directly currently doesn't cause change detection leading to a stale or broken state. Accessing it
through a template binding does correctly trigger change detection on MatCheckbox (there are already
test cases for this method). We need to add a manual call to markForCheck, or else MatCheckbox will
never get kicked if disabled is set directly on the component instance.

Related open Angular bug: angular/angular#20611
jelbourn pushed a commit to jelbourn/components that referenced this pull request May 14, 2018
…etection (angular#11098)

This is related to angular#11056, which is a similar fix for
MatRadioButton.

The MatCheckbox component instance may be accessed directly by a parent component using
ViewChild/ContentChild. Since MatCheckbox uses OnPush change detection setting the disabled property
directly currently doesn't cause change detection leading to a stale or broken state. Accessing it
through a template binding does correctly trigger change detection on MatCheckbox (there are already
test cases for this method). We need to add a manual call to markForCheck, or else MatCheckbox will
never get kicked if disabled is set directly on the component instance.

Related open Angular bug: angular/angular#20611
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement G This is is related to a Google internal issue target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants