From 823e7e431ca0500cc66b705bda49a4ccd1603718 Mon Sep 17 00:00:00 2001 From: arturovt Date: Wed, 27 Jul 2022 00:32:10 +0300 Subject: [PATCH] perf(file): do not run change detection on `click` and `keyup` events when the `td-file-input` button is clicked --- .../src/file-input/file-input.component.html | 3 +- .../file-input/file-input.component.spec.ts | 26 ++++++++++++-- .../src/file-input/file-input.component.ts | 35 +++++++++++++++++-- 3 files changed, 57 insertions(+), 7 deletions(-) diff --git a/libs/angular/file/src/file-input/file-input.component.html b/libs/angular/file/src/file-input/file-input.component.html index 3b65a8a7e..f5b624cf0 100644 --- a/libs/angular/file/src/file-input/file-input.component.html +++ b/libs/angular/file/src/file-input/file-input.component.html @@ -1,12 +1,11 @@ ` element */ + @ViewChild('fileInputButton', { static: true, read: ElementRef }) + _inputButton!: ElementRef; + /** The native ` element */ - @ViewChild('fileInput', { static: true }) _inputElement!: ElementRef; + @ViewChild('fileInput', { static: true }) + _inputElement!: ElementRef; get inputElement(): HTMLInputElement { return this._inputElement.nativeElement; } @@ -104,13 +115,33 @@ export class TdFileInputComponent File | FileList >(); + private _destroy$ = new Subject(); + constructor( + private _ngZone: NgZone, private _renderer: Renderer2, _changeDetectorRef: ChangeDetectorRef ) { super(_changeDetectorRef); } + ngOnInit(): void { + this._ngZone.runOutsideAngular(() => { + merge( + fromEvent(this._inputButton.nativeElement, 'click'), + fromEvent(this._inputButton.nativeElement, 'keyup').pipe( + filter((event) => event.keyCode === ENTER) + ) + ) + .pipe(takeUntil(this._destroy$)) + .subscribe(() => this._inputElement.nativeElement.click()); + }); + } + + ngOnDestroy(): void { + this._destroy$.next(); + } + /** * Method executed when a file is selected. */