Skip to content

Commit f0789eb

Browse files
crisbetotinayuangao
authored andcommitted
fix(datepicker): placeholder not floating when an invalid value is typed in (#8603)
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 61dada8 commit f0789eb

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
@@ -687,6 +687,16 @@ describe('MatDatepicker', () => {
687687
expect(attachToRef.nativeElement.classList.contains('mat-form-field-underline'))
688688
.toBe(true, 'popup should be attached to mat-form-field underline');
689689
});
690+
691+
it('should float the placeholder when an invalid value is entered', () => {
692+
testComponent.datepickerInput.value = 'totally-not-a-date' as any;
693+
fixture.debugElement.nativeElement.querySelector('input').value = 'totally-not-a-date';
694+
fixture.detectChanges();
695+
696+
expect(fixture.debugElement.nativeElement.querySelector('mat-form-field').classList)
697+
.toContain('mat-form-field-should-float');
698+
});
699+
690700
});
691701

692702
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
@@ -294,12 +294,7 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl<
294294

295295
// Implemented as part of MatFormFieldControl.
296296
get empty(): boolean {
297-
return !this._isNeverEmpty() &&
298-
(this.value == null || this.value === '') &&
299-
// Check if the input contains bad input. If so, we know that it only appears empty because
300-
// the value failed to parse. From the user's perspective it is not empty.
301-
// TODO(mmalerba): Add e2e test for bad input case.
302-
!this._isBadInput();
297+
return !this._isNeverEmpty() && !this._elementRef.nativeElement.value && !this._isBadInput();
303298
}
304299

305300
// Implemented as part of MatFormFieldControl.

0 commit comments

Comments
 (0)