Skip to content

Commit 73aa43e

Browse files
crisbetokara
authored andcommitted
fix(select): consider value changes via arrow keys on closed select as user actions (#5112)
Fixes #5084.
1 parent 3eb0681 commit 73aa43e

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/lib/select/select.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
FloatPlaceholderType,
3030
MD_PLACEHOLDER_GLOBAL_OPTIONS
3131
} from '../core/placeholder/placeholder-options';
32+
import {map} from 'rxjs/operator/map';
3233

3334

3435
describe('MdSelect', () => {
@@ -1676,6 +1677,17 @@ describe('MdSelect', () => {
16761677
expect(event.defaultPrevented).toBe(true);
16771678
});
16781679

1680+
it('should consider the selection as a result of a user action when closed', () => {
1681+
const option = fixture.componentInstance.options.first;
1682+
const spy = jasmine.createSpy('option selection spy');
1683+
const subscription = map.call(option.onSelectionChange, e => e.isUserInput).subscribe(spy);
1684+
1685+
dispatchKeyboardEvent(select, 'keydown', DOWN_ARROW);
1686+
expect(spy).toHaveBeenCalledWith(true);
1687+
1688+
subscription.unsubscribe();
1689+
});
1690+
16791691
});
16801692

16811693
describe('for options', () => {

src/lib/select/select.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
566566
* Sets the selected option based on a value. If no option can be
567567
* found with the designated value, the select trigger is cleared.
568568
*/
569-
private _setSelectionByValue(value: any | any[]): void {
569+
private _setSelectionByValue(value: any | any[], isUserInput = false): void {
570570
const isArray = Array.isArray(value);
571571

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

578578
if (isArray) {
579-
value.forEach((currentValue: any) => this._selectValue(currentValue));
579+
value.forEach((currentValue: any) => this._selectValue(currentValue, isUserInput));
580580
this._sortValues();
581581
} else {
582-
this._selectValue(value);
582+
this._selectValue(value, isUserInput);
583583
}
584584

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

604604
if (correspondingOption) {
605-
correspondingOption.select();
605+
isUserInput ? correspondingOption._selectViaInteraction() : correspondingOption.select();
606606
this._selectionModel.select(correspondingOption);
607607
this._keyManager.setActiveItem(optionsArray.indexOf(correspondingOption));
608608
}
@@ -1027,7 +1027,7 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
10271027

10281028
if (currentActiveItem !== prevActiveItem) {
10291029
this._clearSelection();
1030-
this._setSelectionByValue(currentActiveItem.value);
1030+
this._setSelectionByValue(currentActiveItem.value, true);
10311031
this._propagateChanges();
10321032
}
10331033
}

0 commit comments

Comments
 (0)