Skip to content

Commit

Permalink
fix(radio): only emit change event with user input
Browse files Browse the repository at this point in the history
  • Loading branch information
tinayuangao committed Nov 1, 2016
1 parent 0883fb2 commit 813ad00
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
21 changes: 12 additions & 9 deletions src/lib/radio/radio.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
});
});

Expand Down
16 changes: 11 additions & 5 deletions src/lib/radio/radio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}

Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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();
}
}
}

Expand Down

0 comments on commit 813ad00

Please sign in to comment.