Skip to content

Commit 26882f2

Browse files
author
Brian Vaughn
committed
Merge branch 'add-public-methods-for-animation' of https://github.com/imadha/react-virtualized into imadha-add-public-methods-for-animation
2 parents 84bac59 + 21e791e commit 26882f2

File tree

3 files changed

+105
-16
lines changed

3 files changed

+105
-16
lines changed

source/Grid/Grid.jest.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,35 @@ describe('Grid', () => {
366366
expect(grid.state.scrollTop).toEqual(900)
367367
})
368368

369+
it('should support scrollToPosition() public method', () => {
370+
const grid = render(getMarkup())
371+
expect(grid.state.scrollLeft).toEqual(0)
372+
expect(grid.state.scrollTop).toEqual(0)
373+
374+
grid.scrollToPosition({
375+
scrollLeft: 50,
376+
scrollTop: 50
377+
})
378+
expect(grid.state.scrollLeft).toEqual(50)
379+
expect(grid.state.scrollTop).toEqual(50)
380+
})
381+
382+
it('should support getOffsetForCell() public method', () => {
383+
const grid = render(getMarkup())
384+
const { scrollLeft, scrollTop } = grid.getOffsetForCell({
385+
columnIndex: 24,
386+
rowIndex: 49
387+
})
388+
// 100 columns * 50 item width = 5,000 total item width
389+
// 4 columns can be visible at a time and :scrollLeft is initially 0,
390+
// So the minimum amount of scrolling leaves the 25th item at the right (just scrolled into view).
391+
expect(scrollLeft).toEqual(1050)
392+
// 100 rows * 20 item height = 2,000 total item height
393+
// 5 rows can be visible at a time and :scrollTop is initially 0,
394+
// So the minimum amount of scrolling leaves the 50th item at the bottom (just scrolled into view).
395+
expect(scrollTop).toEqual(900)
396+
})
397+
369398
// See issue #565
370399
it('should update scroll position to account for changed cell sizes within a function prop wrapper', () => {
371400
let rowHeight = 20

source/Grid/Grid.js

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,6 @@ export default class Grid extends PureComponent {
270270
this._invokeOnGridRenderedHelper = this._invokeOnGridRenderedHelper.bind(this)
271271
this._onScroll = this._onScroll.bind(this)
272272
this._setScrollingContainerRef = this._setScrollingContainerRef.bind(this)
273-
this._updateScrollLeftForScrollToColumn = this._updateScrollLeftForScrollToColumn.bind(this)
274-
this._updateScrollTopForScrollToRow = this._updateScrollTopForScrollToRow.bind(this)
275273

276274
this._columnWidthGetter = this._wrapSizeGetter(props.columnWidth)
277275
this._rowHeightGetter = this._wrapSizeGetter(props.rowHeight)
@@ -387,6 +385,40 @@ export default class Grid extends PureComponent {
387385
})
388386
}
389387

388+
/**
389+
* Ensure offset position is visible
390+
* Useful for animating position changes
391+
*/
392+
scrollToPosition ({
393+
scrollLeft,
394+
scrollTop
395+
} = {}) {
396+
this._setScrollPosition({ scrollLeft, scrollTop })
397+
}
398+
399+
/**
400+
* Gets offsets for a given cell and alignment
401+
*/
402+
getOffsetForCell ({
403+
columnIndex,
404+
rowIndex,
405+
scrollToAlignment = this.props.scrollToAlignment
406+
} = {}) {
407+
const scrollToColumn = columnIndex >= 0 ? columnIndex : this.props.scrollToColumn
408+
const scrollToRow = rowIndex >= 0 ? rowIndex : this.props.scrollToRow
409+
const offsetProps = {
410+
...this.props,
411+
scrollToColumn,
412+
scrollToRow,
413+
scrollToAlignment
414+
}
415+
416+
return {
417+
scrollLeft: this._getCalculatedScrollLeft(offsetProps),
418+
scrollTop: this._getCalculatedScrollTop(offsetProps)
419+
}
420+
}
421+
390422
componentDidMount () {
391423
const { getScrollbarSize, scrollLeft, scrollToColumn, scrollTop, scrollToRow } = this.props
392424

@@ -1006,7 +1038,7 @@ export default class Grid extends PureComponent {
10061038
return this._wrapPropertyGetter(size)
10071039
}
10081040

1009-
_updateScrollLeftForScrollToColumn (props = this.props, state = this.state) {
1041+
_getCalculatedScrollLeft (props = this.props, state = this.state) {
10101042
const { columnCount, height, scrollToAlignment, scrollToColumn, width } = props
10111043
const { scrollLeft } = state
10121044

@@ -1015,22 +1047,27 @@ export default class Grid extends PureComponent {
10151047
const totalRowsHeight = this._rowSizeAndPositionManager.getTotalSize()
10161048
const scrollBarSize = totalRowsHeight > height ? this._scrollbarSize : 0
10171049

1018-
const calculatedScrollLeft = this._columnSizeAndPositionManager.getUpdatedOffsetForIndex({
1050+
return this._columnSizeAndPositionManager.getUpdatedOffsetForIndex({
10191051
align: scrollToAlignment,
10201052
containerSize: width - scrollBarSize,
10211053
currentOffset: scrollLeft,
10221054
targetIndex
10231055
})
1056+
}
1057+
}
10241058

1025-
if (scrollLeft !== calculatedScrollLeft) {
1026-
this._setScrollPosition({
1027-
scrollLeft: calculatedScrollLeft
1028-
})
1029-
}
1059+
_updateScrollLeftForScrollToColumn (props = this.props, state = this.state) {
1060+
const { scrollLeft } = state
1061+
const calculatedScrollLeft = this._getCalculatedScrollLeft(props, state)
1062+
1063+
if (calculatedScrollLeft >= 0 && scrollLeft !== calculatedScrollLeft) {
1064+
this._setScrollPosition({
1065+
scrollLeft: calculatedScrollLeft
1066+
})
10301067
}
10311068
}
10321069

1033-
_updateScrollTopForScrollToRow (props = this.props, state = this.state) {
1070+
_getCalculatedScrollTop (props = this.props, state = this.state) {
10341071
const { height, rowCount, scrollToAlignment, scrollToRow, width } = props
10351072
const { scrollTop } = state
10361073

@@ -1039,18 +1076,23 @@ export default class Grid extends PureComponent {
10391076
const totalColumnsWidth = this._columnSizeAndPositionManager.getTotalSize()
10401077
const scrollBarSize = totalColumnsWidth > width ? this._scrollbarSize : 0
10411078

1042-
const calculatedScrollTop = this._rowSizeAndPositionManager.getUpdatedOffsetForIndex({
1079+
return this._rowSizeAndPositionManager.getUpdatedOffsetForIndex({
10431080
align: scrollToAlignment,
10441081
containerSize: height - scrollBarSize,
10451082
currentOffset: scrollTop,
10461083
targetIndex
10471084
})
1085+
}
1086+
}
10481087

1049-
if (scrollTop !== calculatedScrollTop) {
1050-
this._setScrollPosition({
1051-
scrollTop: calculatedScrollTop
1052-
})
1053-
}
1088+
_updateScrollTopForScrollToRow (props = this.props, state = this.state) {
1089+
const { scrollTop } = state
1090+
const calculatedScrollTop = this._getCalculatedScrollTop(props, state)
1091+
1092+
if (calculatedScrollTop >= 0 && scrollTop !== calculatedScrollTop) {
1093+
this._setScrollPosition({
1094+
scrollTop: calculatedScrollTop
1095+
})
10541096
}
10551097
}
10561098

source/List/List.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,19 @@ export default class List extends PureComponent {
126126
})
127127
}
128128

129+
/** See Grid#getOffsetForCell */
130+
getOffsetForRow ({
131+
rowIndex,
132+
scrollToAlignment
133+
}) {
134+
const { scrollTop } = this.Grid.getOffsetForCell({
135+
columnIndex: 0,
136+
rowIndex,
137+
scrollToAlignment
138+
})
139+
return scrollTop
140+
}
141+
129142
/** See Grid#scrollToCell */
130143
scrollToRow (index = 0) {
131144
this.Grid.scrollToCell({
@@ -134,6 +147,11 @@ export default class List extends PureComponent {
134147
})
135148
}
136149

150+
/** See Grid#scrollToPosition */
151+
scrollToPosition (scrollTop = 0) {
152+
this.Grid.scrollToPosition({ scrollTop })
153+
}
154+
137155
render () {
138156
const {
139157
className,

0 commit comments

Comments
 (0)