Skip to content

Commit

Permalink
fix(radio-button): Radio buttons do not become unchecked when a bound…
Browse files Browse the repository at this point in the history
… ngModel property is removed

fixes #327
  • Loading branch information
trik committed May 13, 2016
1 parent dee43e7 commit d56946a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/components/radio/radio.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,28 @@ export function main() {
});
}).then(done);
});
it('should deselect all buttons when model is null or undefined', (done: () => void) => {
builder
.overrideTemplate(TestAppWithInitialValue, `
<md-radio-group [(ngModel)]="choice">
<md-radio-button [value]="0"></md-radio-button>
<md-radio-button [value]="1"></md-radio-button>
</md-radio-group>`)
.createAsync(TestAppWithInitialValue)
.then(fixture => {
fakeAsync(function() {
let buttons = fixture.debugElement.queryAll(By.css('md-radio-button'));

fixture.detectChanges();
fixture.componentInstance.choice = 0;
expect(isSinglySelected(buttons[0], buttons)).toBe(true);

fixture.detectChanges();
fixture.componentInstance.choice = null;
expect(allDeselected(buttons)).toBe(true);
});
}).then(done);
});
});
}

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

/** Checks whether no button is selected from a group of buttons. */
function allDeselected(buttons: DebugElement[]): boolean {
let selectedButtons =
buttons.filter((e: DebugElement) => e.componentInstance.checked);
return selectedButtons.length == 0;
}

/** Browser-agnostic function for creating an event. */
function createEvent(name: string): Event {
let ev: Event;
Expand Down
5 changes: 4 additions & 1 deletion src/components/radio/radio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ export class MdRadioGroup implements AfterContentInit, ControlValueAccessor {
});

if (matched.length == 0) {
// Didn't find a button that matches this value, return early without setting.
// Didn't find a button that matches this value, deselecting all buttons.
if (this._value == null) {
this.selected = null;
}
return;
}

Expand Down

0 comments on commit d56946a

Please sign in to comment.