Skip to content

Commit 1914c89

Browse files
tinayuangaovictoriaaa234
authored andcommitted
fix(checkbox): fix checkbox animation when moved between view containers (#10589)
1 parent 05ed3f8 commit 1914c89

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/lib/checkbox/checkbox.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ describe('MatCheckbox', () => {
207207

208208
expect(checkboxInstance.checked).toBe(false);
209209
expect(checkboxInstance.indeterminate).toBe(false);
210+
211+
flush();
210212
}));
211213

212214
it('should add and remove disabled state', () => {
@@ -526,6 +528,19 @@ describe('MatCheckbox', () => {
526528
it('should not initially have any transition classes', () => {
527529
expect(checkboxNativeElement).not.toMatch(/^mat\-checkbox\-anim/g);
528530
});
531+
532+
it('should not have transition classes when animation ends', fakeAsync(() => {
533+
testComponent.isIndeterminate = true;
534+
fixture.detectChanges();
535+
536+
expect(checkboxNativeElement.classList)
537+
.toContain('mat-checkbox-anim-unchecked-indeterminate');
538+
539+
flush();
540+
541+
expect(checkboxNativeElement.classList)
542+
.not.toContain('mat-checkbox-anim-unchecked-indeterminate');
543+
}));
529544
});
530545

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

src/lib/checkbox/checkbox.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
forwardRef,
2020
Inject,
2121
Input,
22+
NgZone,
2223
OnDestroy,
2324
Optional,
2425
Output,
@@ -184,6 +185,7 @@ export class MatCheckbox extends _MatCheckboxMixinBase implements ControlValueAc
184185
constructor(elementRef: ElementRef,
185186
private _changeDetectorRef: ChangeDetectorRef,
186187
private _focusMonitor: FocusMonitor,
188+
private _ngZone: NgZone,
187189
@Attribute('tabindex') tabIndex: string,
188190
@Optional() @Inject(MAT_CHECKBOX_CLICK_ACTION)
189191
private _clickAction: MatCheckboxClickAction,
@@ -307,6 +309,15 @@ export class MatCheckbox extends _MatCheckboxMixinBase implements ControlValueAc
307309

308310
if (this._currentAnimationClass.length > 0) {
309311
element.classList.add(this._currentAnimationClass);
312+
313+
// Remove the animation class to avoid animation when the checkbox is moved between containers
314+
const animationClass = this._currentAnimationClass;
315+
316+
this._ngZone.runOutsideAngular(() => {
317+
setTimeout(() => {
318+
element.classList.remove(animationClass);
319+
}, 1000);
320+
});
310321
}
311322
}
312323

0 commit comments

Comments
 (0)