Skip to content
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
5 changes: 4 additions & 1 deletion src/material/core/option/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,11 @@ export class _MatOptionBase<T = any> implements FocusableOption, AfterViewChecke
const viewValue = this.viewValue;

if (viewValue !== this._mostRecentViewValue) {
if (this._mostRecentViewValue) {
this._stateChanges.next();
}

this._mostRecentViewValue = viewValue;
this._stateChanges.next();
}
}
}
Expand Down
16 changes: 15 additions & 1 deletion src/material/legacy-select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1831,6 +1831,19 @@ describe('MatSelect', () => {
expect(trigger.textContent!.trim()).toBe('Calzone');
}));

it('should update the trigger value if the text as a result of an expression change', fakeAsync(() => {
fixture.componentInstance.control.setValue('pizza-1');
fixture.detectChanges();

expect(trigger.textContent!.trim()).toBe('Pizza');

fixture.componentInstance.capitalize = true;
fixture.detectChanges();
fixture.checkNoChanges();

expect(trigger.textContent!.trim()).toBe('PIZZA');
}));

it('should not select disabled options', fakeAsync(() => {
trigger.click();
fixture.detectChanges();
Expand Down Expand Up @@ -5202,7 +5215,7 @@ describe('MatSelect', () => {
[panelClass]="panelClass" [disableRipple]="disableRipple"
[typeaheadDebounceInterval]="typeaheadDebounceInterval">
<mat-option *ngFor="let food of foods" [value]="food.value" [disabled]="food.disabled">
{{ food.viewValue }}
{{ capitalize ? food.viewValue.toUpperCase() : food.viewValue }}
</mat-option>
</mat-select>
<mat-hint *ngIf="hint">{{ hint }}</mat-hint>
Expand Down Expand Up @@ -5233,6 +5246,7 @@ class BasicSelect {
panelClass = ['custom-one', 'custom-two'];
disableRipple: boolean;
typeaheadDebounceInterval: number;
capitalize = false;

@ViewChild(MatLegacySelect, {static: true}) select: MatLegacySelect;
@ViewChildren(MatLegacyOption) options: QueryList<MatLegacyOption>;
Expand Down
16 changes: 15 additions & 1 deletion src/material/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1879,6 +1879,19 @@ describe('MDC-based MatSelect', () => {
expect(trigger.textContent!.trim()).toBe('Calzone');
}));

it('should update the trigger value if the text as a result of an expression change', fakeAsync(() => {
fixture.componentInstance.control.setValue('pizza-1');
fixture.detectChanges();

expect(trigger.textContent!.trim()).toBe('Pizza');

fixture.componentInstance.capitalize = true;
fixture.detectChanges();
fixture.checkNoChanges();

expect(trigger.textContent!.trim()).toBe('PIZZA');
}));

it('should not select disabled options', fakeAsync(() => {
trigger.click();
fixture.detectChanges();
Expand Down Expand Up @@ -4410,7 +4423,7 @@ describe('MDC-based MatSelect', () => {
[panelClass]="panelClass" [disableRipple]="disableRipple"
[typeaheadDebounceInterval]="typeaheadDebounceInterval">
<mat-option *ngFor="let food of foods" [value]="food.value" [disabled]="food.disabled">
{{ food.viewValue }}
{{ capitalize ? food.viewValue.toUpperCase() : food.viewValue }}
</mat-option>
</mat-select>
<mat-hint *ngIf="hint">{{ hint }}</mat-hint>
Expand Down Expand Up @@ -4442,6 +4455,7 @@ class BasicSelect {
panelClass = ['custom-one', 'custom-two'];
disableRipple: boolean;
typeaheadDebounceInterval: number;
capitalize = false;

@ViewChild(MatSelect, {static: true}) select: MatSelect;
@ViewChildren(MatOption) options: QueryList<MatOption>;
Expand Down
5 changes: 4 additions & 1 deletion src/material/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,10 @@ export abstract class _MatSelectBase<C>
merge(...this.options.map(option => option._stateChanges))
.pipe(takeUntil(changedOrDestroyed))
.subscribe(() => {
this._changeDetectorRef.markForCheck();
// `_stateChanges` can fire as a result of a change in the label's DOM value which may
// be the result of an expression changing. We have to use `detectChanges` in order
// to avoid "changed after checked" errors (see #14793).
this._changeDetectorRef.detectChanges();
this.stateChanges.next();
});
}
Expand Down