Skip to content

Commit

Permalink
fix(radio): Uncheck radio group if uncheck radio button programmatically
Browse files Browse the repository at this point in the history
  • Loading branch information
tinayuangao committed Oct 26, 2016
1 parent 437ec8e commit 14a235b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
38 changes: 38 additions & 0 deletions src/lib/radio/radio.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,44 @@ describe('MdRadio', () => {

expect(radioInstances.every(radio => !radio.checked)).toBe(true);
});

it('should update the group\'s selected radio to null when unchecking that radio '
+ 'programmatically', () => {
let changeSpy = jasmine.createSpy('radio-group change listener');
groupInstance.change.subscribe(changeSpy);
radioInstances[0].checked = true;

fixture.detectChanges();

expect(changeSpy).toHaveBeenCalled();
expect(groupInstance.value).toBeTruthy();

radioInstances[0].checked = false;

fixture.detectChanges();

expect(changeSpy).toHaveBeenCalledTimes(2);
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', () => {
let changeSpy = jasmine.createSpy('radio-group change listener');
groupInstance.change.subscribe(changeSpy);
radioInstances[0].checked = true;

fixture.detectChanges();

expect(changeSpy).toHaveBeenCalled();
expect(groupInstance.value).toBeTruthy();

radioInstances[1].checked = true;

fixture.detectChanges();

expect(changeSpy).toHaveBeenCalledTimes(2);
});
});

describe('group with ngModel', () => {
Expand Down
14 changes: 9 additions & 5 deletions src/lib/radio/radio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,19 @@ export class MdRadioButton implements OnInit {
}

set checked(newCheckedState: boolean) {
if (newCheckedState) {
// Notify all radio buttons with the same name to un-check.
this.radioDispatcher.notify(this.id, this.name);
}

this._checked = newCheckedState;

if (newCheckedState && this.radioGroup && this.radioGroup.value != this.value) {
this.radioGroup.selected = this;
} else if (!newCheckedState && this.radioGroup && this.radioGroup.value == this.value) {
// When unchecking the selected radio button, update the selected radio
// property on the group.
this.radioGroup.selected = null;
}

if (newCheckedState) {
// Notify all radio buttons with the same name to un-check.
this.radioDispatcher.notify(this.id, this.name);
}
}

Expand Down

0 comments on commit 14a235b

Please sign in to comment.