diff --git a/source/CellMeasurer/CellMeasurer.DynamicHeightGrid.example.js b/source/CellMeasurer/CellMeasurer.DynamicHeightGrid.example.js index 685e9b7cf..02cae9aa2 100644 --- a/source/CellMeasurer/CellMeasurer.DynamicHeightGrid.example.js +++ b/source/CellMeasurer/CellMeasurer.DynamicHeightGrid.example.js @@ -40,6 +40,7 @@ export default class DynamicHeightGrid extends React.PureComponent { cellRenderer={this._cellRenderer} rowCount={1000} rowHeight={this._cache.rowHeight} + scrollToRow={100} width={width} /> ); diff --git a/source/CellMeasurer/CellMeasurer.DynamicWidthGrid.example.js b/source/CellMeasurer/CellMeasurer.DynamicWidthGrid.example.js index 8fd5e1771..faeb002b6 100644 --- a/source/CellMeasurer/CellMeasurer.DynamicWidthGrid.example.js +++ b/source/CellMeasurer/CellMeasurer.DynamicWidthGrid.example.js @@ -40,6 +40,7 @@ export default class DynamicWidthGrid extends React.PureComponent { cellRenderer={this._cellRenderer} rowCount={50} rowHeight={35} + scrollToColumn={100} width={width} /> ); diff --git a/source/Grid/Grid.jest.js b/source/Grid/Grid.jest.js index 39337ff86..c6441f7db 100644 --- a/source/Grid/Grid.jest.js +++ b/source/Grid/Grid.jest.js @@ -654,6 +654,56 @@ describe('Grid', () => { expect(node.scrollTop).toBe(1920); }); + it('should not restore scrollLeft when scrolling left and recomputeGridSize with columnIndex smaller than scrollToColumn', () => { + const props = { + columnWidth: 50, + columnCount: 100, + height: 100, + rowCount: 100, + rowHeight: 20, + scrollToColumn: 50, + scrollToRow: 50, + width: 100, + }; + const grid = render(getMarkup(props)); + + expect(grid.state.scrollLeft).toEqual(2450); + + simulateScroll({grid, scrollLeft: 2250}); + expect(grid.state.scrollLeft).toEqual(2250); + expect(grid.state.scrollDirectionHorizontal).toEqual( + SCROLL_DIRECTION_BACKWARD, + ); + + grid.recomputeGridSize({columnIndex: 30}); + expect(grid.state.scrollLeft).toEqual(2250); + }); + + it('should not restore scrollTop when scrolling up and recomputeGridSize with rowIndex smaller than scrollToRow', () => { + const props = { + columnWidth: 50, + columnCount: 100, + height: 100, + rowCount: 100, + rowHeight: 20, + scrollToColumn: 50, + scrollToRow: 50, + width: 100, + }; + const grid = render(getMarkup(props)); + + expect(grid.state.scrollTop).toEqual(920); + + simulateScroll({grid, scrollTop: 720}); + expect(grid.state.scrollTop).toEqual(720); + expect(grid.state.scrollDirectionVertical).toEqual( + SCROLL_DIRECTION_BACKWARD, + ); + + grid.recomputeGridSize({rowIndex: 20}); + expect(grid.state.scrollTop).toEqual(720); + }); + it('should restore scroll offset for column when row count increases from 0 (and vice versa)', () => { const props = { columnWidth: 50, diff --git a/source/Grid/Grid.js b/source/Grid/Grid.js index 4fbb90a97..e2f439214 100644 --- a/source/Grid/Grid.js +++ b/source/Grid/Grid.js @@ -543,8 +543,15 @@ class Grid extends React.PureComponent { // In this case the cDU handler can't know if they changed. // Store this flag to let the next cDU pass know it needs to recompute the scroll offset. this._recomputeScrollLeftFlag = - scrollToColumn >= 0 && columnIndex <= scrollToColumn; - this._recomputeScrollTopFlag = scrollToRow >= 0 && rowIndex <= scrollToRow; + scrollToColumn >= 0 && + (this.state.scrollDirectionHorizontal === SCROLL_DIRECTION_FORWARD + ? columnIndex <= scrollToColumn + : columnIndex >= scrollToColumn); + this._recomputeScrollTopFlag = + scrollToRow >= 0 && + (this.state.scrollDirectionVertical === SCROLL_DIRECTION_FORWARD + ? rowIndex <= scrollToRow + : rowIndex >= scrollToRow); // Clear cell cache in case we are scrolling; // Invalid row heights likely mean invalid cached content as well.