mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
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:
@@ -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"
|
||||
|
||||
@@ -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`. */
|
||||
|
||||
Reference in New Issue
Block a user