Skip to content

Commit 2577064

Browse files
author
路振凯
committed
fix: add bounds check to getColumnWidth function
1 parent 6a249a2 commit 2577064

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/VirtualTable/VirtualCell.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,15 @@ export interface VirtualCellProps<RecordType> {
3131
* Return the width of the column by `colSpan`.
3232
* When `colSpan` is `0` will be trade as `1`.
3333
*/
34-
export function getColumnWidth(colIndex: number, colSpan: number, columnsOffset: number[]) {
34+
export function getColumnWidth(colIndex: number, colSpan: number, columnsOffset: number[]): number {
3535
const mergedColSpan = colSpan || 1;
36-
return columnsOffset[colIndex + mergedColSpan] - (columnsOffset[colIndex] || 0);
36+
37+
const startIndex = Math.max(colIndex, 0);
38+
const endIndex = Math.min(startIndex + mergedColSpan, columnsOffset.length - 1);
39+
40+
const startOffset = columnsOffset[startIndex] || 0;
41+
const endOffset = columnsOffset[endIndex] || startOffset;
42+
return Math.max(endOffset - startOffset, 0);
3743
}
3844

3945
const VirtualCell = <RecordType,>(props: VirtualCellProps<RecordType>) => {

0 commit comments

Comments
 (0)