Skip to content

Commit d56946a

Browse files
author
Marco Marche
committed
fix(radio-button): Radio buttons do not become unchecked when a bound ngModel property is removed
fixes #327
1 parent dee43e7 commit d56946a

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/components/radio/radio.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,28 @@ export function main() {
352352
});
353353
}).then(done);
354354
});
355+
it('should deselect all buttons when model is null or undefined', (done: () => void) => {
356+
builder
357+
.overrideTemplate(TestAppWithInitialValue, `
358+
<md-radio-group [(ngModel)]="choice">
359+
<md-radio-button [value]="0"></md-radio-button>
360+
<md-radio-button [value]="1"></md-radio-button>
361+
</md-radio-group>`)
362+
.createAsync(TestAppWithInitialValue)
363+
.then(fixture => {
364+
fakeAsync(function() {
365+
let buttons = fixture.debugElement.queryAll(By.css('md-radio-button'));
355366

367+
fixture.detectChanges();
368+
fixture.componentInstance.choice = 0;
369+
expect(isSinglySelected(buttons[0], buttons)).toBe(true);
370+
371+
fixture.detectChanges();
372+
fixture.componentInstance.choice = null;
373+
expect(allDeselected(buttons)).toBe(true);
374+
});
375+
}).then(done);
376+
});
356377
});
357378
}
358379

@@ -365,6 +386,13 @@ function isSinglySelected(button: DebugElement, buttons: DebugElement[]): boolea
365386
return component.checked && otherSelectedButtons.length == 0;
366387
}
367388

389+
/** Checks whether no button is selected from a group of buttons. */
390+
function allDeselected(buttons: DebugElement[]): boolean {
391+
let selectedButtons =
392+
buttons.filter((e: DebugElement) => e.componentInstance.checked);
393+
return selectedButtons.length == 0;
394+
}
395+
368396
/** Browser-agnostic function for creating an event. */
369397
function createEvent(name: string): Event {
370398
let ev: Event;

src/components/radio/radio.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,10 @@ export class MdRadioGroup implements AfterContentInit, ControlValueAccessor {
150150
});
151151

152152
if (matched.length == 0) {
153-
// Didn't find a button that matches this value, return early without setting.
153+
// Didn't find a button that matches this value, deselecting all buttons.
154+
if (this._value == null) {
155+
this.selected = null;
156+
}
154157
return;
155158
}
156159

0 commit comments

Comments
 (0)