fix(select): deselect old options when programmatically setting value (#4658)

* fix(select): deselect old options when programmatically setting value

* Review changes

* Fix lint issues: line length
This commit is contained in:
Will Howell
2017-05-23 16:55:25 -04:00
committed by tinayuangao
parent aee42aebb7
commit db72b06b97
2 changed files with 39 additions and 3 deletions
+35
View File
@@ -328,6 +328,41 @@ describe('MdSelect', () => {
expect(optionInstances[2].selected).toBe(false);
});
it('should deselect other options when one is programmatically selected', () => {
let control = fixture.componentInstance.control;
let foods = fixture.componentInstance.foods;
trigger.click();
fixture.detectChanges();
let options =
overlayContainerElement.querySelectorAll('md-option') as NodeListOf<HTMLElement>;
options[0].click();
fixture.detectChanges();
control.setValue(foods[1].value);
fixture.detectChanges();
trigger.click();
fixture.detectChanges();
options =
overlayContainerElement.querySelectorAll('md-option') as NodeListOf<HTMLElement>;
expect(options[0].classList)
.not.toContain('mat-selected', 'Expected first option to no longer be selected');
expect(options[1].classList)
.toContain('mat-selected', 'Expected second option to be selected');
const optionInstances = fixture.componentInstance.options.toArray();
expect(optionInstances[0].selected)
.toBe(false, 'Expected first option to no longer be selected');
expect(optionInstances[1].selected)
.toBe(true, 'Expected second option to be selected');
});
it('should remove selection if option has been removed', async(() => {
let select = fixture.componentInstance.select;
+4 -3
View File
@@ -555,12 +555,13 @@ export class MdSelect implements AfterContentInit, OnDestroy, OnInit, ControlVal
throw getMdSelectNonArrayValueError();
}
this._clearSelection();
if (isArray) {
this._clearSelection();
value.forEach((currentValue: any) => this._selectValue(currentValue));
this._sortValues();
} else if (!this._selectValue(value)) {
this._clearSelection();
} else {
this._selectValue(value);
}
this._setValueWidth();