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

bug(mat-checkbox): Setting value programmatically from changed listener detaches state from template #22149

Closed
creativeux opened this issue Mar 9, 2021 · 1 comment · Fixed by #22316
Assignees
Labels
Accessibility This issue is related to accessibility (a11y) area: material/checkbox P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Comments

@creativeux
Copy link

Reproduction

Use StackBlitz to reproduce your issue:
https://stackblitz.com/edit/angular-ivy-zppefd?file=src%2Fapp%2Fapp.component.ts

Steps to reproduce:

  1. Click "Sausage"
  2. Click "Cheese"
  3. Works correctly for plain HTML inputs, and incorrectly for mat-checkbox.

Expected Behavior

What behavior were you expecting to see?

  • Underlying input element should appropriately represent the checked property.

Actual Behavior

What behavior did you actually see?

  • When invoking controls['control'].setValue(value) within the (changed) listener, the JavaScript state becomes out of sync with the DOM and the checked property is not set accurately.
  • Removing the setValue() command from the tick stack with setTimeout() allows a subsequent digest to capture the change and keep things in sync. (see app.component.ts).
  • When styles are applied, the state mismatch is masked by the presence of the .mat-checkbox-checked class as opposed to relying on the :checked pseudo-class. Commenting / uncommenting the style @import reveals the state mismatch by showing the underlying input.

Environment

  • Angular: 10+ (at least)
  • CDK/Material: 10+ (at least)
  • Browser(s): Yes
  • Operating System (e.g. Windows, macOS, Ubuntu): OSX
@creativeux creativeux added the needs triage This issue needs to be triaged by the team label Mar 9, 2021
@jelbourn jelbourn added Accessibility This issue is related to accessibility (a11y) area: material/checkbox P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent and removed needs triage This issue needs to be triaged by the team labels Mar 9, 2021
@crisbeto crisbeto self-assigned this Mar 23, 2021
crisbeto added a commit to crisbeto/material2 that referenced this issue Mar 23, 2021
…changed inside event

Fixes that the state of the native checkbox might be out of sync if the `checked` property is changed inside the `changed` output.

Fixes angular#22149.
crisbeto added a commit to crisbeto/material2 that referenced this issue Mar 23, 2021
…changed inside event

Fixes that the state of the native checkbox might be out of sync if the `checked` property is changed inside the `changed` output.

Fixes angular#22149.
wagnermaciel pushed a commit that referenced this issue Mar 27, 2021
…changed inside event (#22316)

Fixes that the state of the native checkbox might be out of sync if the `checked` property is changed inside the `changed` output.

Fixes #22149.
wagnermaciel pushed a commit that referenced this issue Mar 27, 2021
…changed inside event (#22316)

Fixes that the state of the native checkbox might be out of sync if the `checked` property is changed inside the `changed` output.

Fixes #22149.

(cherry picked from commit c679416)
@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 Apr 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Accessibility This issue is related to accessibility (a11y) area: material/checkbox P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Projects
None yet
3 participants