Skip to content
Closed
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
17 changes: 13 additions & 4 deletions src/Body/MeasureCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@ import useLayoutEffect from '@rc-component/util/lib/hooks/useLayoutEffect';
export interface MeasureCellProps {
columnKey: React.Key;
onColumnResize: (key: React.Key, width: number) => void;
title?: React.ReactNode;
columnIndex?: number;
}

const MeasureCell: React.FC<MeasureCellProps> = props => {
const { columnKey, onColumnResize, title } = props;

const { columnKey, onColumnResize, columnIndex } = props;
const [width, setWidth] = React.useState(0);
const cellRef = React.useRef<HTMLTableCellElement>(null);

useLayoutEffect(() => {
if (columnIndex !== undefined) {
setWidth(
document
.querySelectorAll('.rc-table-thead >tr > .rc-table-cell')
[columnIndex]?.getClientRects()[0].width || 0,
);
}
if (cellRef.current) {
onColumnResize(columnKey, cellRef.current.offsetWidth);
}
Expand All @@ -25,7 +32,9 @@ const MeasureCell: React.FC<MeasureCellProps> = props => {
ref={cellRef}
style={{ paddingTop: 0, paddingBottom: 0, borderTop: 0, borderBottom: 0, height: 0 }}
>
<div style={{ height: 0, overflow: 'hidden', fontWeight: 'bold' }}>{title || '\xa0'}</div>
<div style={{ height: 0, overflow: 'hidden', fontWeight: 'bold', width: `${width}px` }}>
{'\xa0'}
</div>
</td>
</ResizeObserver>
);
Expand Down
4 changes: 2 additions & 2 deletions src/Body/MeasureRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ const MeasureRow: React.FC<MeasureRowProps> = ({
}}
>
{columnsKey.map(columnKey => {
const column = columns.find(col => col.key === columnKey);
const columnIndex = columns.findIndex(col => col.key === columnKey);
return (
<MeasureCell
key={columnKey}
columnIndex={columnIndex}
columnKey={columnKey}
onColumnResize={onColumnResize}
title={column?.title}
/>
Comment on lines 40 to 45
Copy link
Contributor

Choose a reason for hiding this comment

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

high

为了让 MeasureCell 能够构建正确的 DOM 选择器,而不是使用硬编码的类名,建议将 prefixCls 属性传递给它。这能显著提高 MeasureCell 组件的健壮性。

            <MeasureCell
              key={columnKey}
              prefixCls={prefixCls}
              columnIndex={columnIndex}
              columnKey={columnKey}
              onColumnResize={onColumnResize}
            />

Comment on lines 37 to 45
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

findIndex 容易失配(列 key 可能缺失或规则不同),直接使用 map 的 index 作为列序更稳,并下传 prefixCls

当列未显式设置 key 或内部生成规则不同,findIndex 可能返回 -1,导致测量失败。使用遍历索引与表头叶子列顺序一致,更可靠。

-        {columnsKey.map(columnKey => {
-          const columnIndex = columns.findIndex(col => col.key === columnKey);
-          return (
-            <MeasureCell
-              key={columnKey}
-              columnIndex={columnIndex}
-              columnKey={columnKey}
-              onColumnResize={onColumnResize}
-            />
-          );
-        })}
+        {columnsKey.map((columnKey, columnIndex) => (
+          <MeasureCell
+            key={columnKey}
+            columnIndex={columnIndex}
+            prefixCls={prefixCls}
+            columnKey={columnKey}
+            onColumnResize={onColumnResize}
+          />
+        ))}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
{columnsKey.map(columnKey => {
const column = columns.find(col => col.key === columnKey);
const columnIndex = columns.findIndex(col => col.key === columnKey);
return (
<MeasureCell
key={columnKey}
columnIndex={columnIndex}
columnKey={columnKey}
onColumnResize={onColumnResize}
title={column?.title}
/>
{columnsKey.map((columnKey, columnIndex) => (
<MeasureCell
key={columnKey}
columnIndex={columnIndex}
prefixCls={prefixCls}
columnKey={columnKey}
onColumnResize={onColumnResize}
/>
))}
🤖 Prompt for AI Agents
In src/Body/MeasureRow.tsx around lines 37 to 45, using columns.findIndex(col =>
col.key === columnKey) can return -1 when keys are missing or differ; change to
use the map iteration index (the second argument of map) as the stable
columnIndex to match header leaf order, and pass the component's prefixCls down
to MeasureCell (add prefixCls={prefixCls}) so styling/namespace is preserved.
Ensure you replace columnIndex assignment with the map index, remove reliance on
findIndex, and include prefixCls in the MeasureCell props.

);
})}
Expand Down