mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
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:
@@ -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;
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user