Skip to content

Commit

Permalink
fix(datepicker): placeholder not floating when an invalid value is ty…
Browse files Browse the repository at this point in the history
…ped 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 angular#8575.
  • Loading branch information
crisbeto committed Nov 24, 2017
1 parent 4dd8a31 commit e0bff78
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
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

0 comments on commit e0bff78

Please sign in to comment.