From f11c5eb4c82f64c39b87f0557cfc725865e079b2 Mon Sep 17 00:00:00 2001 From: tinayuangao Date: Tue, 17 Jan 2017 19:53:38 -0800 Subject: [PATCH] fix(checkbox): Emit event when checkbox's indeterminate value changes (#2130) * Emit event when checkbox indeterminate value changes * Add test * Use boolean and emit indeterminate only if value changed --- src/lib/checkbox/checkbox.spec.ts | 35 ++++++++++++++++++++++++++++++- src/lib/checkbox/checkbox.ts | 12 ++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/lib/checkbox/checkbox.spec.ts b/src/lib/checkbox/checkbox.spec.ts index ff830032ffa6..5e45167f0f8d 100644 --- a/src/lib/checkbox/checkbox.spec.ts +++ b/src/lib/checkbox/checkbox.spec.ts @@ -104,6 +104,39 @@ describe('MdCheckbox', () => { expect(inputElement.indeterminate).toBe(false); }); + it('should set indeterminate to false when set checked', () => { + testComponent.isIndeterminate = true; + fixture.detectChanges(); + + expect(checkboxInstance.indeterminate).toBe(true); + expect(inputElement.indeterminate).toBe(true); + expect(testComponent.isIndeterminate).toBe(true); + + testComponent.isChecked = true; + fixture.detectChanges(); + + expect(checkboxInstance.checked).toBe(true); + expect(inputElement.indeterminate).toBe(false); + expect(inputElement.checked).toBe(true); + expect(testComponent.isIndeterminate).toBe(false); + + testComponent.isIndeterminate = true; + fixture.detectChanges(); + + expect(checkboxInstance.indeterminate).toBe(true); + expect(inputElement.indeterminate).toBe(true); + expect(inputElement.checked).toBe(true); + expect(testComponent.isIndeterminate).toBe(true); + + testComponent.isChecked = false; + fixture.detectChanges(); + + expect(checkboxInstance.checked).toBe(false); + expect(inputElement.indeterminate).toBe(false); + expect(inputElement.checked).toBe(false); + expect(testComponent.isIndeterminate).toBe(false); + }); + it('should change native element checked when check programmatically', () => { expect(inputElement.checked).toBe(false); @@ -627,7 +660,7 @@ describe('MdCheckbox', () => { [required]="isRequired" [labelPosition]="labelPos" [checked]="isChecked" - [indeterminate]="isIndeterminate" + [(indeterminate)]="isIndeterminate" [disabled]="isDisabled" [color]="checkboxColor" (change)="changeCount = changeCount + 1" diff --git a/src/lib/checkbox/checkbox.ts b/src/lib/checkbox/checkbox.ts index f89f3146df3a..a8d36d0c3555 100644 --- a/src/lib/checkbox/checkbox.ts +++ b/src/lib/checkbox/checkbox.ts @@ -147,6 +147,9 @@ export class MdCheckbox implements ControlValueAccessor { /** Event emitted when the checkbox's `checked` value changes. */ @Output() change: EventEmitter = new EventEmitter(); + /** Event emitted when the checkbox's `indeterminate` value changes. */ + @Output() indeterminateChange: EventEmitter = new EventEmitter(); + /** The native ` element */ @ViewChild('input') _inputElement: ElementRef; @@ -186,7 +189,10 @@ export class MdCheckbox implements ControlValueAccessor { set checked(checked: boolean) { if (checked != this.checked) { - this._indeterminate = false; + if (this._indeterminate) { + this._indeterminate = false; + this.indeterminateChange.emit(this._indeterminate); + } this._checked = checked; this._transitionCheckState( this._checked ? TransitionCheckState.Checked : TransitionCheckState.Unchecked); @@ -208,6 +214,7 @@ export class MdCheckbox implements ControlValueAccessor { } set indeterminate(indeterminate: boolean) { + let changed = indeterminate != this._indeterminate; this._indeterminate = indeterminate; if (this._indeterminate) { this._transitionCheckState(TransitionCheckState.Indeterminate); @@ -215,6 +222,9 @@ export class MdCheckbox implements ControlValueAccessor { this._transitionCheckState( this.checked ? TransitionCheckState.Checked : TransitionCheckState.Unchecked); } + if (changed) { + this.indeterminateChange.emit(this._indeterminate); + } } /** The color of the button. Can be `primary`, `accent`, or `warn`. */