Skip to content
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

fix(datepicker): not respecting form control updateOn: blur for invalid values #18063

Merged
merged 1 commit into from
Dec 29, 2019
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
3 changes: 2 additions & 1 deletion src/material/datepicker/datepicker-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ export class MatDatepickerInput<D> implements ControlValueAccessor, OnDestroy, V
}

_onInput(value: string) {
const lastValueWasValid = this._lastValueValid;
let date = this._dateAdapter.parse(value, this._dateFormats.parse.dateInput);
this._lastValueValid = !date || this._dateAdapter.isValid(date);
date = this._getValidDateOrNull(date);
Expand All @@ -329,7 +330,7 @@ export class MatDatepickerInput<D> implements ControlValueAccessor, OnDestroy, V
this._cvaOnChange(date);
this._valueChange.emit(date);
this.dateInput.emit(new MatDatepickerInputEvent(this, this._elementRef.nativeElement));
} else {
} else if (lastValueWasValid !== this._lastValueValid) {
this._validatorOnChange();
}
}
Expand Down
38 changes: 38 additions & 0 deletions src/material/datepicker/datepicker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,44 @@ describe('MatDatepicker', () => {

expect(testComponent.datepickerToggle.disabled).toBe(true);
});

it('should not dispatch FormControl change event for invalid values on input when set ' +
'to update on blur', fakeAsync(() => {
const formControl = new FormControl({value: null}, {updateOn: 'blur'});
const spy = jasmine.createSpy('change spy');
const subscription = formControl.valueChanges.subscribe(spy);
const inputEl = fixture.debugElement.query(By.css('input'))!.nativeElement;
const setValue = (value: string) => {
inputEl.value = value;
dispatchFakeEvent(inputEl, 'input');
fixture.detectChanges();
flush();
fixture.detectChanges();
};

fixture.componentInstance.formControl = formControl;
fixture.detectChanges();

expect(spy).not.toHaveBeenCalled();

setValue('10/10/2010');
expect(spy).not.toHaveBeenCalled();

setValue('10/10/');
expect(spy).not.toHaveBeenCalled();

setValue('10/10');
expect(spy).not.toHaveBeenCalled();

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

expect(spy).toHaveBeenCalledTimes(1);
subscription.unsubscribe();
}));

});

describe('datepicker with mat-datepicker-toggle', () => {
Expand Down