Skip to content

Commit

Permalink
fix(datepicker): dateInput event being fired if the value hasn't chan…
Browse files Browse the repository at this point in the history
…ged (#10952)

Fixes the `dateInput` event being fired even if the value hasn't changed. This can happen if the browser fires the `input` event while the value hasn't changed (e.g. if the user marks a character and "replaces" it with the same character).
  • Loading branch information
crisbeto authored and jelbourn committed May 10, 2018
1 parent fbf5648 commit a62cdb6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/lib/datepicker/datepicker-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,13 @@ export class MatDatepickerInput<D> implements AfterContentInit, ControlValueAcce
let date = this._dateAdapter.parse(value, this._dateFormats.parse.dateInput);
this._lastValueValid = !date || this._dateAdapter.isValid(date);
date = this._getValidDateOrNull(date);
this._value = date;
this._cvaOnChange(date);
this._valueChange.emit(date);
this.dateInput.emit(new MatDatepickerInputEvent(this, this._elementRef.nativeElement));

if (!this._dateAdapter.sameDate(date, this._value)) {
this._value = date;
this._cvaOnChange(date);
this._valueChange.emit(date);
this.dateInput.emit(new MatDatepickerInputEvent(this, this._elementRef.nativeElement));
}
}

_onChange() {
Expand Down
18 changes: 18 additions & 0 deletions src/lib/datepicker/datepicker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,7 @@ describe('MatDatepicker', () => {

expect(inputEl.classList).toContain('ng-pristine');

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

Expand Down Expand Up @@ -1132,6 +1133,7 @@ describe('MatDatepicker', () => {
expect(testComponent.onInput).not.toHaveBeenCalled();
expect(testComponent.onDateInput).not.toHaveBeenCalled();

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

Expand Down Expand Up @@ -1179,6 +1181,22 @@ describe('MatDatepicker', () => {
expect(testComponent.onDateInput).toHaveBeenCalled();
})
);

it('should not fire the dateInput event if the value has not changed', () => {
expect(testComponent.onDateInput).not.toHaveBeenCalled();

inputEl.value = '12/12/2012';
dispatchFakeEvent(inputEl, 'input');
fixture.detectChanges();

expect(testComponent.onDateInput).toHaveBeenCalledTimes(1);

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

expect(testComponent.onDateInput).toHaveBeenCalledTimes(1);
});

});

describe('with ISO 8601 strings as input', () => {
Expand Down

0 comments on commit a62cdb6

Please sign in to comment.