Skip to content

fix(input): floating label not reacting when patching the value without emitting an event #9260

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 1 commit into from
Jan 23, 2018
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
20 changes: 19 additions & 1 deletion src/lib/input/input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,21 @@ describe('MatInput with forms', () => {
expect(el).not.toBeNull();
expect(el.classList.contains('mat-form-field-empty')).toBe(false);
}));

it('should update when the form field value is patched without emitting', fakeAsync(() => {
const fixture = TestBed.createComponent(MatInputWithFormControl);
fixture.detectChanges();

const el = fixture.debugElement.query(By.css('label')).nativeElement;

expect(el.classList).toContain('mat-form-field-empty');

fixture.componentInstance.formControl.patchValue('value', {emitEvent: false});
fixture.detectChanges();

expect(el.classList).not.toContain('mat-form-field-empty');
}));

});

@Component({
Expand Down Expand Up @@ -1170,7 +1185,10 @@ class MatInputPlaceholderElementTestComponent {
}

@Component({
template: `<mat-form-field><input matInput [formControl]="formControl"></mat-form-field>`
template: `
<mat-form-field>
<input matInput placeholder="Hello" [formControl]="formControl">
</mat-form-field>`
})
class MatInputWithFormControl {
formControl = new FormControl();
Expand Down
9 changes: 5 additions & 4 deletions src/lib/input/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,12 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl<
// error triggers that we can't subscribe to (e.g. parent form submissions). This means
// that whatever logic is in here has to be super lean or we risk destroying the performance.
this.updateErrorState();
} else {
// When the input isn't used together with `@angular/forms`, we need to check manually for
// changes to the native `value` property in order to update the floating label.
this._dirtyCheckNativeValue();
}

// We need to dirty-check the native element's value, because there are some cases where
// we won't be notified when it changes (e.g. the consumer isn't using forms or they're
// updating the value using `emitEvent: false`).
this._dirtyCheckNativeValue();
}

focus() { this._elementRef.nativeElement.focus(); }
Expand Down