Skip to content

fix(datepicker): placeholder not floating when an invalid value is typed in #8603

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
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
10 changes: 10 additions & 0 deletions src/lib/datepicker/datepicker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,16 @@ describe('MatDatepicker', () => {
expect(attachToRef.nativeElement.classList.contains('mat-form-field-underline'))
.toBe(true, 'popup should be attached to mat-form-field underline');
});

it('should float the placeholder when an invalid value is entered', () => {
testComponent.datepickerInput.value = 'totally-not-a-date' as any;
fixture.debugElement.nativeElement.querySelector('input').value = 'totally-not-a-date';
fixture.detectChanges();

expect(fixture.debugElement.nativeElement.querySelector('mat-form-field').classList)
.toContain('mat-form-field-should-float');
});

});

describe('datepicker with min and max dates and validation', () => {
Expand Down
7 changes: 1 addition & 6 deletions src/lib/input/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,12 +284,7 @@ export class MatInput implements MatFormFieldControl<any>, OnChanges, OnDestroy,

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

// Implemented as part of MatFormFieldControl.
Expand Down