Skip to content

Commit a63d6a7

Browse files
committed
feat: 自动 offset
1 parent 0bcf3bf commit a63d6a7

File tree

6 files changed

+43
-34
lines changed

6 files changed

+43
-34
lines changed

docs/examples/expandedSticky.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const Demo = () => {
77
const [expandedRowKeys, setExpandedRowKeys] = useState<readonly React.Key[]>([]);
88

99
const columns: ColumnType<Record<string, any>>[] = [
10+
// { title: '分割', dataIndex: 'ca' },
1011
{
1112
title: '手机号',
1213
dataIndex: 'a',
@@ -43,7 +44,6 @@ const Demo = () => {
4344
{ key: 'c', a: '13812340987', c: '张夫', d: '文二西路' },
4445
]}
4546
expandable={{
46-
expandedRowOffset: 1,
4747
expandedRowKeys,
4848
onExpandedRowsChange: keys => setExpandedRowKeys(keys),
4949
expandedRowRender: record => <p style={{ margin: 0 }}>{record.key}</p>,

src/Body/BodyRow.tsx

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Cell from '../Cell';
44
import { responseImmutable } from '../context/TableContext';
55
import devRenderTimes from '../hooks/useRenderTimes';
66
import useRowInfo from '../hooks/useRowInfo';
7-
import type { ColumnType, CustomizeComponent, ExpandableConfig } from '../interface';
7+
import type { ColumnType, CustomizeComponent } from '../interface';
88
import ExpandedRow from './ExpandedRow';
99
import { computedExpandedClassName } from '../utils/expandUtil';
1010
import type { TableProps } from '..';
@@ -23,7 +23,6 @@ export interface BodyRowProps<RecordType> {
2323
indent?: number;
2424
rowKey: React.Key;
2525
rowKeys: React.Key[];
26-
expandedRowOffset?: ExpandableConfig<RecordType>['expandedRowOffset'];
2726
}
2827

2928
// ==================================================================================
@@ -104,9 +103,31 @@ export function getCellProps<RecordType>(
104103
};
105104
}
106105

107-
// ==================================================================================
108-
// == getCellProps ==
109-
// ==================================================================================
106+
const getOffsetData = (
107+
columnsData: {
108+
column: ColumnType<any>;
109+
cell: { additionalCellProps: React.TdHTMLAttributes<HTMLElement> };
110+
}[],
111+
) => {
112+
let offsetWidth = 0;
113+
let offsetColumn = 0;
114+
let isRowSpanEnd = false;
115+
columnsData.forEach(item => {
116+
if (!isRowSpanEnd) {
117+
const { column, cell } = item;
118+
if (cell.additionalCellProps.rowSpan !== undefined) {
119+
offsetColumn += 1;
120+
if (typeof column.width === 'number') {
121+
offsetWidth = offsetWidth + (column.width ?? 0);
122+
}
123+
} else {
124+
isRowSpanEnd = true;
125+
}
126+
}
127+
});
128+
return { offsetWidth, offsetColumn };
129+
};
130+
110131
function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
111132
props: BodyRowProps<RecordType>,
112133
) {
@@ -127,7 +148,6 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
127148
rowComponent: RowComponent,
128149
cellComponent,
129150
scopeCellComponent,
130-
expandedRowOffset = 0,
131151
rowKeys,
132152
} = props;
133153

@@ -157,6 +177,17 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
157177
// 此时如果 level > 1 则说明是 expandedRow, 一样需要附加 computedExpandedRowClassName
158178
const expandedClsName = computedExpandedClassName(expandedRowClassName, record, index, indent);
159179

180+
const { columnsData, offsetData } = React.useMemo(() => {
181+
// eslint-disable-next-line @typescript-eslint/no-shadow
182+
const columnsData = flattenColumns.map((column: ColumnType<RecordType>, colIndex) => {
183+
const cell = getCellProps(rowInfo, column, colIndex, indent, index, rowKeys);
184+
return { column, cell };
185+
});
186+
// eslint-disable-next-line @typescript-eslint/no-shadow
187+
const offsetData = getOffsetData(columnsData);
188+
return { columnsData, offsetData };
189+
}, [flattenColumns, indent, index, rowInfo, rowKeys]);
190+
160191
// ======================== Base tr row ========================
161192
const baseRowNode = (
162193
<RowComponent
@@ -178,17 +209,11 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
178209
...styles.row,
179210
}}
180211
>
181-
{flattenColumns.map((column: ColumnType<RecordType>, colIndex) => {
212+
{columnsData.map(item => {
213+
const { column, cell } = item;
182214
const { render, dataIndex, className: columnClassName } = column;
183215

184-
const { key, fixedInfo, appendCellNode, additionalCellProps } = getCellProps(
185-
rowInfo,
186-
column,
187-
colIndex,
188-
indent,
189-
index,
190-
rowKeys,
191-
);
216+
const { key, fixedInfo, appendCellNode, additionalCellProps } = cell;
192217

193218
return (
194219
<Cell<RecordType>
@@ -220,14 +245,6 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
220245
if (rowSupportExpand && (expandedRef.current || expanded)) {
221246
const expandContent = expandedRowRender(record, index, indent + 1, expanded);
222247

223-
const offsetColumns = flattenColumns.filter((_, idx) => idx < expandedRowOffset);
224-
let offsetWidth = 0;
225-
offsetColumns.forEach(item => {
226-
if (typeof item.width === 'number') {
227-
offsetWidth = offsetWidth + (item.width ?? 0);
228-
}
229-
});
230-
231248
expandRowNode = (
232249
<ExpandedRow
233250
expanded={expanded}
@@ -239,8 +256,8 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
239256
prefixCls={prefixCls}
240257
component={RowComponent}
241258
cellComponent={cellComponent}
242-
offsetWidth={offsetWidth}
243-
colSpan={flattenColumns.length - expandedRowOffset}
259+
offsetWidth={offsetData.offsetWidth}
260+
colSpan={flattenColumns.length - offsetData.offsetColumn}
244261
isEmpty={false}
245262
>
246263
{expandContent}

src/Body/index.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ function Body<RecordType>(props: BodyProps<RecordType>) {
3232
expandedKeys,
3333
childrenColumnName,
3434
emptyNode,
35-
expandedRowOffset,
3635
classNames,
3736
styles,
3837
} = useContext(TableContext, [
@@ -44,7 +43,6 @@ function Body<RecordType>(props: BodyProps<RecordType>) {
4443
'expandedKeys',
4544
'childrenColumnName',
4645
'emptyNode',
47-
'expandedRowOffset',
4846
'classNames',
4947
'styles',
5048
]);
@@ -90,7 +88,6 @@ function Body<RecordType>(props: BodyProps<RecordType>) {
9088
cellComponent={tdComponent}
9189
scopeCellComponent={thComponent}
9290
indent={indent}
93-
expandedRowOffset={expandedRowOffset}
9491
/>
9592
);
9693
});

src/Table.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,6 @@ function Table<RecordType extends DefaultRecordType>(
872872
expandedRowRender: expandableConfig.expandedRowRender,
873873
onTriggerExpand,
874874
expandIconColumnIndex: expandableConfig.expandIconColumnIndex,
875-
expandedRowOffset: expandableConfig.expandedRowOffset,
876875
indentSize: expandableConfig.indentSize,
877876
allColumnsFixedLeft: flattenColumns.every(col => col.fixed === 'start'),
878877
emptyNode,
@@ -921,7 +920,6 @@ function Table<RecordType extends DefaultRecordType>(
921920
expandableType,
922921
expandableConfig.expandRowByClick,
923922
expandableConfig.expandedRowRender,
924-
expandableConfig.expandedRowOffset,
925923
onTriggerExpand,
926924
expandableConfig.expandIconColumnIndex,
927925
expandableConfig.indentSize,

src/context/TableContext.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type {
33
ColumnsType,
44
ColumnType,
55
Direction,
6-
ExpandableConfig,
76
ExpandableType,
87
ExpandedRowRender,
98
GetComponent,
@@ -56,7 +55,6 @@ export interface TableContextProps<RecordType = any> {
5655
expandIcon: RenderExpandIcon<RecordType>;
5756
onTriggerExpand: TriggerEventHandler<RecordType>;
5857
expandIconColumnIndex: number;
59-
expandedRowOffset: ExpandableConfig<RecordType>['expandedRowOffset'];
6058
allColumnsFixedLeft: boolean;
6159

6260
// Column

src/interface.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ export interface ExpandableConfig<RecordType> {
256256
rowExpandable?: (record: RecordType) => boolean;
257257
columnWidth?: number | string;
258258
fixed?: FixedType;
259-
expandedRowOffset?: number;
260259
}
261260

262261
// =================== Render ===================

0 commit comments

Comments
 (0)