Skip to content

Commit

Permalink
perf(module:date-picker): do not run change detection when the `date-…
Browse files Browse the repository at this point in the history
…range-popup` is clicked (#7096)
  • Loading branch information
arturovt committed Dec 16, 2021
1 parent 0d4ad23 commit 8f8c71b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
18 changes: 17 additions & 1 deletion components/date-picker/date-picker.component.spec.ts
Expand Up @@ -3,7 +3,7 @@ import { ESCAPE } from '@angular/cdk/keycodes';
import { OverlayContainer } from '@angular/cdk/overlay';
import { registerLocaleData } from '@angular/common';
import zh from '@angular/common/locales/zh';
import { Component, DebugElement, TemplateRef, ViewChild } from '@angular/core';
import { ApplicationRef, Component, DebugElement, TemplateRef, ViewChild } from '@angular/core';
import { ComponentFixture, fakeAsync, flush, inject, TestBed, tick } from '@angular/core/testing';
import { FormControl, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';
Expand Down Expand Up @@ -489,6 +489,22 @@ describe('NzDatePickerComponent', () => {
expect(result.getDate()).toBe(+cellText);
}));

it('should not run change detection when inline mode is enabled and the `date-range-popup` is clicked', () => {
fixtureInstance.nzInline = true;
fixture.detectChanges();

const appRef = TestBed.inject(ApplicationRef);
const event = new MouseEvent('mousedown');

spyOn(appRef, 'tick');
spyOn(event, 'preventDefault').and.callThrough();

debugElement.nativeElement.querySelector('date-range-popup').dispatchEvent(event);

expect(appRef.tick).not.toHaveBeenCalled();
expect(event.preventDefault).toHaveBeenCalled();
});

it('should support nzBackdrop', fakeAsync(() => {
fixtureInstance.nzBackdrop = true;
fixture.detectChanges();
Expand Down
31 changes: 16 additions & 15 deletions components/date-picker/date-range-popup.component.ts
Expand Up @@ -8,8 +8,10 @@ import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ElementRef,
EventEmitter,
Input,
NgZone,
OnChanges,
OnDestroy,
OnInit,
Expand All @@ -18,7 +20,7 @@ import {
TemplateRef,
ViewEncapsulation
} from '@angular/core';
import { merge, Subject } from 'rxjs';
import { fromEvent, merge, Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

import {
Expand Down Expand Up @@ -139,10 +141,7 @@ import { getTimeConfig, isAllowedDate, PREFIX_CLASS } from './util';
<span class="ant-tag ant-tag-blue">{{ name }}</span>
</li>
</ng-template>
`,
host: {
'(mousedown)': 'onMousedown($event)'
}
`
})
export class DateRangePopupComponent implements OnInit, OnChanges, OnDestroy {
@Input() isRange!: boolean;
Expand Down Expand Up @@ -180,7 +179,12 @@ export class DateRangePopupComponent implements OnInit, OnChanges, OnDestroy {
return this.showToday || this.hasTimePicker || !!this.extraFooter || !!this.ranges;
}

constructor(public datePickerService: DatePickerService, public cdr: ChangeDetectorRef) {}
constructor(
public datePickerService: DatePickerService,
public cdr: ChangeDetectorRef,
private ngZone: NgZone,
private host: ElementRef<HTMLElement>
) {}

ngOnInit(): void {
merge(this.datePickerService.valueChange$, this.datePickerService.inputPartChange$)
Expand All @@ -189,6 +193,12 @@ export class DateRangePopupComponent implements OnInit, OnChanges, OnDestroy {
this.updateActiveDate();
this.cdr.markForCheck();
});

this.ngZone.runOutsideAngular(() => {
fromEvent(this.host.nativeElement, 'mousedown')
.pipe(takeUntil(this.destroy$))
.subscribe(event => event.preventDefault());
});
}

ngOnChanges(changes: SimpleChanges): void {
Expand Down Expand Up @@ -222,15 +232,6 @@ export class DateRangePopupComponent implements OnInit, OnChanges, OnDestroy {
);
}

/**
* Prevent input losing focus when click panel
*
* @param event
*/
onMousedown(event: MouseEvent): void {
event.preventDefault();
}

onClickOk(): void {
const inputIndex = { left: 0, right: 1 }[this.datePickerService.activeInput];
const value: CandyDate = this.isRange
Expand Down

0 comments on commit 8f8c71b

Please sign in to comment.