angular / angular/components

bug(checkbox): Tri-state checkbox runs out of sync with its input after a change event

Open
#27,150 0 comments 3 reactions 0 assignees View on GitHub
area: material/checkbox P3
Dominant language
TypeScript
Stars
25k
Forks
6.8k
Avg merge
1d 8h
Merged PRs (30d)
91

Description

### Is this a regression?

- [ ] Yes, this behavior used to work in the previous version

### The previous version in which this bug was not present was

_No response_

### Description

I think the direct implementation with the `(change)` checkbox output (represented in https://stackblitz.com/edit/angular-tzjh4u?file=src%2Fmain.ts) has never worked.

With the Legacy checkbox, we had a workaround that relied on `click` and `keyup.space` DOM events instead of relying on the component's `change` output.
After migrating to MDC, this workaround no longer works, too.

### Reproduction

https://stackblitz.com/edit/angular-tzjh4u?file=src%2Fmain.ts

Steps to reproduce:
1. Click checkbox to toggle between "checked" and "unchecked" - see how the checkbox state (checkmark) is always in sync with the app state (`this.state`)
2. Click the "Set to indeterminate" button
3. Click the checkbox, which is currently in the indeterminate state

### Expected Behavior

Checkbox becomes "unchecked"

### Actual Behavior

Checkbox becomes "checked", despite `this.state` is "unchecked". Checkbox is now out of sync with its input. Note how `ng-reflect-checked="false"`, while the classname includes `mat-mdc-checkbox-checked`

### Environment

- Angular 16.0.2
- CDK/Material: 16.0.1
- Browser(s): Firefox, Chrome
- Operating System (e.g. Windows, macOS, Ubuntu): macOS

Contributor guide

Open the contributing guide

Research direction

Start with the linked StackBlitz reproduction at src/main.ts and reproduce the transition from indeterminate to unchecked through the checkbox change event. Trace the tri-state checkbox behavior and verify that the visual state and input state remain synchronized after the change.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, typescript
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.