@@ -26,8 +26,8 @@ import {
26
26
Host ,
27
27
Inject ,
28
28
Input ,
29
+ NgZone ,
29
30
OnChanges ,
30
- OnDestroy ,
31
31
OnInit ,
32
32
Optional ,
33
33
Output ,
@@ -40,7 +40,7 @@ import {
40
40
ViewEncapsulation
41
41
} from '@angular/core' ;
42
42
import { ControlValueAccessor , NG_VALUE_ACCESSOR } from '@angular/forms' ;
43
- import { of as observableOf , Subject } from 'rxjs' ;
43
+ import { fromEvent , of as observableOf } from 'rxjs' ;
44
44
import { distinctUntilChanged , map , takeUntil , withLatestFrom } from 'rxjs/operators' ;
45
45
46
46
import { NzResizeObserver } from 'ng-zorro-antd/cdk/resize-observer' ;
@@ -49,6 +49,7 @@ import { NzConfigKey, NzConfigService, WithConfig } from 'ng-zorro-antd/core/con
49
49
import { NzFormNoStatusService , NzFormStatusService } from 'ng-zorro-antd/core/form' ;
50
50
import { NzNoAnimationDirective } from 'ng-zorro-antd/core/no-animation' ;
51
51
import { DATE_PICKER_POSITION_MAP , DEFAULT_DATE_PICKER_POSITIONS } from 'ng-zorro-antd/core/overlay' ;
52
+ import { NzDestroyService } from 'ng-zorro-antd/core/services' ;
52
53
import { CandyDate , cloneDate , CompatibleValue , wrongSortOrder } from 'ng-zorro-antd/core/time' ;
53
54
import {
54
55
BooleanInput ,
@@ -241,6 +242,7 @@ export type NzPlacement = 'bottomLeft' | 'bottomRight' | 'topLeft' | 'topRight';
241
242
'(click)' : 'onClickInputBox($event)'
242
243
} ,
243
244
providers : [
245
+ NzDestroyService ,
244
246
DatePickerService ,
245
247
{
246
248
provide : NG_VALUE_ACCESSOR ,
@@ -250,7 +252,7 @@ export type NzPlacement = 'bottomLeft' | 'bottomRight' | 'topLeft' | 'topRight';
250
252
] ,
251
253
animations : [ slideMotion ]
252
254
} )
253
- export class NzDatePickerComponent implements OnInit , OnChanges , OnDestroy , AfterViewInit , ControlValueAccessor {
255
+ export class NzDatePickerComponent implements OnInit , OnChanges , AfterViewInit , ControlValueAccessor {
254
256
readonly _nzModuleName : NzConfigKey = NZ_CONFIG_MODULE_NAME ;
255
257
static ngAcceptInputType_nzAllowClear : BooleanInput ;
256
258
static ngAcceptInputType_nzAutoFocus : BooleanInput ;
@@ -274,7 +276,6 @@ export class NzDatePickerComponent implements OnInit, OnChanges, OnDestroy, Afte
274
276
hasFeedback : boolean = false ;
275
277
276
278
public panelMode : NzDateMode | NzDateMode [ ] = 'date' ;
277
- private destroyed$ : Subject < void > = new Subject ( ) ;
278
279
private isCustomPlaceHolder : boolean = false ;
279
280
private isCustomFormat : boolean = false ;
280
281
private showTime : SupportTimeOptions | boolean = false ;
@@ -358,23 +359,33 @@ export class NzDatePickerComponent implements OnInit, OnChanges, OnDestroy, Afte
358
359
if ( this . isRange && this . platform . isBrowser ) {
359
360
this . nzResizeObserver
360
361
. observe ( this . elementRef )
361
- . pipe ( takeUntil ( this . destroyed $) )
362
+ . pipe ( takeUntil ( this . destroy $) )
362
363
. subscribe ( ( ) => {
363
364
this . updateInputWidthAndArrowLeft ( ) ;
364
365
} ) ;
365
366
}
366
367
367
- this . datePickerService . inputPartChange$ . pipe ( takeUntil ( this . destroyed $) ) . subscribe ( partType => {
368
+ this . datePickerService . inputPartChange$ . pipe ( takeUntil ( this . destroy $) ) . subscribe ( partType => {
368
369
if ( partType ) {
369
370
this . datePickerService . activeInput = partType ;
370
371
}
371
372
this . focus ( ) ;
372
373
this . updateInputWidthAndArrowLeft ( ) ;
373
374
} ) ;
374
375
375
- // prevent mousedown event to trigger focusout event when click in date picker
376
- // see: https://github.com/NG-ZORRO/ng-zorro-antd/issues/7450
377
- this . elementRef . nativeElement . addEventListener ( 'mousedown' , this . onMouseDown ) ;
376
+ if ( this . platform . isBrowser ) {
377
+ this . ngZone . runOutsideAngular ( ( ) =>
378
+ // prevent mousedown event to trigger focusout event when click in date picker
379
+ // see: https://github.com/NG-ZORRO/ng-zorro-antd/issues/7450
380
+ fromEvent ( this . elementRef . nativeElement , 'mousedown' )
381
+ . pipe ( takeUntil ( this . destroy$ ) )
382
+ . subscribe ( event => {
383
+ if ( ( event . target as HTMLInputElement ) . tagName . toLowerCase ( ) !== 'input' ) {
384
+ event . preventDefault ( ) ;
385
+ }
386
+ } )
387
+ ) ;
388
+ }
378
389
}
379
390
380
391
updateInputWidthAndArrowLeft ( ) : void {
@@ -413,12 +424,6 @@ export class NzDatePickerComponent implements OnInit, OnChanges, OnDestroy, Afte
413
424
}
414
425
}
415
426
416
- onMouseDown ( event : Event ) : void {
417
- if ( ( event . target as HTMLInputElement ) . tagName . toLowerCase ( ) !== 'input' ) {
418
- event . preventDefault ( ) ;
419
- }
420
- }
421
-
422
427
onFocus ( event : FocusEvent , partType ?: RangePartType ) : void {
423
428
event . preventDefault ( ) ;
424
429
if ( partType ) {
@@ -430,7 +435,7 @@ export class NzDatePickerComponent implements OnInit, OnChanges, OnDestroy, Afte
430
435
// blur event has not the relatedTarget in IE11, use focusout instead.
431
436
onFocusout ( event : FocusEvent ) : void {
432
437
event . preventDefault ( ) ;
433
- if ( ! this . elementRef . nativeElement . contains ( event . relatedTarget ) ) {
438
+ if ( ! this . elementRef . nativeElement . contains ( < Node > event . relatedTarget ) ) {
434
439
this . checkAndClose ( ) ;
435
440
}
436
441
this . renderClass ( false ) ;
@@ -596,10 +601,12 @@ export class NzDatePickerComponent implements OnInit, OnChanges, OnDestroy, Afte
596
601
protected i18n : NzI18nService ,
597
602
protected cdr : ChangeDetectorRef ,
598
603
private renderer : Renderer2 ,
599
- private elementRef : ElementRef ,
604
+ private ngZone : NgZone ,
605
+ private elementRef : ElementRef < HTMLElement > ,
600
606
private dateHelper : DateHelperService ,
601
607
private nzResizeObserver : NzResizeObserver ,
602
608
private platform : Platform ,
609
+ private destroy$ : NzDestroyService ,
603
610
@Inject ( DOCUMENT ) doc : NzSafeAny ,
604
611
@Optional ( ) private directionality : Directionality ,
605
612
@Host ( ) @Optional ( ) public noAnimation ?: NzNoAnimationDirective ,
@@ -618,21 +625,21 @@ export class NzDatePickerComponent implements OnInit, OnChanges, OnDestroy, Afte
618
625
} ) ,
619
626
withLatestFrom ( this . nzFormNoStatusService ? this . nzFormNoStatusService . noFormStatus : observableOf ( false ) ) ,
620
627
map ( ( [ { status, hasFeedback } , noStatus ] ) => ( { status : noStatus ? '' : status , hasFeedback } ) ) ,
621
- takeUntil ( this . destroyed $)
628
+ takeUntil ( this . destroy $)
622
629
)
623
630
. subscribe ( ( { status, hasFeedback } ) => {
624
631
this . setStatusStyles ( status , hasFeedback ) ;
625
632
} ) ;
626
633
627
634
// Subscribe the every locale change if the nzLocale is not handled by user
628
635
if ( ! this . nzLocale ) {
629
- this . i18n . localeChange . pipe ( takeUntil ( this . destroyed $) ) . subscribe ( ( ) => this . setLocale ( ) ) ;
636
+ this . i18n . localeChange . pipe ( takeUntil ( this . destroy $) ) . subscribe ( ( ) => this . setLocale ( ) ) ;
630
637
}
631
638
632
639
// Default value
633
640
this . datePickerService . isRange = this . isRange ;
634
641
this . datePickerService . initValue ( true ) ;
635
- this . datePickerService . emitValue$ . pipe ( takeUntil ( this . destroyed $) ) . subscribe ( ( ) => {
642
+ this . datePickerService . emitValue$ . pipe ( takeUntil ( this . destroy $) ) . subscribe ( ( ) => {
636
643
const value = this . datePickerService . value ;
637
644
const datePickerPreviousValue = this . datePickerService . initialValue ;
638
645
@@ -675,15 +682,15 @@ export class NzDatePickerComponent implements OnInit, OnChanges, OnDestroy, Afte
675
682
this . close ( ) ;
676
683
} ) ;
677
684
678
- this . directionality . change ?. pipe ( takeUntil ( this . destroyed $) ) . subscribe ( ( direction : Direction ) => {
685
+ this . directionality . change ?. pipe ( takeUntil ( this . destroy $) ) . subscribe ( ( direction : Direction ) => {
679
686
this . dir = direction ;
680
687
this . cdr . detectChanges ( ) ;
681
688
} ) ;
682
689
this . dir = this . directionality . value ;
683
690
this . inputValue = this . isRange ? [ '' , '' ] : '' ;
684
691
this . setModeAndFormat ( ) ;
685
692
686
- this . datePickerService . valueChange$ . pipe ( takeUntil ( this . destroyed $) ) . subscribe ( ( ) => {
693
+ this . datePickerService . valueChange$ . pipe ( takeUntil ( this . destroy $) ) . subscribe ( ( ) => {
687
694
this . updateInputValue ( ) ;
688
695
} ) ;
689
696
}
@@ -727,12 +734,6 @@ export class NzDatePickerComponent implements OnInit, OnChanges, OnDestroy, Afte
727
734
}
728
735
}
729
736
730
- ngOnDestroy ( ) : void {
731
- this . destroyed$ . next ( ) ;
732
- this . destroyed$ . complete ( ) ;
733
- this . elementRef . nativeElement . removeEventListener ( 'mousedown' , this . onMouseDown ) ;
734
- }
735
-
736
737
setModeAndFormat ( ) : void {
737
738
const inputFormats : { [ key in NzDateMode ] ?: string } = {
738
739
year : 'yyyy' ,
0 commit comments