diff --git a/src/Table.tsx b/src/Table.tsx index 802a11a42..763882d46 100644 --- a/src/Table.tsx +++ b/src/Table.tsx @@ -63,7 +63,7 @@ import useColumns from './hooks/useColumns'; import { useLayoutState, useTimeoutLock } from './hooks/useFrame'; import { getPathValue, mergeObject, validateValue, getColumnsKey } from './utils/valueUtil'; import ResizeContext from './context/ResizeContext'; -import useStickyOffsets from './hooks/useStickyOffsets'; +import useStickyOffsets, { stickyOffsetCalculator } from './hooks/useStickyOffsets'; import ColGroup from './ColGroup'; import { getExpandableProps } from './utils/legacyUtil'; import Panel from './Panel'; @@ -641,6 +641,11 @@ function Table(props: TableProps { + const leftOffsets: number[] = []; + const rightOffsets: number[] = []; + let left = 0; + let right = 0; + + for (let start = 0; start < columnCount; start += 1) { + if (direction === 'rtl') { + // Left offset + rightOffsets[start] = right; + right += colWidths[start] || 0; + + // Right offset + const end = columnCount - start - 1; + leftOffsets[end] = left; + left += colWidths[end] || 0; + } else { + // Left offset + leftOffsets[start] = left; + left += colWidths[start] || 0; + + // Right offset + const end = columnCount - start - 1; + rightOffsets[end] = right; + right += colWidths[end] || 0; + } + } + + return { + left: leftOffsets, + right: rightOffsets, + }; +}; + /** * Get sticky column offset width */ function useStickyOffsets(colWidths: number[], columnCount: number, direction: 'ltr' | 'rtl') { - const stickyOffsets: StickyOffsets = useMemo(() => { - const leftOffsets: number[] = []; - const rightOffsets: number[] = []; - let left = 0; - let right = 0; - - for (let start = 0; start < columnCount; start += 1) { - if (direction === 'rtl') { - // Left offset - rightOffsets[start] = right; - right += colWidths[start] || 0; - - // Right offset - const end = columnCount - start - 1; - leftOffsets[end] = left; - left += colWidths[end] || 0; - } else { - // Left offset - leftOffsets[start] = left; - left += colWidths[start] || 0; - - // Right offset - const end = columnCount - start - 1; - rightOffsets[end] = right; - right += colWidths[end] || 0; - } - } - - return { - left: leftOffsets, - right: rightOffsets, - }; - }, [colWidths, columnCount, direction]); + const stickyOffsets: StickyOffsets = useMemo( + () => stickyOffsetCalculator(colWidths, columnCount, direction), + [colWidths, columnCount, direction], + ); return stickyOffsets; }