Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/lib/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,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;

Expand Down
7 changes: 4 additions & 3 deletions src/lib/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,12 +544,13 @@ export class MdSelect implements AfterContentInit, OnDestroy, OnInit, ControlVal
throw getMdSelectNonArrayValueError();
}

this._clearSelection();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Can you add a new line after this one?


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();
Expand Down