Skip to content

Commit 6cb2009

Browse files
MKirovaMayaKirova
authored andcommitted
fix(igxForOf): Add check in case scrollComponent is already destroyed.
1 parent 934d442 commit 6cb2009

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

projects/igniteui-angular/src/lib/directives/for-of/for_of.directive.spec.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,35 @@ describe('IgxForOf directive -', () => {
197197
horizontalScroller = fix.nativeElement.querySelector('igx-horizontal-virtual-helper');
198198
});
199199

200+
it('should reset scroll position if component is destroyed and recreated.', async () => {
201+
let scrollComponent = fix.debugElement.query(By.css(VERTICAL_SCROLLER)).componentInstance;
202+
expect(scrollComponent.scrollAmount).toBe(0);
203+
expect(scrollComponent.destroyed).toBeFalsy();
204+
205+
fix.componentInstance.scrollTop(500);
206+
fix.detectChanges();
207+
await wait();
208+
expect(scrollComponent.scrollAmount).toBe(500);
209+
210+
fix.componentInstance.exists = false;
211+
fix.detectChanges();
212+
await wait();
213+
214+
expect(scrollComponent.destroyed).toBeTruthy();
215+
216+
fix.componentInstance.exists = true;
217+
fix.detectChanges();
218+
await wait();
219+
220+
scrollComponent = fix.debugElement.query(By.css(VERTICAL_SCROLLER)).componentInstance;
221+
expect(scrollComponent.scrollAmount).toBe(0);
222+
expect(scrollComponent.destroyed).toBeFalsy();
223+
224+
displayContainer = fix.nativeElement.querySelector(DISPLAY_CONTAINER);
225+
const firstInnerDisplayContainer = displayContainer.children[0];
226+
expect(firstInnerDisplayContainer.children[0].textContent).toBe('0');
227+
});
228+
200229
it('should initialize directive with vertical virtualization', async () => {
201230
expect(displayContainer).not.toBeNull();
202231
expect(verticalScroller).not.toBeNull();
@@ -1252,10 +1281,10 @@ export class VirtualComponent {
12521281
public cols = [];
12531282
public data = [];
12541283

1255-
@ViewChild('container', { read: ViewContainerRef, static: true })
1284+
@ViewChild('container', { read: ViewContainerRef, static: false })
12561285
public container: ViewContainerRef;
12571286

1258-
@ViewChild('scrollContainer', { read: TestIgxForOfDirective, static: true })
1287+
@ViewChild('scrollContainer', { read: TestIgxForOfDirective, static: false })
12591288
public parentVirtDir: TestIgxForOfDirective<any>;
12601289

12611290
@ViewChildren('childContainer', { read: TestIgxForOfDirective })
@@ -1299,7 +1328,7 @@ export class VirtualComponent {
12991328
/** Only vertically virtualized component */
13001329
@Component({
13011330
template: `
1302-
<div #container [style.width]='width' [style.height]='height'>
1331+
<div *ngIf='exists' #container [style.width]='width' [style.height]='height'>
13031332
<ng-template #scrollContainer igxForTest let-rowData [igxForOf]="data"
13041333
[igxForScrollOrientation]="'vertical'"
13051334
[igxForContainerSize]='height'
@@ -1327,6 +1356,7 @@ export class VerticalVirtualComponent extends VirtualComponent {
13271356
];
13281357
public data = [];
13291358
public itemSize = '50px';
1359+
public exists = true;
13301360
}
13311361

13321362
/** Only horizontally virtualized component */

projects/igniteui-angular/src/lib/directives/for-of/for_of.directive.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ export class IgxForOfDirective<T> implements OnInit, OnChanges, DoCheck, OnDestr
390390
this.scrollComponent = this.syncScrollService.getScrollMaster(this.igxForScrollOrientation);
391391
this.state.chunkSize = this._calculateChunkSize();
392392
this.dc.instance.notVirtual = !(this.igxForContainerSize && this.state.chunkSize < this.igxForOf.length);
393-
if (this.scrollComponent) {
393+
if (this.scrollComponent && !this.scrollComponent.destroyed) {
394394
this.state.startIndex = Math.min(this.getIndexAt(this.scrollPosition, this.sizesCache, 0),
395395
this.igxForOf.length - this.state.chunkSize);
396396
}

src/app/list-performance/list-performance.sample.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ <h4 class="sample-title">Performance sample</h4>
1313
<igx-icon>clear</igx-icon>
1414
</igx-suffix>
1515
</igx-input-group>
16-
16+
<button igxButton (click)='showList = !showList'>Toggle List</button>
1717
<igx-card>
18-
<igx-list>
18+
<igx-list *ngIf='showList'>
1919
<igx-list-item [isHeader]="true">Contacts</igx-list-item>
2020
<div class="virtualized-items" [style.height]="'500px'" [style.overflow]="'hidden'" [style.position]="'relative'">
2121
<igx-list-item igxRipple="pink" igxRippleTarget=".igx-list__item" *igxFor="let item of data | igxFilter: fo1; scrollOrientation : 'vertical'; containerSize: '500px'; itemSize: '56px'">

src/app/list-performance/list-performance.sample.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { IgxFilterOptions } from 'igniteui-angular';
99
export class ListPerformanceSampleComponent {
1010
search1: string;
1111
options = {};
12+
showList = true;
1213

1314
data = [{
1415
key: 1,

0 commit comments

Comments
 (0)