Skip to content
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
25 changes: 25 additions & 0 deletions src/lib/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1663,6 +1663,31 @@ describe('MdSelect', () => {
expect(fixture.componentInstance.options.toArray()[6].selected).toBe(true);
});

it('should not shift focus when the selected options are updated programmatically ' +
'in a multi select', () => {
fixture.destroy();

const multiFixture = TestBed.createComponent(MultiSelect);
const instance = multiFixture.componentInstance;

multiFixture.detectChanges();
select = multiFixture.debugElement.query(By.css('md-select')).nativeElement;
multiFixture.componentInstance.select.open();
multiFixture.detectChanges();

const options =
overlayContainerElement.querySelectorAll('md-option') as NodeListOf<HTMLElement>;

options[3].focus();
expect(document.activeElement).toBe(options[3], 'Expected fourth option to be focused.');

multiFixture.componentInstance.control.setValue(['steak-0', 'sushi-7']);
multiFixture.detectChanges();

expect(document.activeElement)
.toBe(options[3], 'Expected fourth option to remain focused.');
});

it('should not cycle through the options if the control is disabled', () => {
const formControl = fixture.componentInstance.control;

Expand Down
12 changes: 8 additions & 4 deletions src/lib/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,13 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
value.forEach((currentValue: any) => this._selectValue(currentValue, isUserInput));
this._sortValues();
} else {
this._selectValue(value, isUserInput);
const correspondingOption = this._selectValue(value, isUserInput);

// Shift focus to the active item. Note that we shouldn't do this in multiple
// mode, because we don't know what option the user interacted with last.
if (correspondingOption) {
this._keyManager.setActiveItem(this.options.toArray().indexOf(correspondingOption));
}
}

this._setValueWidth();
Expand All @@ -592,15 +598,13 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
* @returns Option that has the corresponding value.
*/
private _selectValue(value: any, isUserInput = false): MdOption | undefined {
let optionsArray = this.options.toArray();
let correspondingOption = optionsArray.find(option => {
let correspondingOption = this.options.find(option => {
return option.value != null && option.value === value;
});

if (correspondingOption) {
isUserInput ? correspondingOption._selectViaInteraction() : correspondingOption.select();
this._selectionModel.select(correspondingOption);
this._keyManager.setActiveItem(optionsArray.indexOf(correspondingOption));
}

return correspondingOption;
Expand Down