From c9426f004de26dcc5f0c2139a4730afd05288adc Mon Sep 17 00:00:00 2001 From: Wenqi Chen <1264578441@qq.com> Date: Fri, 18 Sep 2020 17:44:57 +0800 Subject: [PATCH] fix(module:date-picker): nzCalendarChange not work when clicking ok (#5790) close #5782 --- .../date-picker/date-picker.component.ts | 6 ++---- .../date-picker/date-range-popup.component.ts | 19 +++++++------------ components/date-picker/demo/time.ts | 5 +++++ .../range-picker.component.spec.ts | 15 +++++++++++++++ 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/components/date-picker/date-picker.component.ts b/components/date-picker/date-picker.component.ts index ab3ab8c0faa..baf46487802 100644 --- a/components/date-picker/date-picker.component.ts +++ b/components/date-picker/date-picker.component.ts @@ -201,7 +201,7 @@ export class NzDatePickerComponent implements OnInit, OnChanges, OnDestroy, Cont if (this.isRange) { const vAsRange = value as CandyDate[]; if (vAsRange.length) { - this.onChangeFn([vAsRange[0].nativeDate, vAsRange[1].nativeDate]); + this.onChangeFn([vAsRange[0]?.nativeDate ?? null, vAsRange[1]?.nativeDate ?? null]); } else { this.onChangeFn([]); } @@ -361,12 +361,11 @@ export class NzDatePickerComponent implements OnInit, OnChanges, OnDestroy, Cont } } - // Emitted when done with date selecting onResultOk(): void { if (this.isRange) { const value = this.datePickerService.value as CandyDate[]; if (value.length) { - this.nzOnOk.emit([value[0].nativeDate, value[1].nativeDate]); + this.nzOnOk.emit([value[0]?.nativeDate || null, value[1]?.nativeDate || null]); } else { this.nzOnOk.emit([]); } @@ -377,6 +376,5 @@ export class NzDatePickerComponent implements OnInit, OnChanges, OnDestroy, Cont this.nzOnOk.emit(null); } } - this.datePickerService.emitValue$.next(); } } diff --git a/components/date-picker/date-range-popup.component.ts b/components/date-picker/date-range-popup.component.ts index 477c411c93d..3bc5079a9ed 100644 --- a/components/date-picker/date-range-popup.component.ts +++ b/components/date-picker/date-range-popup.component.ts @@ -199,17 +199,12 @@ export class DateRangePopupComponent implements OnInit, OnChanges, OnDestroy { } onClickOk(): void { - const otherPart = this.datePickerService.activeInput === 'left' ? 'right' : 'left'; - const selectedValue = this.datePickerService.value; - if (this.isAllowed(selectedValue, true)) { - this.resultOk.emit(); - } else { - if (this.isRange && this.isOneAllowed(selectedValue as CandyDate[])) { - this.datePickerService.inputPartChange$.next(otherPart); - } else { - this.datePickerService.inputPartChange$.next(); - } - } + const inputIndex = { left: 0, right: 1 }[this.datePickerService.activeInput]; + const value: CandyDate = this.isRange + ? (this.datePickerService.value as CandyDate[])[inputIndex] + : (this.datePickerService.value as CandyDate); + this.changeValueFromSelect(value); + this.resultOk.emit(); } onClickToday(value: CandyDate): void { @@ -384,7 +379,7 @@ export class DateRangePopupComponent implements OnInit, OnChanges, OnDestroy { const value = typeof val === 'function' ? val() : val; if (value) { this.datePickerService.setValue([new CandyDate(value[0]), new CandyDate(value[1])]); - this.resultOk.emit(); + this.datePickerService.emitValue$.next(); } } diff --git a/components/date-picker/demo/time.ts b/components/date-picker/demo/time.ts index 1eec16eb257..705c6dbf892 100644 --- a/components/date-picker/demo/time.ts +++ b/components/date-picker/demo/time.ts @@ -18,6 +18,7 @@ import { Component } from '@angular/core'; [nzPlaceHolder]="['Start Time', 'End Time']" ngModel (ngModelChange)="onChange($event)" + (nzOnCalendarChange)="onCalendarChange($event)" (nzOnOk)="onOk($event)" > `, @@ -40,4 +41,8 @@ export class NzDemoDatePickerTimeComponent { onOk(result: Date | Date[] | null): void { console.log('onOk', result); } + + onCalendarChange(result: Array): void { + console.log('onCalendarChange', result); + } } diff --git a/components/date-picker/range-picker.component.spec.ts b/components/date-picker/range-picker.component.spec.ts index 8650e2f25cc..e4ca52d54a9 100644 --- a/components/date-picker/range-picker.component.spec.ts +++ b/components/date-picker/range-picker.component.spec.ts @@ -267,6 +267,21 @@ describe('NzRangePickerComponent', () => { expect((result[1] as Date).getDate()).toBe(+rightText); })); + it('should support nzOnCalendarChange when nzShowTime is true', fakeAsync(() => { + const nzOnCalendarChange = spyOn(fixtureInstance, 'nzOnCalendarChange'); + fixtureInstance.nzShowTime = true; + fixture.detectChanges(); + openPickerByClickTrigger(); + const left = getFirstCell('left'); + dispatchMouseEvent(left, 'click'); + fixture.detectChanges(); + dispatchMouseEvent(queryFromOverlay('.ant-picker-ok > button'), 'click'); + fixture.detectChanges(); + tick(500); + fixture.detectChanges(); + expect(nzOnCalendarChange).toHaveBeenCalled(); + })); + it('should support nzOnChange', fakeAsync(() => { fixtureInstance.modelValue = [new Date('2018-11-11'), new Date('2018-11-11')]; const nzOnChange = spyOn(fixtureInstance, 'modelValueChange');