Skip to content

Commit dd3987a

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 dd3987a

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
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 & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,6 @@ export class MatInput implements MatFormFieldControl<any>, OnChanges, OnDestroy,
264264
return this._neverEmptyInputTypes.indexOf(this._type) > -1;
265265
}
266266

267-
/** Checks whether the input is invalid based on the native validation. */
268-
protected _isBadInput() {
269-
// The `validity` property won't be present on platform-server.
270-
let validity = (this._elementRef.nativeElement as HTMLInputElement).validity;
271-
return validity && validity.badInput;
272-
}
273-
274267
/** Determines if the component host is a textarea. If not recognizable it returns false. */
275268
protected _isTextarea() {
276269
let nativeElement = this._elementRef.nativeElement;
@@ -284,12 +277,7 @@ export class MatInput implements MatFormFieldControl<any>, OnChanges, OnDestroy,
284277

285278
// Implemented as part of MatFormFieldControl.
286279
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();
280+
return !this._isNeverEmpty() && !this._elementRef.nativeElement.value;
293281
}
294282

295283
// Implemented as part of MatFormFieldControl.

0 commit comments

Comments
 (0)