Skip to content

Commit 93e4520

Browse files
MKirovaMayaKirova
authored andcommitted
fix(igxForOf): Add check in case scrollComponent is already destroyed.
1 parent 844f59f commit 93e4520

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,35 @@ describe('IgxForOf directive -', () => {
202202
horizontalScroller = fix.nativeElement.querySelector('igx-horizontal-virtual-helper');
203203
});
204204

205+
it('should reset scroll position if component is destroyed and recreated.', async () => {
206+
let scrollComponent = fix.debugElement.query(By.css(VERTICAL_SCROLLER)).componentInstance;
207+
expect(scrollComponent.scrollAmount).toBe(0);
208+
expect(scrollComponent.destroyed).toBeFalsy();
209+
210+
fix.componentInstance.scrollTop(500);
211+
fix.detectChanges();
212+
await wait();
213+
expect(scrollComponent.scrollAmount).toBe(500);
214+
215+
fix.componentInstance.exists = false;
216+
fix.detectChanges();
217+
await wait();
218+
219+
expect(scrollComponent.destroyed).toBeTruthy();
220+
221+
fix.componentInstance.exists = true;
222+
fix.detectChanges();
223+
await wait();
224+
225+
scrollComponent = fix.debugElement.query(By.css(VERTICAL_SCROLLER)).componentInstance;
226+
expect(scrollComponent.scrollAmount).toBe(0);
227+
expect(scrollComponent.destroyed).toBeFalsy();
228+
229+
displayContainer = fix.nativeElement.querySelector(DISPLAY_CONTAINER);
230+
const firstInnerDisplayContainer = displayContainer.children[0];
231+
expect(firstInnerDisplayContainer.children[0].textContent).toBe('0');
232+
});
233+
205234
it('should initialize directive with vertical virtualization', async () => {
206235
expect(displayContainer).not.toBeNull();
207236
expect(verticalScroller).not.toBeNull();
@@ -1413,10 +1442,10 @@ export class VirtualComponent {
14131442
public cols = [];
14141443
public data = [];
14151444

1416-
@ViewChild('container', { read: ViewContainerRef, static: true })
1445+
@ViewChild('container', { read: ViewContainerRef, static: false })
14171446
public container: ViewContainerRef;
14181447

1419-
@ViewChild('scrollContainer', { read: TestIgxForOfDirective, static: true })
1448+
@ViewChild('scrollContainer', { read: TestIgxForOfDirective, static: false })
14201449
public parentVirtDir: TestIgxForOfDirective<any>;
14211450

14221451
@ViewChildren('childContainer', { read: TestIgxForOfDirective })

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
@@ -375,7 +375,7 @@ export class IgxForOfDirective<T> implements OnInit, OnChanges, DoCheck, OnDestr
375375
this.scrollComponent = this.syncScrollService.getScrollMaster(this.igxForScrollOrientation);
376376
this.state.chunkSize = this._calculateChunkSize();
377377
this.dc.instance.notVirtual = !(this.igxForContainerSize && this.state.chunkSize < this.igxForOf.length);
378-
if (this.scrollComponent) {
378+
if (this.scrollComponent && !this.scrollComponent.destroyed) {
379379
this.state.startIndex = Math.min(this.getIndexAt(this.scrollPosition, this.sizesCache, 0),
380380
this.igxForOf.length - this.state.chunkSize);
381381
}

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)