Skip to content

Commit 42c37bd

Browse files
authored
fix: 7.x header blank when using sticky or scroll.y (#1268)
1 parent a056eaa commit 42c37bd

File tree

5 files changed

+423
-345
lines changed

5 files changed

+423
-345
lines changed

src/Body/MeasureCell.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as React from 'react';
22
import ResizeObserver from 'rc-resize-observer';
3+
import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect';
34

45
export interface MeasureCellProps {
56
columnKey: React.Key;
@@ -9,7 +10,7 @@ export interface MeasureCellProps {
910
export default function MeasureCell({ columnKey, onColumnResize }: MeasureCellProps) {
1011
const cellRef = React.useRef<HTMLTableCellElement>();
1112

12-
React.useEffect(() => {
13+
useLayoutEffect(() => {
1314
if (cellRef.current) {
1415
onColumnResize(columnKey, cellRef.current.offsetWidth);
1516
}

src/Body/MeasureRow.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react';
22
import ResizeObserver from 'rc-resize-observer';
33
import MeasureCell from './MeasureCell';
4+
import isVisible from 'rc-util/lib/Dom/isVisible';
45

56
export interface MeasureCellProps {
67
prefixCls: string;
@@ -9,17 +10,22 @@ export interface MeasureCellProps {
910
}
1011

1112
export default function MeasureRow({ prefixCls, columnsKey, onColumnResize }: MeasureCellProps) {
13+
const ref = React.useRef<HTMLTableRowElement>(null);
14+
1215
return (
1316
<tr
1417
aria-hidden="true"
1518
className={`${prefixCls}-measure-row`}
1619
style={{ height: 0, fontSize: 0 }}
20+
ref={ref}
1721
>
1822
<ResizeObserver.Collection
1923
onBatchResize={infoList => {
20-
infoList.forEach(({ data: columnKey, size }) => {
21-
onColumnResize(columnKey, size.offsetWidth);
22-
});
24+
if (isVisible(ref.current)) {
25+
infoList.forEach(({ data: columnKey, size }) => {
26+
onColumnResize(columnKey, size.offsetWidth);
27+
});
28+
}
2329
}}
2430
>
2531
{columnsKey.map(columnKey => (

src/Table.tsx

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import type { CompareProps } from '@rc-component/context/lib/Immutable';
2828
import classNames from 'classnames';
2929
import ResizeObserver from 'rc-resize-observer';
30-
import isVisible from 'rc-util/lib/Dom/isVisible';
3130
import { isStyleSupport } from 'rc-util/lib/Dom/styleChecker';
3231
import { getTargetScrollBarSize } from 'rc-util/lib/getScrollBarSize';
3332
import useEvent from 'rc-util/lib/hooks/useEvent';
@@ -48,7 +47,7 @@ import Header from './Header/Header';
4847
import useColumns from './hooks/useColumns';
4948
import useExpand from './hooks/useExpand';
5049
import useFixedInfo from './hooks/useFixedInfo';
51-
import { useLayoutState, useTimeoutLock } from './hooks/useFrame';
50+
import { useTimeoutLock } from './hooks/useFrame';
5251
import useHover from './hooks/useHover';
5352
import useSticky from './hooks/useSticky';
5453
import useStickyOffsets from './hooks/useStickyOffsets';
@@ -76,6 +75,7 @@ import Column from './sugar/Column';
7675
import ColumnGroup from './sugar/ColumnGroup';
7776
import { getColumnsKey, validateValue, validNumberValue } from './utils/valueUtil';
7877
import { getDOM } from 'rc-util/lib/Dom/findDOMNode';
78+
import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect';
7979

8080
export const DEFAULT_PREFIX = 'rc-table';
8181

@@ -349,7 +349,7 @@ function Table<RecordType extends DefaultRecordType>(
349349
const scrollSummaryRef = React.useRef<HTMLDivElement>();
350350
const [pingedLeft, setPingedLeft] = React.useState(false);
351351
const [pingedRight, setPingedRight] = React.useState(false);
352-
const [colsWidths, updateColsWidths] = useLayoutState(new Map<React.Key, number>());
352+
const [colsWidths, updateColsWidths] = React.useState(new Map<React.Key, number>());
353353

354354
// Convert map to number width
355355
const colsKeys = getColumnsKey(flattenColumns);
@@ -403,16 +403,14 @@ function Table<RecordType extends DefaultRecordType>(
403403
}
404404

405405
const onColumnResize = React.useCallback((columnKey: React.Key, width: number) => {
406-
if (isVisible(fullTableRef.current)) {
407-
updateColsWidths(widths => {
408-
if (widths.get(columnKey) !== width) {
409-
const newWidths = new Map(widths);
410-
newWidths.set(columnKey, width);
411-
return newWidths;
412-
}
413-
return widths;
414-
});
415-
}
406+
updateColsWidths(widths => {
407+
if (widths.get(columnKey) !== width) {
408+
const newWidths = new Map(widths);
409+
newWidths.set(columnKey, width);
410+
return newWidths;
411+
}
412+
return widths;
413+
});
416414
}, []);
417415

418416
const [setScrollTarget, getScrollTarget] = useTimeoutLock(null);
@@ -524,7 +522,7 @@ function Table<RecordType extends DefaultRecordType>(
524522
const [scrollbarSize, setScrollbarSize] = React.useState(0);
525523
const [supportSticky, setSupportSticky] = React.useState(true); // Only IE not support, we mark as support first
526524

527-
React.useEffect(() => {
525+
useLayoutEffect(() => {
528526
if (!tailor || !useInternalHooks) {
529527
if (scrollBodyRef.current instanceof Element) {
530528
setScrollbarSize(getTargetScrollBarSize(scrollBodyRef.current).width);

0 commit comments

Comments
 (0)