Skip to content

Commit

Permalink
fix(module:datepicker): send OnChange event for same value (#7815)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicoss54 authored Feb 4, 2023
1 parent 445d183 commit 3602abc
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
19 changes: 19 additions & 0 deletions components/date-picker/date-picker.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,25 @@ describe('NzDatePickerComponent', () => {
expect(isSameDay(new Date('2021-04-12'), result)).toBeTruthy();
expect(getPickerContainer()).toBeNull();
}));
it("should not send onChangeEvent if value doesn't change", fakeAsync(() => {
const nzOnChange = spyOn(fixtureInstance, 'nzOnChange');
fixtureInstance.useSuite = 5;
fixtureInstance.firstValue = new Date('2021-04-12');
fixture.detectChanges();

openPickerByClickTrigger();
expect(getPickerContainer()).not.toBeNull();
typeInElement('2021-04-12', getPickerInput(fixture.debugElement));
fixture.detectChanges();

triggerInputBlur();
fixture.detectChanges();
tick(500);
fixture.detectChanges();

expect(nzOnChange).not.toHaveBeenCalled();
expect(getPickerContainer()).toBeNull();
}));

it('should support changing language at runtime', fakeAsync(() => {
fixture.detectChanges();
Expand Down
21 changes: 21 additions & 0 deletions components/date-picker/date-picker.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,27 @@ export class NzDatePickerComponent implements OnInit, OnChanges, OnDestroy, Afte
this.datePickerService.initValue(true);
this.datePickerService.emitValue$.pipe(takeUntil(this.destroyed$)).subscribe(() => {
const value = this.datePickerService.value;
const datePickerPreviousValue = this.datePickerService.initialValue;

// Check if the value has change for a simple datepicker, let us to avoid notify the control for nothing
if (!this.isRange && (value as CandyDate)?.isSame((datePickerPreviousValue as CandyDate)?.nativeDate)) {
this.onTouchedFn();
return this.close();
}

// check if the value has change for a simple datepicker, let us to avoid notify the control for nothing
if (this.isRange) {
const [previousStartDate, previousEndDate] = datePickerPreviousValue as CandyDate[];
const [currentStartDate, currentEndDate] = value as CandyDate[];
if (
previousStartDate?.isSame(currentStartDate?.nativeDate) &&
previousEndDate?.isSame(currentEndDate?.nativeDate)
) {
this.onTouchedFn();
return this.close();
}
}

this.datePickerService.initialValue = cloneDate(value);
if (this.isRange) {
const vAsRange = value as CandyDate[];
Expand Down
23 changes: 23 additions & 0 deletions components/date-picker/range-picker.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,29 @@ describe('NzRangePickerComponent', () => {
const result = (nzOnChange.calls.allArgs()[0] as Date[][])[0];
expect((result[0] as Date).getDate()).toBe(+leftText);
}));
it('should not call nzOnChange if values do not change', fakeAsync(() => {
fixtureInstance.modelValue = [new Date('2018-11-11'), new Date('2018-11-11')];
const nzOnChange = spyOn(fixtureInstance, 'modelValueChange');
fixture.detectChanges();
flush();
fixture.detectChanges();
openPickerByClickTrigger();
const leftInput = getPickerInput(fixture.debugElement);
const rightInput = getRangePickerRightInput(fixture.debugElement);
typeInElement('2018-11-11 00:00:00', leftInput);
fixture.detectChanges();
flush();
fixture.detectChanges();
typeInElement('2018-11-11 00:00:00', rightInput);
fixture.detectChanges();
flush();
fixture.detectChanges();
triggerInputBlur();
fixture.detectChanges();
flush();
fixture.detectChanges();
expect(nzOnChange).not.toHaveBeenCalled();
}));

it('should support nzInline', fakeAsync(() => {
const nzOnChange = spyOn(fixtureInstance, 'modelValueChange');
Expand Down

0 comments on commit 3602abc

Please sign in to comment.