Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(module:time-picker): do not run change detection when the time picker panel is clicked #7126

Merged
merged 1 commit into from
Dec 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion components/time-picker/time-picker-panel.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, DebugElement, NO_ERRORS_SCHEMA, ViewChild, ViewEncapsulation } from '@angular/core';
import { ApplicationRef, Component, DebugElement, NO_ERRORS_SCHEMA, ViewChild, ViewEncapsulation } from '@angular/core';
import { ComponentFixture, fakeAsync, flush, TestBed, tick, waitForAsync } from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';
Expand Down Expand Up @@ -158,6 +158,21 @@ describe('time-picker-panel', () => {
const listOfSelectedLi = panelElement.nativeElement.querySelector('.ant-picker-time-panel-cell-selected');
expect(listOfSelectedLi.offsetTop).toBe(0);
}));

describe('change detection behavior', () => {
it('should not run change detection when the timer picker panel is clicked', () => {
const appRef = TestBed.inject(ApplicationRef);
const event = new MouseEvent('mousedown');

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

fixture.nativeElement.querySelector('nz-time-picker-panel').dispatchEvent(event);

expect(appRef.tick).not.toHaveBeenCalled();
expect(event.preventDefault).toHaveBeenCalled();
});
});
});
describe('disabled time-picker-panel', () => {
let fixture: ComponentFixture<NzTestTimePanelDisabledComponent>;
Expand Down
24 changes: 11 additions & 13 deletions components/time-picker/time-picker-panel.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
ViewEncapsulation
} from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { Subject } from 'rxjs';
import { fromEvent, Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

import { reqAnimFrame } from 'ng-zorro-antd/core/polyfill';
Expand Down Expand Up @@ -124,8 +124,7 @@ export type NzTimePickerUnit = 'hour' | 'minute' | 'second' | '12-hour';
'[class.ant-picker-time-panel-column-2]': `enabledColumns === 2 && !nzInDatePicker`,
'[class.ant-picker-time-panel-column-3]': `enabledColumns === 3 && !nzInDatePicker`,
'[class.ant-picker-time-panel-narrow]': `enabledColumns < 3`,
'[class.ant-picker-time-panel-placement-bottomLeft]': `!nzInDatePicker`,
'(mousedown)': 'onMousedown($event)'
'[class.ant-picker-time-panel-placement-bottomLeft]': `!nzInDatePicker`
},
providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: NzTimePickerPanelComponent, multi: true }]
})
Expand Down Expand Up @@ -524,7 +523,7 @@ export class NzTimePickerPanelComponent implements ControlValueAccessor, OnInit,
private ngZone: NgZone,
private cdr: ChangeDetectorRef,
public dateHelper: DateHelperService,
private elementRef: ElementRef
private elementRef: ElementRef<HTMLElement>
) {
// TODO: move to host after View Engine deprecation
this.elementRef.nativeElement.classList.add('ant-picker-time-panel');
Expand All @@ -541,6 +540,14 @@ export class NzTimePickerPanelComponent implements ControlValueAccessor, OnInit,
this.scrollToTime();
this.firstScrolled = true;
});

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

ngOnDestroy(): void {
Expand Down Expand Up @@ -577,13 +584,4 @@ export class NzTimePickerPanelComponent implements ControlValueAccessor, OnInit,
registerOnTouched(fn: () => void): void {
this.onTouch = fn;
}

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