-
Notifications
You must be signed in to change notification settings - Fork 6.8k
virtual-scroll: add support for scrollToOffset
and scrollToIndex
#12272
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
Changes from 10 commits
868bbf9
a31a4c8
017a131
2527306
05c29b9
dce1cad
38487f7
91bcd58
5a63174
0947381
0b253eb
54705af
496571b
983e03b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -155,6 +155,30 @@ describe('CdkVirtualScrollViewport', () => { | |
expect(viewport.getRenderedRange()).toEqual({start: 2, end: 6}); | ||
})); | ||
|
||
it('should scroll to offset', fakeAsync(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a test for horizontal mode? |
||
finishInit(fixture); | ||
viewport.scrollToOffset(testComponent.itemSize * 2); | ||
|
||
triggerScroll(viewport); | ||
fixture.detectChanges(); | ||
flush(); | ||
|
||
expect(viewport.elementRef.nativeElement.scrollTop).toBe(testComponent.itemSize * 2); | ||
expect(viewport.getRenderedRange()).toEqual({start: 2, end: 6}); | ||
})); | ||
|
||
it('should scroll to index', fakeAsync(() => { | ||
finishInit(fixture); | ||
viewport.scrollToIndex(2); | ||
|
||
triggerScroll(viewport); | ||
fixture.detectChanges(); | ||
flush(); | ||
|
||
expect(viewport.elementRef.nativeElement.scrollTop).toBe(testComponent.itemSize * 2); | ||
expect(viewport.getRenderedRange()).toEqual({start: 2, end: 6}); | ||
})); | ||
|
||
it('should update viewport as user scrolls down', fakeAsync(() => { | ||
finishInit(fixture); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
*/ | ||
|
||
import {ListRange} from '@angular/cdk/collections'; | ||
import {supportsScrollBehavior} from '@angular/cdk/platform'; | ||
import { | ||
ChangeDetectionStrategy, | ||
ChangeDetectorRef, | ||
|
@@ -245,7 +246,28 @@ export class CdkVirtualScrollViewport implements OnInit, OnDestroy { | |
} | ||
} | ||
|
||
/** Sets the scroll offset on the viewport. */ | ||
/** Scrolls to the offset on the viewport. */ | ||
scrollToOffset(offset: number, behavior: ScrollBehavior = 'auto') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add |
||
const viewportElement = this.elementRef.nativeElement; | ||
const offsetDirection = this.orientation === 'horizontal' ? 'left' : 'top'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like this variable is only used when the scroll behavior is supported. Consider moving it into the |
||
|
||
if (supportsScrollBehavior()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do this differently based on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method signatures aren't compatible, old browsers only support: The new ones also support: |
||
viewportElement.scrollTo({[offsetDirection]: offset, behavior}); | ||
} else { | ||
if (this.orientation === 'horizontal') { | ||
viewportElement.scrollLeft = offset; | ||
} else { | ||
viewportElement.scrollTop = offset; | ||
} | ||
} | ||
} | ||
|
||
/** Scroll the viewport to the specified index. */ | ||
scrollToIndex(index: number, behavior: ScrollBehavior = 'auto') { | ||
this._scrollStrategy.scrollToIndex(index, behavior); | ||
} | ||
|
||
/** Internal method to set the scroll offset on the viewport. */ | ||
setScrollOffset(offset: number) { | ||
// Rather than setting the offset immediately, we batch it up to be applied along with other DOM | ||
// writes during the next change detection cycle. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,11 @@ export function supportsPassiveEventListeners(): boolean { | |
return supportsPassiveEvents; | ||
} | ||
|
||
/** Check whether the browser supports scroll behaviors. */ | ||
export function supportsScrollBehavior(): boolean { | ||
return 'scrollBehavior' in document.documentElement.style; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will probably throw an error in Universal. |
||
} | ||
|
||
/** Cached result Set of input types support by the current browser. */ | ||
let supportedInputTypes: Set<string>; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a return type here since it's part of the public API. I think some of the other methods are missing return types as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh this actually doesn't return anything, must've put that by mistake
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should still have
void
, though, since dgeni can't use the inferred types. Also missing@param
JsDocs