From 813ad00c29e1d3725562aded185bcd2c8b4b3a79 Mon Sep 17 00:00:00 2001 From: Yuan Gao Date: Tue, 1 Nov 2016 13:20:17 -0700 Subject: [PATCH] fix(radio): only emit change event with user input --- src/lib/radio/radio.spec.ts | 21 ++++++++++++--------- src/lib/radio/radio.ts | 16 +++++++++++----- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/lib/radio/radio.spec.ts b/src/lib/radio/radio.spec.ts index 361854ddae93..5545a4fb55a7 100644 --- a/src/lib/radio/radio.spec.ts +++ b/src/lib/radio/radio.spec.ts @@ -152,21 +152,22 @@ describe('MdRadio', () => { expect(spies[1]).toHaveBeenCalledTimes(1); }); - it('should emit a change event from the radio group', () => { + it('should not emit a change event from the radio group when change group value ' + + 'programmatically', () => { expect(groupInstance.value).toBeFalsy(); let changeSpy = jasmine.createSpy('radio-group change listener'); groupInstance.change.subscribe(changeSpy); - groupInstance.value = 'fire'; + radioLabelElements[0].click(); fixture.detectChanges(); - expect(changeSpy).toHaveBeenCalled(); + expect(changeSpy).toHaveBeenCalledTimes(1); groupInstance.value = 'water'; fixture.detectChanges(); - expect(changeSpy).toHaveBeenCalledTimes(2); + expect(changeSpy).toHaveBeenCalledTimes(1); }); // TODO(jelbourn): test this in an e2e test with *real* focus, rather than faking @@ -242,34 +243,36 @@ describe('MdRadio', () => { fixture.detectChanges(); - expect(changeSpy).toHaveBeenCalled(); + expect(changeSpy).toHaveBeenCalledTimes(0); expect(groupInstance.value).toBeTruthy(); radioInstances[0].checked = false; fixture.detectChanges(); - expect(changeSpy).toHaveBeenCalledTimes(2); + expect(changeSpy).toHaveBeenCalledTimes(0); expect(groupInstance.value).toBeFalsy(); expect(radioInstances.every(radio => !radio.checked)).toBe(true); expect(groupInstance.selected).toBeNull(); }); - it('should fire a change event from the group whenever a radio checked state changes', () => { + it('should not fire a change event from the group when a radio checked state changes', () => { let changeSpy = jasmine.createSpy('radio-group change listener'); groupInstance.change.subscribe(changeSpy); radioInstances[0].checked = true; fixture.detectChanges(); - expect(changeSpy).toHaveBeenCalled(); + expect(changeSpy).toHaveBeenCalledTimes(0); expect(groupInstance.value).toBeTruthy(); + expect(groupInstance.value).toBe('fire'); radioInstances[1].checked = true; fixture.detectChanges(); - expect(changeSpy).toHaveBeenCalledTimes(2); + expect(groupInstance.value).toBe('water'); + expect(changeSpy).toHaveBeenCalledTimes(0); }); }); diff --git a/src/lib/radio/radio.ts b/src/lib/radio/radio.ts index f272a8a82a2b..bae13f07c039 100644 --- a/src/lib/radio/radio.ts +++ b/src/lib/radio/radio.ts @@ -127,11 +127,6 @@ export class MdRadioGroup implements AfterContentInit, ControlValueAccessor { this._value = newValue; this._updateSelectedRadioFromValue(); - - // Only fire a change event if this isn't the first time the value is ever set. - if (this._isInitialized) { - this._emitChangeEvent(); - } } } @@ -171,6 +166,13 @@ export class MdRadioGroup implements AfterContentInit, ControlValueAccessor { } } + emitChangeEvent(): void { + // Only fire a change event if this isn't the first time the value is ever set. + if (this._isInitialized) { + this._emitChangeEvent(); + } + } + private _updateRadioButtonNames(): void { if (this._radios) { this._radios.forEach(radio => { @@ -418,12 +420,16 @@ export class MdRadioButton implements OnInit { // emit its event object to the `change` output. event.stopPropagation(); + let groupValueChanged = this.value != this.radioGroup.value; this.checked = true; this._emitChangeEvent(); if (this.radioGroup) { this.radioGroup._controlValueAccessorChangeFn(this.value); this.radioGroup._touch(); + if (groupValueChanged) { + this.radioGroup.emitChangeEvent(); + } } }