Skip to content

Commit

Permalink
fix(material/checkbox): incorrect animation state when going from pre…
Browse files Browse the repository at this point in the history
…-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.
  • Loading branch information
crisbeto authored Jul 20, 2022
1 parent c440f84 commit cf6b83e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions scripts/check-mdc-tests-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const config = {
'should not remove margin if initial label is set through binding',
'should re-add margin if label is added asynchronously',
'should properly update margin if label content is projected',
'should transition correctly from initially checked to indeterminate',

// TODO: the focus origin behavior needs to be implemented in the MDC checkbox
'should not change focus origin if origin not specified',
Expand Down
15 changes: 15 additions & 0 deletions src/material/checkbox/checkbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,21 @@ describe('MatCheckbox', () => {
'mat-checkbox-anim-unchecked-indeterminate',
);
}));

it('should transition correctly from initially checked to indeterminate', () => {
testComponent.isIndeterminate = false;
testComponent.isChecked = true;
fixture.detectChanges();

expect(checkboxNativeElement.className).not.toMatch(/^mat\-checkbox\-anim/g);

testComponent.isIndeterminate = testComponent.isChecked = true;
fixture.detectChanges();

expect(checkboxNativeElement.classList).toContain(
'mat-checkbox-anim-checked-indeterminate',
);
});
});

describe(`when MAT_CHECKBOX_CLICK_ACTION is 'check'`, () => {
Expand Down
6 changes: 4 additions & 2 deletions src/material/checkbox/checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ export abstract class _MatCheckboxBase<E>
if (oldState === newState || !element) {
return;
}
if (this._currentAnimationClass.length > 0) {
if (this._currentAnimationClass) {
element.classList.remove(this._currentAnimationClass);
}

Expand Down Expand Up @@ -438,7 +438,9 @@ export abstract class _MatCheckboxBase<E>
if (newState === TransitionCheckState.Checked) {
return this._animationClasses.uncheckedToChecked;
} else if (newState == TransitionCheckState.Indeterminate) {
return this._animationClasses.uncheckedToIndeterminate;
return this._checked
? this._animationClasses.checkedToIndeterminate
: this._animationClasses.uncheckedToIndeterminate;
}
break;
case TransitionCheckState.Unchecked:
Expand Down

0 comments on commit cf6b83e

Please sign in to comment.