Skip to content

Commit b73b2fb

Browse files
committed
fix(material/checkbox): incorrect animation state when going from pre-checked to indeterminate (#25297)
Fixes that we weren't setting the correct animation class when the checkbox goes from being pre-checked to indeterminate. Fixes #25289. (cherry picked from commit cf6b83e)
1 parent ea4148d commit b73b2fb

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

scripts/check-mdc-tests-config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export const config = {
2424
'should not remove margin if initial label is set through binding',
2525
'should re-add margin if label is added asynchronously',
2626
'should properly update margin if label content is projected',
27+
'should transition correctly from initially checked to indeterminate',
2728

2829
// TODO: the focus origin behavior needs to be implemented in the MDC checkbox
2930
'should not change focus origin if origin not specified',

src/material/checkbox/checkbox.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,21 @@ describe('MatCheckbox', () => {
562562
'mat-checkbox-anim-unchecked-indeterminate',
563563
);
564564
}));
565+
566+
it('should transition correctly from initially checked to indeterminate', () => {
567+
testComponent.isIndeterminate = false;
568+
testComponent.isChecked = true;
569+
fixture.detectChanges();
570+
571+
expect(checkboxNativeElement.className).not.toMatch(/^mat\-checkbox\-anim/g);
572+
573+
testComponent.isIndeterminate = testComponent.isChecked = true;
574+
fixture.detectChanges();
575+
576+
expect(checkboxNativeElement.classList).toContain(
577+
'mat-checkbox-anim-checked-indeterminate',
578+
);
579+
});
565580
});
566581

567582
describe(`when MAT_CHECKBOX_CLICK_ACTION is 'check'`, () => {

src/material/checkbox/checkbox.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ export abstract class _MatCheckboxBase<E>
329329
if (oldState === newState || !element) {
330330
return;
331331
}
332-
if (this._currentAnimationClass.length > 0) {
332+
if (this._currentAnimationClass) {
333333
element.classList.remove(this._currentAnimationClass);
334334
}
335335

@@ -435,7 +435,9 @@ export abstract class _MatCheckboxBase<E>
435435
if (newState === TransitionCheckState.Checked) {
436436
return this._animationClasses.uncheckedToChecked;
437437
} else if (newState == TransitionCheckState.Indeterminate) {
438-
return this._animationClasses.uncheckedToIndeterminate;
438+
return this._checked
439+
? this._animationClasses.checkedToIndeterminate
440+
: this._animationClasses.uncheckedToIndeterminate;
439441
}
440442
break;
441443
case TransitionCheckState.Unchecked:

0 commit comments

Comments
 (0)