Skip to content

Commit 285fe6c

Browse files
authored
virtual-scroll: address amcdnl's feedback (#10988)
* rewrite offsets to the end of the rendered content as offsets to the start * add some more autosize demos for testing * make sure not to remove too many items * virtual-scroll: address amcdnl's feedback
1 parent 1f9051e commit 285fe6c

File tree

5 files changed

+42
-5
lines changed

5 files changed

+42
-5
lines changed

src/cdk-experimental/scrolling/auto-size-virtual-scroll.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import {coerceNumberProperty} from '@angular/cdk/coercion';
910
import {ListRange} from '@angular/cdk/collections';
1011
import {Directive, forwardRef, Input, OnChanges} from '@angular/core';
1112
import {VIRTUAL_SCROLL_STRATEGY, VirtualScrollStrategy} from './virtual-scroll-strategy';
@@ -386,14 +387,20 @@ export class CdkAutoSizeVirtualScroll implements OnChanges {
386387
* The minimum amount of buffer rendered beyond the viewport (in pixels).
387388
* If the amount of buffer dips below this number, more items will be rendered.
388389
*/
389-
@Input() minBufferPx: number = 100;
390+
@Input()
391+
get minBufferPx(): number { return this._minBufferPx; }
392+
set minBufferPx(value: number) { this._minBufferPx = coerceNumberProperty(value); }
393+
_minBufferPx = 100;
390394

391395
/**
392396
* The number of pixels worth of buffer to shoot for when rendering new items.
393397
* If the actual amount turns out to be less it will not necessarily trigger an additional
394398
* rendering cycle (as long as the amount of buffer is still greater than `minBufferPx`).
395399
*/
396-
@Input() addBufferPx: number = 200;
400+
@Input()
401+
get addBufferPx(): number { return this._addBufferPx; }
402+
set addBufferPx(value: number) { this._addBufferPx = coerceNumberProperty(value); }
403+
_addBufferPx = 200;
397404

398405
/** The scroll strategy used by this directive. */
399406
_scrollStrategy = new AutoSizeVirtualScrollStrategy(this.minBufferPx, this.addBufferPx);

src/cdk-experimental/scrolling/fixed-size-virtual-scroll.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import {coerceNumberProperty} from '@angular/cdk/coercion';
910
import {ListRange} from '@angular/cdk/collections';
1011
import {Directive, forwardRef, Input, OnChanges} from '@angular/core';
1112
import {VIRTUAL_SCROLL_STRATEGY, VirtualScrollStrategy} from './virtual-scroll-strategy';
@@ -139,10 +140,16 @@ export function _fixedSizeVirtualScrollStrategyFactory(fixedSizeDir: CdkFixedSiz
139140
})
140141
export class CdkFixedSizeVirtualScroll implements OnChanges {
141142
/** The size of the items in the list (in pixels). */
142-
@Input() itemSize = 20;
143+
@Input()
144+
get itemSize(): number { return this._itemSize; }
145+
set itemSize(value: number) { this._itemSize = coerceNumberProperty(value); }
146+
_itemSize = 20;
143147

144148
/** The number of extra elements to render on either side of the scrolling viewport. */
145-
@Input() bufferSize = 5;
149+
@Input()
150+
get bufferSize(): number { return this._bufferSize; }
151+
set bufferSize(value: number) { this._bufferSize = coerceNumberProperty(value); }
152+
_bufferSize = 5;
146153

147154
/** The scroll strategy used by this directive. */
148155
_scrollStrategy = new FixedSizeVirtualScrollStrategy(this.itemSize, this.bufferSize);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export class CdkVirtualForOf<T> implements CollectionViewer, DoCheck, OnDestroy
7171
const ds = value instanceof DataSource ? value :
7272
// Slice the value if its an NgIterable to ensure we're working with an array.
7373
new ArrayDataSource<T>(
74-
value instanceof Observable ? value : Array.prototype.slice.call(value));
74+
value instanceof Observable ? value : Array.prototype.slice.call(value || []));
7575
this._dataSourceChanges.next(ds);
7676
}
7777
_cdkVirtualForOf: DataSource<T> | Observable<T[]> | NgIterable<T>;

src/demo-app/virtual-scroll/virtual-scroll-demo.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,12 @@ <h2>Fixed size</h2>
4848
Item #{{i}} - ({{size}}px)
4949
</div>
5050
</cdk-virtual-scroll-viewport>
51+
52+
<h2>Observable data</h2>
53+
54+
<cdk-virtual-scroll-viewport class="demo-viewport" [itemSize]="50">
55+
<div *cdkVirtualFor="let size of observableData; let i = index" class="demo-item"
56+
[style.height.px]="size">
57+
Item #{{i}} - ({{size}}px)
58+
</div>
59+
</cdk-virtual-scroll-viewport>

src/demo-app/virtual-scroll/virtual-scroll-demo.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
import {Component, ViewEncapsulation} from '@angular/core';
10+
import {BehaviorSubject} from 'rxjs/index';
1011

1112
@Component({
1213
moduleId: module.id,
@@ -21,4 +22,17 @@ export class VirtualScrollDemo {
2122
decreasingSizeData = Array(10000).fill(0)
2223
.map((_, i) => (1 + Math.floor((10000 - i) / 1000)) * 20);
2324
randomData = Array(10000).fill(0).map(() => Math.round(Math.random() * 100));
25+
observableData = new BehaviorSubject<number[]>([]);
26+
27+
constructor() {
28+
this.emitData();
29+
}
30+
31+
emitData() {
32+
let data = this.observableData.value.concat([50]);
33+
this.observableData.next(data);
34+
if (data.length < 1000) {
35+
setTimeout(() => this.emitData(), 1000);
36+
}
37+
}
2438
}

0 commit comments

Comments
 (0)