Skip to content

Commit 14a235b

Browse files
committed
fix(radio): Uncheck radio group if uncheck radio button programmatically
1 parent 437ec8e commit 14a235b

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

src/lib/radio/radio.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,44 @@ describe('MdRadio', () => {
216216

217217
expect(radioInstances.every(radio => !radio.checked)).toBe(true);
218218
});
219+
220+
it('should update the group\'s selected radio to null when unchecking that radio '
221+
+ 'programmatically', () => {
222+
let changeSpy = jasmine.createSpy('radio-group change listener');
223+
groupInstance.change.subscribe(changeSpy);
224+
radioInstances[0].checked = true;
225+
226+
fixture.detectChanges();
227+
228+
expect(changeSpy).toHaveBeenCalled();
229+
expect(groupInstance.value).toBeTruthy();
230+
231+
radioInstances[0].checked = false;
232+
233+
fixture.detectChanges();
234+
235+
expect(changeSpy).toHaveBeenCalledTimes(2);
236+
expect(groupInstance.value).toBeFalsy();
237+
expect(radioInstances.every(radio => !radio.checked)).toBe(true);
238+
expect(groupInstance.selected).toBeNull();
239+
});
240+
241+
it('should fire a change event from the group whenever a radio checked state changes', () => {
242+
let changeSpy = jasmine.createSpy('radio-group change listener');
243+
groupInstance.change.subscribe(changeSpy);
244+
radioInstances[0].checked = true;
245+
246+
fixture.detectChanges();
247+
248+
expect(changeSpy).toHaveBeenCalled();
249+
expect(groupInstance.value).toBeTruthy();
250+
251+
radioInstances[1].checked = true;
252+
253+
fixture.detectChanges();
254+
255+
expect(changeSpy).toHaveBeenCalledTimes(2);
256+
});
219257
});
220258

221259
describe('group with ngModel', () => {

src/lib/radio/radio.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,15 +295,19 @@ export class MdRadioButton implements OnInit {
295295
}
296296

297297
set checked(newCheckedState: boolean) {
298-
if (newCheckedState) {
299-
// Notify all radio buttons with the same name to un-check.
300-
this.radioDispatcher.notify(this.id, this.name);
301-
}
302-
303298
this._checked = newCheckedState;
304299

305300
if (newCheckedState && this.radioGroup && this.radioGroup.value != this.value) {
306301
this.radioGroup.selected = this;
302+
} else if (!newCheckedState && this.radioGroup && this.radioGroup.value == this.value) {
303+
// When unchecking the selected radio button, update the selected radio
304+
// property on the group.
305+
this.radioGroup.selected = null;
306+
}
307+
308+
if (newCheckedState) {
309+
// Notify all radio buttons with the same name to un-check.
310+
this.radioDispatcher.notify(this.id, this.name);
307311
}
308312
}
309313

0 commit comments

Comments
 (0)