Skip to content

Commit c108607

Browse files
tinayuangaohansl
authored andcommitted
fix(radio): Uncheck radio group if uncheck radio button programmatically (#1561)
* added test cases for uncheck * notify all radio buttons after status change and radio group value change to avoid notify radio group twice Fixes #609
1 parent 92ac392 commit c108607

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
@@ -233,6 +233,44 @@ describe('MdRadio', () => {
233233
.toBe(0, 'Expect no [md-ripple] in radio buttons');
234234
}
235235
}));
236+
237+
it('should update the group\'s selected radio to null when unchecking that radio '
238+
+ 'programmatically', () => {
239+
let changeSpy = jasmine.createSpy('radio-group change listener');
240+
groupInstance.change.subscribe(changeSpy);
241+
radioInstances[0].checked = true;
242+
243+
fixture.detectChanges();
244+
245+
expect(changeSpy).toHaveBeenCalled();
246+
expect(groupInstance.value).toBeTruthy();
247+
248+
radioInstances[0].checked = false;
249+
250+
fixture.detectChanges();
251+
252+
expect(changeSpy).toHaveBeenCalledTimes(2);
253+
expect(groupInstance.value).toBeFalsy();
254+
expect(radioInstances.every(radio => !radio.checked)).toBe(true);
255+
expect(groupInstance.selected).toBeNull();
256+
});
257+
258+
it('should fire a change event from the group whenever a radio checked state changes', () => {
259+
let changeSpy = jasmine.createSpy('radio-group change listener');
260+
groupInstance.change.subscribe(changeSpy);
261+
radioInstances[0].checked = true;
262+
263+
fixture.detectChanges();
264+
265+
expect(changeSpy).toHaveBeenCalled();
266+
expect(groupInstance.value).toBeTruthy();
267+
268+
radioInstances[1].checked = true;
269+
270+
fixture.detectChanges();
271+
272+
expect(changeSpy).toHaveBeenCalledTimes(2);
273+
});
236274
});
237275

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

src/lib/radio/radio.ts

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

308308
set checked(newCheckedState: boolean) {
309-
if (newCheckedState) {
310-
// Notify all radio buttons with the same name to un-check.
311-
this.radioDispatcher.notify(this.id, this.name);
312-
}
313-
314309
this._checked = newCheckedState;
315310

316311
if (newCheckedState && this.radioGroup && this.radioGroup.value != this.value) {
317312
this.radioGroup.selected = this;
313+
} else if (!newCheckedState && this.radioGroup && this.radioGroup.value == this.value) {
314+
// When unchecking the selected radio button, update the selected radio
315+
// property on the group.
316+
this.radioGroup.selected = null;
317+
}
318+
319+
if (newCheckedState) {
320+
// Notify all radio buttons with the same name to un-check.
321+
this.radioDispatcher.notify(this.id, this.name);
318322
}
319323
}
320324

0 commit comments

Comments
 (0)