Skip to content

Commit e0bff78

Browse files
committed
fix(datepicker): placeholder not floating when an invalid value is typed in
Fixes the datepicker input's placeholder not floating if the user types in an invalid value. This was due to the datepicker's value being parsed to `null` which the form field inferred as empty. These changes switch to always check the DOM element, rather than the actual control value. Fixes #8575.
1 parent 4dd8a31 commit e0bff78

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/lib/datepicker/datepicker.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,16 @@ describe('MatDatepicker', () => {
679679
expect(attachToRef.nativeElement.classList.contains('mat-form-field-underline'))
680680
.toBe(true, 'popup should be attached to mat-form-field underline');
681681
});
682+
683+
it('should float the placeholder when an invalid value is entered', () => {
684+
testComponent.datepickerInput.value = 'totally-not-a-date' as any;
685+
fixture.debugElement.nativeElement.querySelector('input').value = 'totally-not-a-date';
686+
fixture.detectChanges();
687+
688+
expect(fixture.debugElement.nativeElement.querySelector('mat-form-field').classList)
689+
.toContain('mat-form-field-should-float');
690+
});
691+
682692
});
683693

684694
describe('datepicker with min and max dates and validation', () => {

src/lib/input/input.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,7 @@ export class MatInput implements MatFormFieldControl<any>, OnChanges, OnDestroy,
284284

285285
// Implemented as part of MatFormFieldControl.
286286
get empty(): boolean {
287-
return !this._isNeverEmpty() &&
288-
(this.value == null || this.value === '') &&
289-
// Check if the input contains bad input. If so, we know that it only appears empty because
290-
// the value failed to parse. From the user's perspective it is not empty.
291-
// TODO(mmalerba): Add e2e test for bad input case.
292-
!this._isBadInput();
287+
return !this._isNeverEmpty() && !this._elementRef.nativeElement.value && !this._isBadInput();
293288
}
294289

295290
// Implemented as part of MatFormFieldControl.

0 commit comments

Comments
 (0)