Skip to content

Commit 391a9fd

Browse files
crisbetommalerba
authored andcommittedJan 4, 2019
fix(select): form field state not updated if options are reset (#14720)
Fixes the state of the form field around a `mat-select` not being updated when the amount of options changes, causing the value to be updated. Fixes #14709.
1 parent 7188719 commit 391a9fd

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed
 

‎src/lib/select/select.spec.ts

+23-2
Original file line numberDiff line numberDiff line change
@@ -2453,6 +2453,22 @@ describe('MatSelect', () => {
24532453
expect(component.select.errorState).toBe(true);
24542454
expect(errorStateMatcher.isErrorState).toHaveBeenCalled();
24552455
}));
2456+
2457+
it('should notify that the state changed when the options have changed', fakeAsync(() => {
2458+
testComponent.formControl.setValue('pizza-1');
2459+
fixture.detectChanges();
2460+
2461+
const spy = jasmine.createSpy('stateChanges spy');
2462+
const subscription = testComponent.select.stateChanges.subscribe(spy);
2463+
2464+
testComponent.options = [];
2465+
fixture.detectChanges();
2466+
tick();
2467+
2468+
expect(spy).toHaveBeenCalled();
2469+
subscription.unsubscribe();
2470+
}));
2471+
24562472
});
24572473

24582474
describe('with custom error behavior', () => {
@@ -4640,8 +4656,9 @@ class InvalidSelectInForm {
46404656
<form [formGroup]="formGroup">
46414657
<mat-form-field>
46424658
<mat-select placeholder="Food" formControlName="food">
4643-
<mat-option value="steak-0">Steak</mat-option>
4644-
<mat-option value="pizza-1">Pizza</mat-option>
4659+
<mat-option *ngFor="let option of options" [value]="option.value">
4660+
{{option.viewValue}}
4661+
</mat-option>
46454662
</mat-select>
46464663
46474664
<mat-error>This field is required</mat-error>
@@ -4652,6 +4669,10 @@ class InvalidSelectInForm {
46524669
class SelectInsideFormGroup {
46534670
@ViewChild(FormGroupDirective) formGroupDirective: FormGroupDirective;
46544671
@ViewChild(MatSelect) select: MatSelect;
4672+
options = [
4673+
{value: 'steak-0', viewValue: 'Steak'},
4674+
{value: 'pizza-1', viewValue: 'Pizza'},
4675+
];
46554676
formControl = new FormControl('', Validators.required);
46564677
formGroup = new FormGroup({
46574678
food: this.formControl

‎src/lib/select/select.ts

+1
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,7 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
819819
// has changed after it was checked" errors from Angular.
820820
Promise.resolve().then(() => {
821821
this._setSelectionByValue(this.ngControl ? this.ngControl.value : this._value);
822+
this.stateChanges.next();
822823
});
823824
}
824825

0 commit comments

Comments
 (0)
Please sign in to comment.