From 65a62ad1b402caa43438f9dd406e9c27f64e67cc Mon Sep 17 00:00:00 2001 From: Didier Colens Date: Sun, 4 Feb 2018 18:12:00 +0100 Subject: [PATCH] fix for #998 --- source/Grid/Grid.jest.js | 80 ++++++++++++++++++++++++++++++++++++++++ source/Grid/Grid.js | 5 ++- 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/source/Grid/Grid.jest.js b/source/Grid/Grid.jest.js index a71365db1..1000e8aa8 100644 --- a/source/Grid/Grid.jest.js +++ b/source/Grid/Grid.jest.js @@ -637,6 +637,86 @@ describe('Grid', () => { expect(node.scrollTop).toBe(1920); }); + // See issue #998 + it('call to recomputeGridSize should not reset position to scrollToRow when scrolling forward', () => { + const cellRendererCalls = []; + function cellRenderer({ columnIndex, key, rowIndex, style }) { + cellRendererCalls.push({ columnIndex, rowIndex }); + return defaultCellRenderer({ columnIndex, key, rowIndex, style }); + } + const props = { + cellRenderer, + columnWidth: 100, + height: 40, + rowHeight: 20, + scrollToRow: 0, + width: 100, + }; + + const grid = render(getMarkup(props)); + expect(cellRendererCalls).toEqual([ + { columnIndex: 0, rowIndex: 0 }, + { columnIndex: 0, rowIndex: 1 }, + ]); + + //scroll forward to row 45 + simulateScroll({ grid, scrollTop: 900 }); + + expect(grid.state.scrollDirectionVertical).toEqual( + SCROLL_DIRECTION_FORWARD, + ); + cellRendererCalls.splice(0); + + grid.recomputeGridSize({ rowIndex: 45 }); + + //grid should still be at row 45 + expect(cellRendererCalls).toEqual([ + { columnIndex: 0, rowIndex: 45 }, + { columnIndex: 0, rowIndex: 46 }, + ]); + }); + // see #998 + it('call to recomputeGridSize should not reset position to scrollToRow when scrolling backward', () => { + const cellRendererCalls = []; + function cellRenderer({ columnIndex, key, rowIndex, style }) { + cellRendererCalls.push({ columnIndex, rowIndex }); + return defaultCellRenderer({ columnIndex, key, rowIndex, style }); + } + const props = { + cellRenderer, + columnWidth: 100, + height: 40, + rowHeight: 20, + scrollToRow: 99, + width: 100, + }; + + const grid = render(getMarkup(props)); + expect(cellRendererCalls).toEqual([ + { columnIndex: 0, rowIndex: 0 }, + { columnIndex: 0, rowIndex: 1 }, + { columnIndex: 0, rowIndex: 98 }, + { columnIndex: 0, rowIndex: 99 }, + ]); + + //scroll backward to row 45 + simulateScroll({ grid, scrollTop: 900 }); + + expect(grid.state.scrollDirectionVertical).toEqual( + SCROLL_DIRECTION_BACKWARD, + ); + cellRendererCalls.splice(0); + + grid.recomputeGridSize({ rowIndex: 45 }); + + //grid should still be at row 45 + expect(cellRendererCalls).toEqual([ + { columnIndex: 0, rowIndex: 45 }, + { columnIndex: 0, rowIndex: 46 }, + ]); + }); + + 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 423ea5762..c7b74df54 100644 --- a/source/Grid/Grid.js +++ b/source/Grid/Grid.js @@ -481,7 +481,10 @@ export default class Grid extends React.PureComponent { // 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; + 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.