-
-
Notifications
You must be signed in to change notification settings - Fork 611
fix expanded & sticky #1277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix expanded & sticky #1277
Changes from all commits
4c0b03d
65cdf0b
e7f477a
3019a32
af0df67
e58aad0
f159a58
26db737
1dc05b8
1e9da7a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
title: expandedSticky | ||
nav: | ||
title: Demo | ||
path: /demo | ||
--- | ||
|
||
<code src="../examples/expandedSticky.tsx"></code> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import React, { useState } from 'react'; | ||
import type { ColumnType } from 'rc-table'; | ||
import Table from 'rc-table'; | ||
import '../../assets/index.less'; | ||
|
||
// 合并单元格 | ||
export const getRowSpan = (source: (string | number | undefined)[] = []) => { | ||
const list: { rowSpan?: number }[] = []; | ||
let span = 0; | ||
source.reverse().forEach((key, index) => { | ||
span = span + 1; | ||
if (key !== source[index + 1]) { | ||
list.push({ rowSpan: span }); | ||
span = 0; | ||
} else { | ||
list.push({ rowSpan: 0 }); | ||
} | ||
}); | ||
return list.reverse(); | ||
}; | ||
|
||
const Demo = () => { | ||
const [expandedRowKeys, setExpandedRowKeys] = useState<readonly React.Key[]>([]); | ||
|
||
const data = [ | ||
{ key: 'a', a: '小二', d: '文零西路' }, | ||
{ key: 'b', a: '张三', d: '文一西路' }, | ||
{ key: 'c', a: '张三', d: '文二西路' }, | ||
]; | ||
const rowKeys = data.map(item => item.key); | ||
|
||
const rowSpanList = getRowSpan(data.map(item => item.a)); | ||
|
||
const columns: ColumnType<Record<string, any>>[] = [ | ||
{ | ||
title: '手机号', | ||
dataIndex: 'a', | ||
width: 100, | ||
fixed: 'left', | ||
onCell: (_, index) => { | ||
const { rowSpan = 1 } = rowSpanList[index]; | ||
const props: React.TdHTMLAttributes<HTMLTableCellElement> = {}; | ||
props.rowSpan = rowSpan; | ||
if (rowSpan >= 1) { | ||
let currentRowSpan = rowSpan; | ||
for (let i = index; i < index + rowSpan; i += 1) { | ||
const rowKey = rowKeys[i]; | ||
if (expandedRowKeys.includes(rowKey)) { | ||
currentRowSpan += 1; | ||
} | ||
} | ||
props.rowSpan = currentRowSpan; | ||
} | ||
return props; | ||
}, | ||
}, | ||
Table.EXPAND_COLUMN, | ||
{ title: 'Address', fixed: 'right', dataIndex: 'd', width: 200 }, | ||
]; | ||
|
||
return ( | ||
<div style={{ height: 10000 }}> | ||
<h2>expanded & sticky</h2> | ||
<Table<Record<string, any>> | ||
rowKey="key" | ||
sticky | ||
scroll={{ x: 800 }} | ||
columns={columns} | ||
data={data} | ||
expandable={{ | ||
expandedRowOffset: 1, | ||
expandedRowKeys, | ||
onExpandedRowsChange: keys => setExpandedRowKeys(keys), | ||
expandedRowRender: record => <p style={{ margin: 0 }}>{record.key}</p>, | ||
}} | ||
className="table" | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Demo; |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -4,7 +4,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||
import { responseImmutable } from '../context/TableContext'; | ||||||||||||||||||||||||||||||||||||||||||||||
import devRenderTimes from '../hooks/useRenderTimes'; | ||||||||||||||||||||||||||||||||||||||||||||||
import useRowInfo from '../hooks/useRowInfo'; | ||||||||||||||||||||||||||||||||||||||||||||||
import type { ColumnType, CustomizeComponent } from '../interface'; | ||||||||||||||||||||||||||||||||||||||||||||||
import type { ColumnType, CustomizeComponent, ExpandableConfig } from '../interface'; | ||||||||||||||||||||||||||||||||||||||||||||||
import ExpandedRow from './ExpandedRow'; | ||||||||||||||||||||||||||||||||||||||||||||||
import { computedExpandedClassName } from '../utils/expandUtil'; | ||||||||||||||||||||||||||||||||||||||||||||||
import { TableProps } from '..'; | ||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -22,6 +22,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||
scopeCellComponent: CustomizeComponent; | ||||||||||||||||||||||||||||||||||||||||||||||
indent?: number; | ||||||||||||||||||||||||||||||||||||||||||||||
rowKey: React.Key; | ||||||||||||||||||||||||||||||||||||||||||||||
expandedRowOffset?: ExpandableConfig<RecordType>['expandedRowOffset']; | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
// ================================================================================== | ||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -107,6 +108,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||
rowComponent: RowComponent, | ||||||||||||||||||||||||||||||||||||||||||||||
cellComponent, | ||||||||||||||||||||||||||||||||||||||||||||||
scopeCellComponent, | ||||||||||||||||||||||||||||||||||||||||||||||
expandedRowOffset = 0, | ||||||||||||||||||||||||||||||||||||||||||||||
} = props; | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
const rowInfo = useRowInfo(record, rowKey, index, indent); | ||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -196,6 +198,14 @@ | |||||||||||||||||||||||||||||||||||||||||||||
if (rowSupportExpand && (expandedRef.current || expanded)) { | ||||||||||||||||||||||||||||||||||||||||||||||
const expandContent = expandedRowRender(record, index, indent + 1, expanded); | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
const offsetColumns = flattenColumns.filter((_, idx) => idx < expandedRowOffset); | ||||||||||||||||||||||||||||||||||||||||||||||
let offsetWidth = 0; | ||||||||||||||||||||||||||||||||||||||||||||||
offsetColumns.forEach(item => { | ||||||||||||||||||||||||||||||||||||||||||||||
if (typeof item.width === 'number') { | ||||||||||||||||||||||||||||||||||||||||||||||
offsetWidth = offsetWidth + (item.width ?? 0); | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+201
to
+207
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion 改进偏移宽度计算逻辑 当前的宽度计算实现存在几个问题:
建议改进为: - const offsetColumns = flattenColumns.filter((_, idx) => idx < expandedRowOffset);
- let offsetWidth = 0;
- offsetColumns.forEach(item => {
- if (typeof item.width === 'number') {
- offsetWidth = offsetWidth + (item.width ?? 0);
- }
- });
+ // 验证偏移值的有效性
+ const validOffset = Math.min(Math.max(0, expandedRowOffset), flattenColumns.length);
+ const offsetColumns = flattenColumns.slice(0, validOffset);
+
+ const offsetWidth = offsetColumns.reduce((total, column) => {
+ if (typeof column.width === 'number') {
+ return total + column.width;
+ }
+ // 处理字符串宽度(可选:解析 px 值)
+ if (typeof column.width === 'string' && column.width.endsWith('px')) {
+ const numValue = parseInt(column.width, 10);
+ return isNaN(numValue) ? total : total + numValue;
+ }
+ return total;
+ }, 0); 📝 Committable suggestion
Suggested change
🧰 Tools🪛 GitHub Check: codecov/patch[warning] 204-206: src/Body/BodyRow.tsx#L204-L206 🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
expandRowNode = ( | ||||||||||||||||||||||||||||||||||||||||||||||
<ExpandedRow | ||||||||||||||||||||||||||||||||||||||||||||||
expanded={expanded} | ||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -207,7 +217,8 @@ | |||||||||||||||||||||||||||||||||||||||||||||
prefixCls={prefixCls} | ||||||||||||||||||||||||||||||||||||||||||||||
component={RowComponent} | ||||||||||||||||||||||||||||||||||||||||||||||
cellComponent={cellComponent} | ||||||||||||||||||||||||||||||||||||||||||||||
colSpan={flattenColumns.length} | ||||||||||||||||||||||||||||||||||||||||||||||
offsetWidth={offsetWidth} | ||||||||||||||||||||||||||||||||||||||||||||||
colSpan={flattenColumns.length - expandedRowOffset} | ||||||||||||||||||||||||||||||||||||||||||||||
isEmpty={false} | ||||||||||||||||||||||||||||||||||||||||||||||
> | ||||||||||||||||||||||||||||||||||||||||||||||
{expandContent} | ||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
那个 rowSpan 的挪过来?
expandedRowOffset
如果没有那个合并的能力,这里其实是个半吊子There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
合也行,主要是另外哪个我还想做个模式,支持合并单元格是 + 1,还是分割
