Skip to content

Commit 7d3e962

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 415cfad commit 7d3e962

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

scripts/check-mdc-tests-config.ts

+1
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

+15
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,21 @@ describe('MatCheckbox', () => {
557557
'mat-checkbox-anim-unchecked-indeterminate',
558558
);
559559
}));
560+
561+
it('should transition correctly from initially checked to indeterminate', () => {
562+
testComponent.isIndeterminate = false;
563+
testComponent.isChecked = true;
564+
fixture.detectChanges();
565+
566+
expect(checkboxNativeElement.className).not.toMatch(/^mat\-checkbox\-anim/g);
567+
568+
testComponent.isIndeterminate = testComponent.isChecked = true;
569+
fixture.detectChanges();
570+
571+
expect(checkboxNativeElement.classList).toContain(
572+
'mat-checkbox-anim-checked-indeterminate',
573+
);
574+
});
560575
});
561576

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

src/material/checkbox/checkbox.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ export abstract class _MatCheckboxBase<E>
332332
if (oldState === newState || !element) {
333333
return;
334334
}
335-
if (this._currentAnimationClass.length > 0) {
335+
if (this._currentAnimationClass) {
336336
element.classList.remove(this._currentAnimationClass);
337337
}
338338

@@ -438,7 +438,9 @@ export abstract class _MatCheckboxBase<E>
438438
if (newState === TransitionCheckState.Checked) {
439439
return this._animationClasses.uncheckedToChecked;
440440
} else if (newState == TransitionCheckState.Indeterminate) {
441-
return this._animationClasses.uncheckedToIndeterminate;
441+
return this._checked
442+
? this._animationClasses.checkedToIndeterminate
443+
: this._animationClasses.uncheckedToIndeterminate;
442444
}
443445
break;
444446
case TransitionCheckState.Unchecked:

0 commit comments

Comments
 (0)