Skip to content

Commit 99b1309

Browse files
committed
chore: optimize codes logic
1 parent 04091fc commit 99b1309

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

src/Cell/index.tsx

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,16 +182,19 @@ function Cell<RecordType>(
182182
additionalProps?.onMouseLeave?.(event);
183183
});
184184

185-
let mergedLastFixLeft = lastFixLeft;
186-
const { current } = headerCellRefs;
187-
const dom = current[colIndex];
188-
if (lastFixLeft && dom) {
189-
const offsetLeft =
190-
dom.getBoundingClientRect().x - dom.parentElement.getBoundingClientRect().x || 0;
191-
192-
// should not be tagged as lastFixLeft if cell is not stickying;
193-
mergedLastFixLeft = typeof fixLeft === 'number' && offsetLeft === fixLeft + bodyScrollLeft;
194-
}
185+
const mergedLastFixLeft = React.useMemo(() => {
186+
const { current } = headerCellRefs;
187+
const dom = current[colIndex];
188+
189+
if (lastFixLeft && dom && typeof fixLeft === 'number') {
190+
const offsetLeft =
191+
dom.getBoundingClientRect().x - dom.parentElement.getBoundingClientRect().x || 0;
192+
193+
// should not be tagged as lastFixLeft if cell is not stickying;
194+
return offsetLeft === fixLeft + bodyScrollLeft;
195+
}
196+
return lastFixLeft;
197+
}, [bodyScrollLeft, colIndex, fixLeft, headerCellRefs, lastFixLeft]);
195198
// ====================== Render ======================
196199
if (mergedColSpan === 0 || mergedRowSpan === 0) {
197200
return null;

src/Header/HeaderRow.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ const HeaderRow = <RecordType extends any>(props: RowProps<RecordType>) => {
4747

4848
const columnsKey = getColumnsKey(cells.map(cell => cell.column));
4949

50+
const handleHeaderCellRef =
51+
(cellIndex: number) => (headerCellRef: HTMLTableCellElement | null) => {
52+
headerCellRefs.current[cellIndex] = headerCellRef;
53+
};
54+
5055
return (
5156
<RowComponent {...rowProps}>
5257
{cells.map((cell: CellType<RecordType>, cellIndex) => {
@@ -67,7 +72,7 @@ const HeaderRow = <RecordType extends any>(props: RowProps<RecordType>) => {
6772
return (
6873
<Cell
6974
colIndex={cellIndex}
70-
ref={headerCellRef => (headerCellRefs.current[cellIndex] = headerCellRef)}
75+
ref={handleHeaderCellRef(cellIndex)}
7176
{...cell}
7277
scope={column.title ? (cell.colSpan > 1 ? 'colgroup' : 'col') : null}
7378
ellipsis={column.ellipsis}

0 commit comments

Comments
 (0)