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
This commit is contained in:
tinayuangao
2017-01-17 19:53:38 -08:00
committed by mmalerba
parent 46e654b117
commit f11c5eb4c8
2 changed files with 45 additions and 2 deletions
+34 -1
View File
@@ -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"
+11 -1
View File
@@ -147,6 +147,9 @@ export class MdCheckbox implements ControlValueAccessor {
/** Event emitted when the checkbox's `checked` value changes. */
@Output() change: EventEmitter<MdCheckboxChange> = new EventEmitter<MdCheckboxChange>();
/** Event emitted when the checkbox's `indeterminate` value changes. */
@Output() indeterminateChange: EventEmitter<boolean> = new EventEmitter<boolean>();
/** The native `<input type="checkbox"> 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`. */