Skip to content

Commit 61029c8

Browse files
crisbetommalerba
authored andcommitted
fix(datepicker): not respecting form control updateOn: blur fo… (#18063)
Fixes the datepicker input not respecting when a `FormControl` is set to `updateOn: 'blur'` when changing between invalid values. The input was emitting for each change like the default. Fixes #16461.
1 parent a30094b commit 61029c8

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/material/datepicker/datepicker-input.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ export class MatDatepickerInput<D> implements ControlValueAccessor, OnDestroy, V
320320
}
321321

322322
_onInput(value: string) {
323+
const lastValueWasValid = this._lastValueValid;
323324
let date = this._dateAdapter.parse(value, this._dateFormats.parse.dateInput);
324325
this._lastValueValid = !date || this._dateAdapter.isValid(date);
325326
date = this._getValidDateOrNull(date);
@@ -329,7 +330,7 @@ export class MatDatepickerInput<D> implements ControlValueAccessor, OnDestroy, V
329330
this._cvaOnChange(date);
330331
this._valueChange.emit(date);
331332
this.dateInput.emit(new MatDatepickerInputEvent(this, this._elementRef.nativeElement));
332-
} else {
333+
} else if (lastValueWasValid !== this._lastValueValid) {
333334
this._validatorOnChange();
334335
}
335336
}

src/material/datepicker/datepicker.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,44 @@ describe('MatDatepicker', () => {
876876

877877
expect(testComponent.datepickerToggle.disabled).toBe(true);
878878
});
879+
880+
it('should not dispatch FormControl change event for invalid values on input when set ' +
881+
'to update on blur', fakeAsync(() => {
882+
const formControl = new FormControl({value: null}, {updateOn: 'blur'});
883+
const spy = jasmine.createSpy('change spy');
884+
const subscription = formControl.valueChanges.subscribe(spy);
885+
const inputEl = fixture.debugElement.query(By.css('input'))!.nativeElement;
886+
const setValue = (value: string) => {
887+
inputEl.value = value;
888+
dispatchFakeEvent(inputEl, 'input');
889+
fixture.detectChanges();
890+
flush();
891+
fixture.detectChanges();
892+
};
893+
894+
fixture.componentInstance.formControl = formControl;
895+
fixture.detectChanges();
896+
897+
expect(spy).not.toHaveBeenCalled();
898+
899+
setValue('10/10/2010');
900+
expect(spy).not.toHaveBeenCalled();
901+
902+
setValue('10/10/');
903+
expect(spy).not.toHaveBeenCalled();
904+
905+
setValue('10/10');
906+
expect(spy).not.toHaveBeenCalled();
907+
908+
dispatchFakeEvent(inputEl, 'blur');
909+
fixture.detectChanges();
910+
flush();
911+
fixture.detectChanges();
912+
913+
expect(spy).toHaveBeenCalledTimes(1);
914+
subscription.unsubscribe();
915+
}));
916+
879917
});
880918

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

0 commit comments

Comments
 (0)