Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Commit bf2867b

Browse files
authored
fix(select): Value inconsistancy (#1902)
fix #1900 fix #1898
1 parent 595afb9 commit bf2867b

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

packages/select/select.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ import { MdcList, MdcListItemChange } from '@angular-mdc/web/list';
4545
import { MdcSelectIcon } from './select-icon';
4646

4747
import { MDCSelectHelperTextFoundation } from '@material/select/helper-text';
48-
import { cssClasses } from '@material/select/constants';
49-
import { MDCSelectFoundation } from '@material/select';
48+
import { cssClasses, MDCSelectFoundation } from '@material/select';
5049

5150
/**
5251
* Represents the default options for mdc-select that can be configured
@@ -78,7 +77,7 @@ export class MdcSelectChange {
7877
constructor(
7978
public source: MdcSelect,
8079
public index: number,
81-
public value: string) { }
80+
public value: any) { }
8281
}
8382

8483
@Directive({
@@ -246,11 +245,11 @@ export class MdcSelect extends _MdcSelectMixinBase implements AfterContentInit,
246245

247246
/** Value of the select control. */
248247
@Input()
249-
get value(): string { return this._value; }
250-
set value(newValue: string) {
248+
get value(): any { return this._value; }
249+
set value(newValue: any) {
251250
this.setSelectionByValue(newValue);
252251
}
253-
private _value: string = '';
252+
private _value: any = '';
254253

255254
@Input()
256255
get helperText(): MdcHelperText | null { return this._helperText; }
@@ -274,7 +273,7 @@ export class MdcSelect extends _MdcSelectMixinBase implements AfterContentInit,
274273
* to facilitate the two-way binding for the `value` input.
275274
*/
276275
@Output() readonly valueChange:
277-
EventEmitter<{ index: number, value: string }> = new EventEmitter<any>();
276+
EventEmitter<{ index: number, value: any }> = new EventEmitter<any>();
278277

279278
@ViewChild(MdcFloatingLabel) _floatingLabel?: MdcFloatingLabel;
280279
@ViewChild(MdcLineRipple) _lineRipple?: MdcLineRipple;
@@ -322,15 +321,15 @@ export class MdcSelect extends _MdcSelectMixinBase implements AfterContentInit,
322321
this._lineRipple.deactivate();
323322
}
324323
},
325-
notifyChange: (value: string) =>
324+
notifyChange: (value: any) =>
326325
this.selectionChange.emit(new MdcSelectChange(this, this.getSelectedIndex(), value))
327326
};
328327
}
329328

330329
private _getNativeSelectAdapterMethods() {
331330
return {
332331
getValue: () => this._platform.isBrowser ? this._getInputElement().value : '',
333-
setValue: (value: string) => this._getInputElement().value = value,
332+
setValue: (value: any) => this._getInputElement().value = value,
334333
isMenuOpen: () => false,
335334
setSelectedIndex: (index: number) => this._getInputElement().selectedIndex = index,
336335
setDisabled: (isDisabled: boolean) => this._getInputElement().disabled = isDisabled,
@@ -398,7 +397,7 @@ export class MdcSelect extends _MdcSelectMixinBase implements AfterContentInit,
398397

399398
private _foundation!: {
400399
setSelectedIndex(index: number): void,
401-
setValue(value: string): void,
400+
setValue(value: any): void,
402401
setDisabled(isDisabled: boolean): void,
403402
notchOutline(openNotch: boolean): void,
404403
handleChange(didChange?: boolean): void,
@@ -407,7 +406,9 @@ export class MdcSelect extends _MdcSelectMixinBase implements AfterContentInit,
407406
handleClick(normalizedX: number): void,
408407
handleKeydown(event: KeyboardEvent): void,
409408
setValid(isValid: boolean): void,
410-
layout(): void
409+
layout(): void,
410+
handleMenuOpened(): void,
411+
handleMenuClosed(): void
411412
};
412413

413414
constructor(
@@ -483,7 +484,7 @@ export class MdcSelect extends _MdcSelectMixinBase implements AfterContentInit,
483484
this._foundation.handleChange(false);
484485
}
485486

486-
writeValue(value: string): void {
487+
writeValue(value: any): void {
487488
this.setSelectionByValue(value, false);
488489
}
489490

@@ -527,7 +528,7 @@ export class MdcSelect extends _MdcSelectMixinBase implements AfterContentInit,
527528
this._foundation.handleKeydown(evt);
528529
}
529530

530-
getValue(): string {
531+
getValue(): any {
531532
return this._value;
532533
}
533534

@@ -543,7 +544,7 @@ export class MdcSelect extends _MdcSelectMixinBase implements AfterContentInit,
543544
* Sets the selected option based on a value. If no option can be
544545
* found with the designated value, the select trigger is cleared.
545546
*/
546-
setSelectionByValue(value: string, isUserInput: boolean = true): void {
547+
setSelectionByValue(value: any, isUserInput: boolean = true): void {
547548
if (!this._foundation) { return; }
548549

549550
this._setEnhancedSelection(value); // if enhanced select, perform selection
@@ -694,6 +695,7 @@ export class MdcSelect extends _MdcSelectMixinBase implements AfterContentInit,
694695
// Subscribe to menu opened event
695696
this._menu.opened.pipe(takeUntil(this._destroyed))
696697
.subscribe(() => {
698+
this._foundation.handleMenuOpened();
697699
const selectedIndex = this._list.getSelectedIndex();
698700
if (selectedIndex > -1) {
699701
this._list.items.toArray()[selectedIndex].focus();
@@ -703,6 +705,7 @@ export class MdcSelect extends _MdcSelectMixinBase implements AfterContentInit,
703705
// Subscribe to menu closed event
704706
this._menu.closed.pipe(takeUntil(this._destroyed))
705707
.subscribe(() => {
708+
this._foundation.handleMenuClosed();
706709
this._selectedText.nativeElement.removeAttribute('aria-expanded');
707710
if (this._platform.isBrowser) {
708711
if (document.activeElement !== this._selectedText.nativeElement) {

0 commit comments

Comments
 (0)