Skip to content

select: fix maximum call stack size error and improve performance in multiple mode #17062

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

Closed
wants to merge 4 commits into from
Closed
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: 38 additions & 1 deletion src/material/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3905,7 +3905,10 @@ describe('MatSelect', () => {
});

describe('with multiple selection', () => {
beforeEach(async(() => configureMatSelectTestingModule([MultiSelect])));
beforeEach(async(() => configureMatSelectTestingModule([
MultiSelect,
MultiSelectWithLotsOfOptions
])));

let fixture: ComponentFixture<MultiSelect>;
let testInstance: MultiSelect;
Expand Down Expand Up @@ -4286,6 +4289,18 @@ describe('MatSelect', () => {
expect(testInstance.control.value).toEqual([]);
});

it('should not throw when selecting a large amount of options', fakeAsync(() => {
fixture.destroy();

const lotsOfOptionsFixture = TestBed.createComponent(MultiSelectWithLotsOfOptions);

expect(() => {
lotsOfOptionsFixture.componentInstance.checkAll();
lotsOfOptionsFixture.detectChanges();
flush();
}).not.toThrow();
}));

});
});

Expand Down Expand Up @@ -5062,3 +5077,25 @@ class SelectWithFormFieldLabel {
class SelectWithNgIfAndLabel {
showSelect = true;
}

@Component({
template: `
<mat-form-field>
<mat-select multiple [ngModel]="value">
<mat-option *ngFor="let item of items" [value]="item">{{item}}</mat-option>
</mat-select>
</mat-form-field>
`
})
class MultiSelectWithLotsOfOptions {
items = new Array(1000).fill(0).map((_, i) => i);
value: number[] = [];

checkAll() {
this.value = [...this.items];
}

uncheckAll() {
this.value = [];
}
}
51 changes: 36 additions & 15 deletions src/material/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -847,16 +847,40 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
throw getMatSelectNonArrayValueError();
}

this._selectionModel.clear();
value.forEach((currentValue: any) => this._selectValue(currentValue));
const optionsToSelect: MatOption[] = [];
const optionsToStay: MatOption[] = [];

value.forEach(currentValue => {
const correspondingOption = this._findOption(currentValue);

if (correspondingOption) {
this._selectionModel.isSelected(correspondingOption)
? optionsToStay.push(correspondingOption)
: optionsToSelect.push(correspondingOption);
}
});

const optionsToDeselect = this._selectionModel.selected.filter(opt =>
optionsToStay.indexOf(opt) === -1);

if (optionsToSelect.length > 0) {
this._selectionModel.select(...optionsToSelect);
}

if (optionsToDeselect.length > 0) {
this._selectionModel.deselect(...optionsToDeselect);
}

this._sortValues();
} else {
this._selectionModel.clear();
const correspondingOption = this._selectValue(value);
const correspondingOption = this._findOption(value);

// Shift focus to the active item. Note that we shouldn't do this in multiple
// mode, because we don't know what option the user interacted with last.
if (correspondingOption) {
this._selectionModel.select(correspondingOption);

// Shift focus to the active item. Note that we shouldn't do this in multiple
// mode, because we don't know what option the user interacted with last.
this._keyManager.setActiveItem(correspondingOption);
}
}
Expand All @@ -865,11 +889,11 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
}

/**
* Finds and selects and option based on its value.
* Finds an option based on its value.
* @returns Option that has the corresponding value.
*/
private _selectValue(value: any): MatOption | undefined {
const correspondingOption = this.options.find((option: MatOption) => {
private _findOption(value: any): MatOption | undefined {
return this.options.find((option: MatOption) => {
try {
// Treat null as a special reset value.
return option.value != null && this._compareWith(option.value, value);
Expand All @@ -881,12 +905,6 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
return false;
}
});

if (correspondingOption) {
this._selectionModel.select(correspondingOption);
}

return correspondingOption;
}

/** Sets up a key manager to listen to keyboard events on the overlay panel. */
Expand Down Expand Up @@ -947,7 +965,10 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
this._selectionModel.clear();
this._propagateChanges(option.value);
} else {
option.selected ? this._selectionModel.select(option) : this._selectionModel.deselect(option);
if (wasSelected !== option.selected) {
option.selected ? this._selectionModel.select(option) :
this._selectionModel.deselect(option);
}

if (isUserInput) {
this._keyManager.setActiveItem(option);
Expand Down