Skip to content

Commit

Permalink
fix(form-field): error when trying to lock label into position too ea…
Browse files Browse the repository at this point in the history
…rly (#18666)

Fixes an error that is thrown by the form field's `_animateAndLockLabel` if it's called before `ngAfterViewInit`. This can happen if an autocomplete receives focus too early.

Fixes #18663.
  • Loading branch information
crisbeto authored Mar 12, 2020
1 parent dd239dd commit eb92de8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/material/form-field/form-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ export class MatFormField extends _MatFormFieldMixinBase
if (this._hasFloatingLabel() && this._canLabelFloat) {
// If animations are disabled, we shouldn't go in here,
// because the `transitionend` will never fire.
if (this._animationsEnabled) {
if (this._animationsEnabled && this._label) {
this._showAlwaysAnimate = true;

fromEvent(this._label.nativeElement, 'transitionend').pipe(take(1)).subscribe(() => {
Expand Down
7 changes: 7 additions & 0 deletions src/material/input/input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,13 @@ describe('MatInput without forms', () => {
expect(inputContainer.floatLabel).toBe('always');
}));

it('should not throw when trying to animate and lock too early', fakeAsync(() => {
const fixture = createComponent(MatInputTextTestController);
const formField = fixture.debugElement.query(By.directive(MatFormField))!
.componentInstance as MatFormField;
expect(() => formField._animateAndLockLabel()).not.toThrow();
}));

it('should not highlight when focusing a readonly input', fakeAsync(() => {
let fixture = createComponent(MatInputWithReadonlyInput);
fixture.detectChanges();
Expand Down

0 comments on commit eb92de8

Please sign in to comment.