MCPcopy Create free account
hub / github.com/angular/components / _setSelectionByValue

Method _setSelectionByValue

src/material/select/select.ts:1038–1064  ·  view source on GitHub ↗

* Sets the selected option based on a value. If no option can be * found with the designated value, the select trigger is cleared.

(value: any | any[])

Source from the content-addressed store, hash-verified

1036 * found with the designated value, the select trigger is cleared.
1037 */
1038 private _setSelectionByValue(value: any | any[]): void {
1039 this.options.forEach(option => option.setInactiveStyles());
1040 this._selectionModel.clear();
1041
1042 if (this.multiple && value) {
1043 if (!Array.isArray(value) && (typeof ngDevMode === 'undefined' || ngDevMode)) {
1044 throw getMatSelectNonArrayValueError();
1045 }
1046
1047 value.forEach((currentValue: any) => this._selectOptionByValue(currentValue));
1048 this._sortValues();
1049 } else {
1050 const correspondingOption = this._selectOptionByValue(value);
1051
1052 // Shift focus to the active item. Note that we shouldn't do this in multiple
1053 // mode, because we don't know what option the user interacted with last.
1054 if (correspondingOption) {
1055 this._keyManager.updateActiveItem(correspondingOption);
1056 } else if (!this.panelOpen) {
1057 // Otherwise reset the highlighted option. Note that we only want to do this while
1058 // closed, because doing it while open can shift the user's focus unnecessarily.
1059 this._keyManager.updateActiveItem(-1);
1060 }
1061 }
1062
1063 this._changeDetectorRef.markForCheck();
1064 }
1065
1066 /**
1067 * Finds and selects and option based on its value.

Callers 2

_initializeSelectionMethod · 0.95
_assignValueMethod · 0.95

Calls 7

_selectOptionByValueMethod · 0.95
_sortValuesMethod · 0.95
updateActiveItemMethod · 0.80
markForCheckMethod · 0.80
setInactiveStylesMethod · 0.65
clearMethod · 0.65

Tested by

no test coverage detected