Skip to content

Commit 9ea3d01

Browse files
howardjingjelbourn
authored andcommitted
feat(cdk/scrolling): update CdkVirtualForOf to work with sets. (#20594)
The previous usage of `Array.prototype.slice.call` does not handle `Set` objects appropriately (since a `Set` does not have a `length` property). Updated it to use `Array.from`. Fixes #20210. (cherry picked from commit e15f82c)
1 parent 02f7561 commit 9ea3d01

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/cdk/scrolling/virtual-for-of.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ export class CdkVirtualForOf<T> implements
105105
if (isDataSource(value)) {
106106
this._dataSourceChanges.next(value);
107107
} else {
108-
// Slice the value if its an NgIterable to ensure we're working with an array.
108+
// If value is an an NgIterable, convert it to an array.
109109
this._dataSourceChanges.next(new ArrayDataSource<T>(
110-
isObservable(value) ? value : Array.prototype.slice.call(value || [])));
110+
isObservable(value) ? value : Array.from(value || [])));
111111
}
112112
}
113113

src/cdk/scrolling/virtual-scroll-viewport.spec.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ describe('CdkVirtualScrollViewport', () => {
444444
.toBe(0, 'should render from first item');
445445
}));
446446

447-
it('should handle dynamic item array keeping position when possibile', fakeAsync(() => {
447+
it('should handle dynamic item array keeping position when possible', fakeAsync(() => {
448448
testComponent.items = Array(100).fill(0);
449449
finishInit(fixture);
450450
triggerScroll(viewport, testComponent.itemSize * 50);
@@ -516,6 +516,15 @@ describe('CdkVirtualScrollViewport', () => {
516516
}
517517
}));
518518

519+
it('should work with a Set', fakeAsync(() => {
520+
const data = new Set(['hello', 'world', 'how', 'are', 'you']);
521+
testComponent.items = data as any;
522+
finishInit(fixture);
523+
524+
expect(viewport.getRenderedRange())
525+
.toEqual({start: 0, end: 4}, 'newly emitted items should be rendered');
526+
}));
527+
519528
it('should work with an Observable', fakeAsync(() => {
520529
const data = new Subject<number[]>();
521530
testComponent.items = data as any;

0 commit comments

Comments
 (0)