Skip to content

Commit

Permalink
fix(material/select): value not updated if the same array is updated …
Browse files Browse the repository at this point in the history
…and re-assigned (#21625)

`mat-select` has an internal check that doesn't re-assign a value if the reference is the same.
This was meant primarily for primitives, but it ends up breaking the behavior for a
multi-select where the value is always an array.

These changes skip the check for a multi-select receiving a new array value.

Fixes #21583.

(cherry picked from commit cfbbbfe)
  • Loading branch information
crisbeto authored and mmalerba committed Feb 10, 2021
1 parent 14b5c3f commit 0a002e5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/material-experimental/mdc-select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3768,6 +3768,30 @@ describe('MDC-based MatSelect', () => {
.toEqual([true, true, true, true, false, false]);
}));

it('should update the option selected state if the same array is mutated and passed back in',
fakeAsync(() => {
const value: string[] = [];
trigger.click();
testInstance.control.setValue(value);
fixture.detectChanges();

const optionNodes =
Array.from<HTMLElement>(overlayContainerElement.querySelectorAll('mat-option'));
const optionInstances = testInstance.options.toArray();

expect(optionNodes.some(option => {
return option.classList.contains('mdc-list-item--selected');
})).toBe(false);
expect(optionInstances.some(option => option.selected)).toBe(false);

value.push('eggs-5');
testInstance.control.setValue(value);
fixture.detectChanges();

expect(optionNodes[5].classList).toContain('mdc-list-item--selected');
expect(optionInstances[5].selected).toBe(true);
}));

});

it('should be able to provide default values through an injection token', fakeAsync(() => {
Expand Down
22 changes: 22 additions & 0 deletions src/material/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4666,6 +4666,28 @@ describe('MatSelect', () => {
.toEqual([true, true, true, true, false, false]);
}));

it('should update the option selected state if the same array is mutated and passed back in',
fakeAsync(() => {
const value: string[] = [];
trigger.click();
testInstance.control.setValue(value);
fixture.detectChanges();

const optionNodes =
Array.from<HTMLElement>(overlayContainerElement.querySelectorAll('mat-option'));
const optionInstances = testInstance.options.toArray();

expect(optionNodes.some(option => option.classList.contains('mat-selected'))).toBe(false);
expect(optionInstances.some(option => option.selected)).toBe(false);

value.push('eggs-5');
testInstance.control.setValue(value);
fixture.detectChanges();

expect(optionNodes[5].classList).toContain('mat-selected');
expect(optionInstances[5].selected).toBe(true);
}));

});

it('should be able to provide default values through an injection token', fakeAsync(() => {
Expand Down
3 changes: 2 additions & 1 deletion src/material/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,8 @@ export abstract class _MatSelectBase<C> extends _MatSelectMixinBase implements A
@Input()
get value(): any { return this._value; }
set value(newValue: any) {
if (newValue !== this._value) {
// Always re-assign an array, because it might have been mutated.
if (newValue !== this._value || (this._multiple && Array.isArray(newValue))) {
if (this.options) {
this._setSelectionByValue(newValue);
}
Expand Down

0 comments on commit 0a002e5

Please sign in to comment.