Skip to content

Commit 5ebfb48

Browse files
committed
virtual-scroll: address amcdnl's feedback
1 parent 461a3a9 commit 5ebfb48

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';
@@ -381,14 +382,20 @@ export class CdkAutoSizeVirtualScroll implements OnChanges {
381382
* The minimum amount of buffer rendered beyond the viewport (in pixels).
382383
* If the amount of buffer dips below this number, more items will be rendered.
383384
*/
384-
@Input() minBufferPx: number = 100;
385+
@Input()
386+
get minBufferPx(): number { return this._minBufferPx; }
387+
set minBufferPx(value: number) { this._minBufferPx = coerceNumberProperty(value); }
388+
_minBufferPx = 100;
385389

386390
/**
387391
* The number of pixels worth of buffer to shoot for when rendering new items.
388392
* If the actual amount turns out to be less it will not necessarily trigger an additional
389393
* rendering cycle (as long as the amount of buffer is still greater than `minBufferPx`).
390394
*/
391-
@Input() addBufferPx: number = 200;
395+
@Input()
396+
get addBufferPx(): number { return this._addBufferPx; }
397+
set addBufferPx(value: number) { this._addBufferPx = coerceNumberProperty(value); }
398+
_addBufferPx = 200;
392399

393400
/** The scroll strategy used by this directive. */
394401
_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)