Skip to content

fix(select): consider value changes via arrow keys on closed select as user actions #5112

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 2 commits into from
Jun 13, 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
12 changes: 12 additions & 0 deletions src/lib/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
FloatPlaceholderType,
MD_PLACEHOLDER_GLOBAL_OPTIONS
} from '../core/placeholder/placeholder-options';
import {map} from 'rxjs/operator/map';


describe('MdSelect', () => {
Expand Down Expand Up @@ -1676,6 +1677,17 @@ describe('MdSelect', () => {
expect(event.defaultPrevented).toBe(true);
});

it('should consider the selection as a result of a user action when closed', () => {
const option = fixture.componentInstance.options.first;
const spy = jasmine.createSpy('option selection spy');
const subscription = map.call(option.onSelectionChange, e => e.isUserInput).subscribe(spy);

dispatchKeyboardEvent(select, 'keydown', DOWN_ARROW);
expect(spy).toHaveBeenCalledWith(true);

subscription.unsubscribe();
});

});

describe('for options', () => {
Expand Down
12 changes: 6 additions & 6 deletions src/lib/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
* Sets the selected option based on a value. If no option can be
* found with the designated value, the select trigger is cleared.
*/
private _setSelectionByValue(value: any | any[]): void {
private _setSelectionByValue(value: any | any[], isUserInput = false): void {
const isArray = Array.isArray(value);

if (this.multiple && value && !isArray) {
Expand All @@ -576,10 +576,10 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
this._clearSelection();

if (isArray) {
value.forEach((currentValue: any) => this._selectValue(currentValue));
value.forEach((currentValue: any) => this._selectValue(currentValue, isUserInput));
this._sortValues();
} else {
this._selectValue(value);
this._selectValue(value, isUserInput);
}

this._setValueWidth();
Expand All @@ -595,14 +595,14 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
* Finds and selects and option based on its value.
* @returns Option that has the corresponding value.
*/
private _selectValue(value: any): MdOption {
private _selectValue(value: any, isUserInput = false): MdOption {
let optionsArray = this.options.toArray();
let correspondingOption = optionsArray.find(option => {
return option.value != null && option.value === value;
});

if (correspondingOption) {
correspondingOption.select();
isUserInput ? correspondingOption._selectViaInteraction() : correspondingOption.select();
this._selectionModel.select(correspondingOption);
this._keyManager.setActiveItem(optionsArray.indexOf(correspondingOption));
}
Expand Down Expand Up @@ -1027,7 +1027,7 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On

if (currentActiveItem !== prevActiveItem) {
this._clearSelection();
this._setSelectionByValue(currentActiveItem.value);
this._setSelectionByValue(currentActiveItem.value, true);
this._propagateChanges();
}
}
Expand Down