Skip to content

fix(form-field): not updating outline when prefix/suffix is added or removed #13253

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
45 changes: 28 additions & 17 deletions src/lib/form-field/form-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
MAT_LABEL_GLOBAL_OPTIONS,
mixinColor,
} from '@angular/material/core';
import {EMPTY, fromEvent, merge} from 'rxjs';
import {fromEvent, merge} from 'rxjs';
import {startWith, take} from 'rxjs/operators';
import {MatError} from './error';
import {matFormFieldAnimations} from './form-field-animations';
Expand Down Expand Up @@ -153,14 +153,7 @@ export class MatFormField extends _MatFormFieldMixinBase
this._appearance = value || (this._defaults && this._defaults.appearance) || 'legacy';

if (this._appearance === 'outline' && oldValue !== value) {
// @breaking-change 7.0.0 Remove this check and else block once _ngZone is required.
if (this._ngZone) {
this._ngZone!.onStable.pipe(take(1)).subscribe(() => {
this._ngZone!.runOutsideAngular(() => this.updateOutlineGap());
});
} else {
Promise.resolve().then(() => this.updateOutlineGap());
}
this._updateOutlineGapOnStable();
}
}
_appearance: MatFormFieldAppearance;
Expand Down Expand Up @@ -273,22 +266,30 @@ export class MatFormField extends _MatFormFieldMixinBase

ngAfterContentInit() {
this._validateControlChild();
if (this._control.controlType) {
this._elementRef.nativeElement.classList
.add(`mat-form-field-type-${this._control.controlType}`);

const control = this._control;

if (control.controlType) {
this._elementRef.nativeElement.classList.add(`mat-form-field-type-${control.controlType}`);
}

// Subscribe to changes in the child control state in order to update the form field UI.
this._control.stateChanges.pipe(startWith<void>(null!)).subscribe(() => {
control.stateChanges.pipe(startWith<void>(null!)).subscribe(() => {
this._validatePlaceholders();
this._syncDescribedByIds();
this._changeDetectorRef.markForCheck();
});

// Run change detection if the value, prefix, or suffix changes.
const valueChanges = this._control.ngControl && this._control.ngControl.valueChanges || EMPTY;
merge(valueChanges, this._prefixChildren.changes, this._suffixChildren.changes)
.subscribe(() => this._changeDetectorRef.markForCheck());
// Run change detection if the value changes.
if (control.ngControl && control.ngControl.valueChanges) {
control.ngControl.valueChanges.subscribe(() => this._changeDetectorRef.markForCheck());
}

// Run change detection and update the outline if the suffix or prefix changes.
merge(this._prefixChildren.changes, this._suffixChildren.changes).subscribe(() => {
this._updateOutlineGapOnStable();
this._changeDetectorRef.markForCheck();
});

// Re-validate when the number of hints changes.
this._hintChildren.changes.pipe(startWith(null)).subscribe(() => {
Expand Down Expand Up @@ -503,4 +504,14 @@ export class MatFormField extends _MatFormFieldMixinBase
private _getStartEnd(rect: ClientRect): number {
return this._dir && this._dir.value === 'rtl' ? rect.right : rect.left;
}

/** Updates the outline gap the new time the zone stabilizes. */
private _updateOutlineGapOnStable() {
// @breaking-change 7.0.0 Remove this check and else block once _ngZone is required.
if (this._ngZone) {
this._ngZone.onStable.pipe(take(1)).subscribe(() => this.updateOutlineGap());
} else {
Promise.resolve().then(() => this.updateOutlineGap());
}
}
}
25 changes: 25 additions & 0 deletions src/lib/input/input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,28 @@ describe('MatInput with appearance', () => {
expect(parseInt(outlineGap.style.width)).toBeGreaterThan(0);
}));

it('should update the outline gap when the prefix/suffix is added or removed', fakeAsync(() => {
fixture.destroy();
TestBed.resetTestingModule();

const outlineFixture = createComponent(MatInputWithAppearanceAndLabel);

outlineFixture.componentInstance.appearance = 'outline';
outlineFixture.detectChanges();
flush();
outlineFixture.detectChanges();

spyOn(outlineFixture.componentInstance.formField, 'updateOutlineGap');

outlineFixture.componentInstance.showPrefix = true;
outlineFixture.detectChanges();
flush();
outlineFixture.detectChanges();

expect(outlineFixture.componentInstance.formField.updateOutlineGap).toHaveBeenCalled();
}));


});

describe('MatFormField default options', () => {
Expand Down Expand Up @@ -1735,13 +1757,16 @@ class MatInputWithAppearance {
@Component({
template: `
<mat-form-field [appearance]="appearance">
<span matPrefix *ngIf="showPrefix">Somewhat long prefix</span>
<mat-label>{{labelContent}}</mat-label>
<input matInput>
</mat-form-field>
`
})
class MatInputWithAppearanceAndLabel {
@ViewChild(MatFormField) formField: MatFormField;
appearance: MatFormFieldAppearance;
showPrefix: boolean;
labelContent = 'Label';
}

Expand Down