|
5 | 5 | * Forked from version 8.17.0; includes the following modifications: |
6 | 6 | * 1) Calculate actual row heights in determining startIndex. This allows dynamic row heights. |
7 | 7 | * */ |
8 | | -import calculateAverageHeight from './calculateAverageHeight'; |
| 8 | +// import calculateAverageHeight from './calculateAverageHeight'; |
9 | 9 |
|
10 | 10 | const calculateRows = ({ measuredRows, height, rowKey, rows, scrollTop = 0 }) => { |
11 | | - // used exact measured row heights for determining `startIndex` for smooth scroll |
12 | | - // average heights are not accurate when there is lots of variation in row heights |
| 11 | + // default overscan to 10 for now, could be accepted as a prop in the future |
| 12 | + const overscan = 20; |
| 13 | + |
| 14 | + // averageHeight of measuredRows can still be used to closely approximate amount of rows to render |
| 15 | + const measuredAmounts = Object.keys(measuredRows).map(key => measuredRows[key]); |
| 16 | + const amountOfMeasuredRows = measuredAmounts.length; |
| 17 | + const totalMeasuredHeight = measuredAmounts.reduce((a, b) => a + b, 0); |
| 18 | + |
| 19 | + // if we have no rows, use a small row height so we get a good sample in future loops |
| 20 | + const averageHeight = amountOfMeasuredRows > 0 ? totalMeasuredHeight / amountOfMeasuredRows : 50; |
| 21 | + |
13 | 22 | let startIndex = 0; |
14 | 23 | let startHeight = 0; |
15 | 24 | let accruedHeight = 0; |
| 25 | + let i = 0; |
16 | 26 |
|
17 | | - //default overscan to 10 for now, could be accepted as a prop in the future |
18 | | - const overscan = 10; |
19 | | - |
20 | | - for (let i = 0; i < Object.keys(measuredRows).length; i++) { |
| 27 | + while (accruedHeight < scrollTop) { |
21 | 28 | // measuredRows use aria-rowindex as identifiers which is 1 based |
22 | | - accruedHeight += measuredRows[i + 1]; |
23 | | - |
24 | | - if (scrollTop < accruedHeight) { |
| 29 | + if (measuredRows.hasOwnProperty(i + 1)) { |
| 30 | + accruedHeight += measuredRows[i + 1]; |
| 31 | + } else { |
| 32 | + accruedHeight += averageHeight; |
| 33 | + } |
| 34 | + if (scrollTop <= accruedHeight) { |
25 | 35 | startIndex = i; |
26 | 36 | break; |
27 | | - } |
28 | | - else if(i + overscan > Object.keys(measuredRows).length){ |
29 | | - // stop accruing after we reach i + overscan |
| 37 | + } else if (i + overscan > rows.length) { |
| 38 | + // stop accruing after we reach i + overscan (the end) |
30 | 39 | startHeight = accruedHeight; |
31 | 40 | startIndex = i; |
32 | 41 | break; |
33 | 42 | } |
34 | | - else { |
35 | | - // accrue and continue |
36 | | - startHeight = accruedHeight; |
37 | | - } |
| 43 | + // accrue and continue |
| 44 | + startHeight = accruedHeight; |
| 45 | + i += 1; |
38 | 46 | } |
39 | 47 |
|
40 | | - // averageHeight of measuredRows can still be used to closely approximate amount of rows to render |
41 | | - // if this causes issues w/ row visibility, exact heights can still be used |
42 | | - const averageHeight = calculateAverageHeight(measuredRows); |
43 | 48 | const amountOfRowsToRender = Math.ceil(height / averageHeight) + overscan; |
44 | | - |
45 | | - // const zeroedIndex = startIndex; |
46 | 49 | const rowsToRender = rows.slice(startIndex, Math.max(startIndex + amountOfRowsToRender, 0)); |
47 | 50 |
|
48 | 51 | if (process.env.NODE_ENV !== 'production' && typeof window !== 'undefined' && window.LOG_VIRTUALIZED) { |
@@ -90,7 +93,7 @@ const calculateRows = ({ measuredRows, height, rowKey, rows, scrollTop = 0 }) => |
90 | 93 |
|
91 | 94 | return { |
92 | 95 | amountOfRowsToRender, |
93 | | - startIndex: startIndex, |
| 96 | + startIndex, |
94 | 97 | showExtraRow: !(startIndex % 2), |
95 | 98 | startHeight, |
96 | 99 | endHeight |
|
0 commit comments