Skip to content

Commit db72b06

Browse files
willshowelltinayuangao
authored andcommitted
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
1 parent aee42ae commit db72b06

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/lib/select/select.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,41 @@ describe('MdSelect', () => {
328328
expect(optionInstances[2].selected).toBe(false);
329329
});
330330

331+
it('should deselect other options when one is programmatically selected', () => {
332+
let control = fixture.componentInstance.control;
333+
let foods = fixture.componentInstance.foods;
334+
335+
trigger.click();
336+
fixture.detectChanges();
337+
338+
let options =
339+
overlayContainerElement.querySelectorAll('md-option') as NodeListOf<HTMLElement>;
340+
341+
options[0].click();
342+
fixture.detectChanges();
343+
344+
control.setValue(foods[1].value);
345+
fixture.detectChanges();
346+
347+
trigger.click();
348+
fixture.detectChanges();
349+
350+
options =
351+
overlayContainerElement.querySelectorAll('md-option') as NodeListOf<HTMLElement>;
352+
353+
expect(options[0].classList)
354+
.not.toContain('mat-selected', 'Expected first option to no longer be selected');
355+
expect(options[1].classList)
356+
.toContain('mat-selected', 'Expected second option to be selected');
357+
358+
const optionInstances = fixture.componentInstance.options.toArray();
359+
360+
expect(optionInstances[0].selected)
361+
.toBe(false, 'Expected first option to no longer be selected');
362+
expect(optionInstances[1].selected)
363+
.toBe(true, 'Expected second option to be selected');
364+
});
365+
331366
it('should remove selection if option has been removed', async(() => {
332367
let select = fixture.componentInstance.select;
333368

src/lib/select/select.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -555,12 +555,13 @@ export class MdSelect implements AfterContentInit, OnDestroy, OnInit, ControlVal
555555
throw getMdSelectNonArrayValueError();
556556
}
557557

558+
this._clearSelection();
559+
558560
if (isArray) {
559-
this._clearSelection();
560561
value.forEach((currentValue: any) => this._selectValue(currentValue));
561562
this._sortValues();
562-
} else if (!this._selectValue(value)) {
563-
this._clearSelection();
563+
} else {
564+
this._selectValue(value);
564565
}
565566

566567
this._setValueWidth();

0 commit comments

Comments
 (0)