Skip to content

virtual-scroll: add update method #11210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/cdk-experimental/scrolling/virtual-scroll-viewport.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ describe('CdkVirtualScrollViewport', () => {
expect(viewport.getViewportSize()).toBe(testComponent.viewportSize);
}));

it('should update viewport size', fakeAsync(() => {
testComponent.viewportSize = 300;
viewport.updateViewportSize();
expect(viewport.getViewportSize()).toBe(300);

testComponent.viewportSize = 500;
viewport.updateViewportSize();
expect(viewport.getViewportSize()).toBe(500);
}));

it('should get the rendered range', fakeAsync(() => {
finishInit(fixture);

Expand Down
20 changes: 16 additions & 4 deletions src/cdk-experimental/scrolling/virtual-scroll-viewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,12 @@ export class CdkVirtualScrollViewport implements DoCheck, OnInit, OnDestroy {
@Inject(VIRTUAL_SCROLL_STRATEGY) private _scrollStrategy: VirtualScrollStrategy) {}

ngOnInit() {
const viewportEl = this.elementRef.nativeElement;
Promise.resolve().then(() => {
this._viewportSize = this.orientation === 'horizontal' ?
viewportEl.clientWidth : viewportEl.clientHeight;
this._measureViewportSize();
this._scrollStrategy.attach(this);

this._ngZone.runOutsideAngular(() => {
fromEvent(viewportEl, 'scroll')
fromEvent(this.elementRef.nativeElement, 'scroll')
// Sample the scroll stream at every animation frame. This way if there are multiple
// scroll events in the same frame we only need to recheck our layout once.
.pipe(sampleTime(0, animationFrameScheduler), takeUntil(this._destroyed))
Expand Down Expand Up @@ -146,6 +144,13 @@ export class CdkVirtualScrollViewport implements DoCheck, OnInit, OnDestroy {
this._destroyed.complete();
}

/** Update the viewport dimensions and re-render. */
updateViewportSize() {
// TODO: Cleanup later when add logic for handling content resize
this._measureViewportSize();
this._scrollStrategy.onDataLengthChanged();
}

/** Attaches a `CdkVirtualForOf` to this viewport. */
attach(forOf: CdkVirtualForOf<any>) {
if (this._forOf) {
Expand Down Expand Up @@ -305,4 +310,11 @@ export class CdkVirtualScrollViewport implements DoCheck, OnInit, OnDestroy {
}
return this._forOf.measureRangeSize(range, this.orientation);
}

/** Measure the viewport size. */
private _measureViewportSize() {
const viewportEl = this.elementRef.nativeElement;
this._viewportSize = this.orientation === 'horizontal' ?
viewportEl.clientWidth : viewportEl.clientHeight;
}
}