From f4323b2c0f80a81284402a12852522c72b781346 Mon Sep 17 00:00:00 2001 From: tinayuangao Date: Fri, 3 Mar 2017 17:18:30 -0800 Subject: [PATCH] fix(checkbox): show checkbox animation only if user click or indeterminate state (#3137) Fixes #2783 --- src/lib/checkbox/checkbox.spec.ts | 4 ++-- src/lib/checkbox/checkbox.ts | 19 +++++++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/lib/checkbox/checkbox.spec.ts b/src/lib/checkbox/checkbox.spec.ts index 47d7a1efbc37..c278585214fe 100644 --- a/src/lib/checkbox/checkbox.spec.ts +++ b/src/lib/checkbox/checkbox.spec.ts @@ -384,11 +384,11 @@ describe('MdCheckbox', () => { describe('state transition css classes', () => { it('should transition unchecked -> checked -> unchecked', () => { - testComponent.isChecked = true; + inputElement.click(); fixture.detectChanges(); expect(checkboxNativeElement.classList).toContain('mat-checkbox-anim-unchecked-checked'); - testComponent.isChecked = false; + inputElement.click(); fixture.detectChanges(); expect(checkboxNativeElement.classList) .not.toContain('mat-checkbox-anim-unchecked-checked'); diff --git a/src/lib/checkbox/checkbox.ts b/src/lib/checkbox/checkbox.ts index 14e7d642be5e..a6196e0e3fdf 100644 --- a/src/lib/checkbox/checkbox.ts +++ b/src/lib/checkbox/checkbox.ts @@ -195,8 +195,6 @@ export class MdCheckbox implements ControlValueAccessor { this.indeterminateChange.emit(this._indeterminate); } this._checked = checked; - this._transitionCheckState( - this._checked ? TransitionCheckState.Checked : TransitionCheckState.Unchecked); this._changeDetectorRef.markForCheck(); } } @@ -217,13 +215,14 @@ export class MdCheckbox implements ControlValueAccessor { set indeterminate(indeterminate: boolean) { let changed = indeterminate != this._indeterminate; this._indeterminate = indeterminate; - if (this._indeterminate) { - this._transitionCheckState(TransitionCheckState.Indeterminate); - } else { - this._transitionCheckState( - this.checked ? TransitionCheckState.Checked : TransitionCheckState.Unchecked); - } + if (changed) { + if (this._indeterminate) { + this._transitionCheckState(TransitionCheckState.Indeterminate); + } else { + this._transitionCheckState( + this.checked ? TransitionCheckState.Checked : TransitionCheckState.Unchecked); + } this.indeterminateChange.emit(this._indeterminate); } } @@ -348,6 +347,8 @@ export class MdCheckbox implements ControlValueAccessor { if (!this.disabled) { this.toggle(); + this._transitionCheckState( + this._checked ? TransitionCheckState.Checked : TransitionCheckState.Unchecked); // Emit our custom change event if the native input emitted one. // It is important to only emit it, if the native input triggered one, because @@ -379,6 +380,8 @@ export class MdCheckbox implements ControlValueAccessor { // [checked] bound to it. if (newState === TransitionCheckState.Checked) { animSuffix = 'unchecked-checked'; + } else if (newState == TransitionCheckState.Indeterminate) { + animSuffix = 'unchecked-indeterminate'; } else { return ''; }