Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -641,6 +641,11 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp

return 0;
}) as number[];
headerProps.stickyOffsets = stickyOffsetCalculator(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

่ฟ™ๆ ทๆฏๆฌก render ้ƒฝไผš่ฐƒ็”จไธ€ๆฌก stickyOffsetCalculator๏ผŒๆ€ง่ƒฝไผšๅทฎๅพˆๅคšใ€‚

headerProps.colWidths,
flattenColumns.length,
direction,
);
} else {
bodyContent = (
<div
Expand Down
75 changes: 42 additions & 33 deletions src/hooks/useStickyOffsets.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,52 @@
import { useMemo } from 'react';
import type { StickyOffsets } from '../interface';

export const stickyOffsetCalculator = (
colWidths: number[],
columnCount: number,
direction: 'ltr' | 'rtl',
) => {
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;
}
Expand Down