Skip to content

fix(select): unable to preselect array value in single selection mode #7603

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
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
39 changes: 39 additions & 0 deletions src/lib/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ describe('MatSelect', () => {
SelectInsideFormGroup,
NgModelCompareWithSelect,
CustomErrorBehaviorSelect,
SingleSelectWithPreselectedArrayValues,
],
providers: [
{provide: OverlayContainer, useFactory: () => {
Expand Down Expand Up @@ -1006,6 +1007,19 @@ describe('MatSelect', () => {
});
});
}));

it('should be able to preselect an array value in single-selection mode', fakeAsync(() => {
const fixture = TestBed.createComponent(SingleSelectWithPreselectedArrayValues);
fixture.detectChanges();
tick();
fixture.detectChanges();

const trigger = fixture.debugElement.query(By.css('.mat-select-trigger')).nativeElement;

expect(trigger.textContent).toContain('Pizza');
expect(fixture.componentInstance.options.toArray()[1].selected).toBe(true);
}));

});

describe('misc forms', () => {
Expand Down Expand Up @@ -3814,3 +3828,28 @@ class CustomErrorBehaviorSelect {
];
errorStateMatcher: ErrorStateMatcher;
}


@Component({
template: `
<mat-form-field>
<mat-select placeholder="Food" [(ngModel)]="selectedFoods">
<mat-option *ngFor="let food of foods"
[value]="food.value">{{ food.viewValue }}
</mat-option>
</mat-select>
</mat-form-field>
`
})
class SingleSelectWithPreselectedArrayValues {
foods: any[] = [
{ value: ['steak-0', 'steak-1'], viewValue: 'Steak' },
{ value: ['pizza-1', 'pizza-2'], viewValue: 'Pizza' },
{ value: ['tacos-2', 'tacos-3'], viewValue: 'Tacos' },
];

selectedFoods = this.foods[1].value;

@ViewChild(MatSelect) select: MatSelect;
@ViewChildren(MatOption) options: QueryList<MatOption>;
}
21 changes: 10 additions & 11 deletions src/lib/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -711,18 +711,17 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
* found with the designated value, the select trigger is cleared.
*/
private _setSelectionByValue(value: any | any[], isUserInput = false): void {
const isArray = Array.isArray(value);

if (this.multiple && value && !isArray) {
throw getMatSelectNonArrayValueError();
}

this._clearSelection();
if (this.multiple && value) {
if (!Array.isArray(value)) {
throw getMatSelectNonArrayValueError();
}

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

const correspondingOption = this._selectValue(value, isUserInput);

// Shift focus to the active item. Note that we shouldn't do this in multiple
Expand Down Expand Up @@ -856,10 +855,10 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
private _propagateChanges(fallbackValue?: any): void {
let valueToEmit: any = null;

if (Array.isArray(this.selected)) {
valueToEmit = this.selected.map(option => option.value);
if (this.multiple) {
valueToEmit = (this.selected as MatOption[]).map(option => option.value);
} else {
valueToEmit = this.selected ? this.selected.value : fallbackValue;
valueToEmit = this.selected ? (this.selected as MatOption).value : fallbackValue;
}

this._value = valueToEmit;
Expand Down