Skip to content

Commit 2a68edb

Browse files
amcdnlmmalerba
authored andcommitted
virtual-scroll: add update method (#11476)
Supersedes #11210
1 parent c3899cf commit 2a68edb

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ describe('CdkVirtualScrollViewport', () => {
5757
expect(viewport.getViewportSize()).toBe(testComponent.viewportSize);
5858
}));
5959

60+
it('should update viewport size', fakeAsync(() => {
61+
testComponent.viewportSize = 300;
62+
fixture.detectChanges();
63+
viewport.checkViewportSize();
64+
expect(viewport.getViewportSize()).toBe(300);
65+
66+
testComponent.viewportSize = 500;
67+
fixture.detectChanges();
68+
viewport.checkViewportSize();
69+
expect(viewport.getViewportSize()).toBe(500);
70+
}));
71+
6072
it('should get the rendered range', fakeAsync(() => {
6173
finishInit(fixture);
6274

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,14 @@ export class CdkVirtualScrollViewport implements DoCheck, OnInit, OnDestroy {
107107
@Inject(VIRTUAL_SCROLL_STRATEGY) private _scrollStrategy: VirtualScrollStrategy) {}
108108

109109
ngOnInit() {
110-
const viewportEl = this.elementRef.nativeElement;
111110
// It's still too early to measure the viewport at this point. Deferring with a promise allows
112111
// the Viewport to be rendered with the correct size before we measure.
113112
Promise.resolve().then(() => {
114-
this._viewportSize = this.orientation === 'horizontal' ?
115-
viewportEl.clientWidth : viewportEl.clientHeight;
113+
this._measureViewportSize();
116114
this._scrollStrategy.attach(this);
117115

118116
this._ngZone.runOutsideAngular(() => {
119-
fromEvent(viewportEl, 'scroll')
117+
fromEvent(this.elementRef.nativeElement, 'scroll')
120118
// Sample the scroll stream at every animation frame. This way if there are multiple
121119
// scroll events in the same frame we only need to recheck our layout once.
122120
.pipe(sampleTime(0, animationFrameScheduler), takeUntil(this._destroyed))
@@ -307,4 +305,18 @@ export class CdkVirtualScrollViewport implements DoCheck, OnInit, OnDestroy {
307305
}
308306
return this._forOf.measureRangeSize(range, this.orientation);
309307
}
308+
309+
/** Update the viewport dimensions and re-render. */
310+
checkViewportSize() {
311+
// TODO: Cleanup later when add logic for handling content resize
312+
this._measureViewportSize();
313+
this._scrollStrategy.onDataLengthChanged();
314+
}
315+
316+
/** Measure the viewport size. */
317+
private _measureViewportSize() {
318+
const viewportEl = this.elementRef.nativeElement;
319+
this._viewportSize = this.orientation === 'horizontal' ?
320+
viewportEl.clientWidth : viewportEl.clientHeight;
321+
}
310322
}

0 commit comments

Comments
 (0)