Skip to content

fix(datepicker): reformat valid values on blur #10777

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 1 commit into from
Apr 11, 2018
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
24 changes: 20 additions & 4 deletions src/lib/datepicker/datepicker-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class MatDatepickerInputEvent<D> {
'[disabled]': 'disabled',
'(input)': '_onInput($event.target.value)',
'(change)': '_onChange()',
'(blur)': '_onTouched()',
'(blur)': '_onBlur()',
'(keydown)': '_onKeydown($event)',
},
exportAs: 'matDatepickerInput',
Expand Down Expand Up @@ -123,10 +123,10 @@ export class MatDatepickerInput<D> implements AfterContentInit, ControlValueAcce
value = this._dateAdapter.deserialize(value);
this._lastValueValid = !value || this._dateAdapter.isValid(value);
value = this._getValidDateOrNull(value);
let oldDate = this.value;
const oldDate = this.value;
this._value = value;
this._elementRef.nativeElement.value =
value ? this._dateAdapter.format(value, this._dateFormats.display.dateInput) : '';
this._formatValue(value);

if (!this._dateAdapter.sameDate(oldDate, value)) {
this._valueChange.emit(value);
}
Expand Down Expand Up @@ -343,6 +343,22 @@ export class MatDatepickerInput<D> implements AfterContentInit, ControlValueAcce
return this._formField ? this._formField.color : undefined;
}

/** Handles blur events on the input. */
_onBlur() {
// Reformat the input only if we have a valid value.
if (this.value) {
this._formatValue(this.value);
}

this._onTouched();
}

/** Formats a value and sets it on the input element. */
private _formatValue(value: D | null) {
this._elementRef.nativeElement.value =
value ? this._dateAdapter.format(value, this._dateFormats.display.dateInput) : '';
}

/**
* @param obj The object to check.
* @returns The given object if it is both a date instance and valid, otherwise null.
Expand Down
32 changes: 32 additions & 0 deletions src/lib/datepicker/datepicker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,38 @@ describe('MatDatepicker', () => {
expect(inputEl.classList).toContain('ng-touched');
});

it('should reformat the input value on blur', () => {
if (SUPPORTS_INTL) {
// Skip this test if the internationalization API is not supported in the current
// browser. Browsers like Safari 9 do not support the "Intl" API.
return;
}

const inputEl = fixture.debugElement.query(By.css('input')).nativeElement;

inputEl.value = '2001-01-01';
dispatchFakeEvent(inputEl, 'input');
fixture.detectChanges();

dispatchFakeEvent(inputEl, 'blur');
fixture.detectChanges();

expect(inputEl.value).toBe('1/1/2001');
});

it('should not reformat invalid dates on blur', () => {
const inputEl = fixture.debugElement.query(By.css('input')).nativeElement;

inputEl.value = 'very-valid-date';
dispatchFakeEvent(inputEl, 'input');
fixture.detectChanges();

dispatchFakeEvent(inputEl, 'blur');
fixture.detectChanges();

expect(inputEl.value).toBe('very-valid-date');
});

it('should mark input touched on calendar selection', fakeAsync(() => {
let inputEl = fixture.debugElement.query(By.css('input')).nativeElement;

Expand Down