Skip to content

Commit 2f15e97

Browse files
committed
feat: rowKeys
1 parent 094972a commit 2f15e97

File tree

5 files changed

+25
-13
lines changed

5 files changed

+25
-13
lines changed

src/Body/BodyRow.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export interface BodyRowProps<RecordType> {
1919
scopeCellComponent: CustomizeComponent;
2020
indent?: number;
2121
rowKey: React.Key;
22-
getRowKey: (index: number) => React.Key;
22+
rowKeys: React.Key[];
2323
}
2424

2525
// ==================================================================================
@@ -31,7 +31,7 @@ export function getCellProps<RecordType>(
3131
colIndex: number,
3232
indent: number,
3333
index: number,
34-
getRowKey?: (index: number) => React.Key,
34+
rowKeys: React.Key[],
3535
) {
3636
const {
3737
record,
@@ -74,12 +74,12 @@ export function getCellProps<RecordType>(
7474
const addChildrenRowSpan = (rowSpan: number, index2: number) => {
7575
const _index = index2 + 1;
7676
let _rowSpan = rowSpan;
77-
const rowKey = getRowKey?.(_index);
77+
const rowKey = rowKeys[_index];
7878
if (rowKey !== undefined) {
7979
// 下面如果是 0 的,增加 +1 逻辑
8080
const thisCellProps = column.onCell(record, _index);
8181
if (thisCellProps.rowSpan === 0) {
82-
const thisExpanded = expandedKeys.has(getRowKey?.(_index));
82+
const thisExpanded = expandedKeys.has(rowKey);
8383
if (thisExpanded) {
8484
_rowSpan = _rowSpan + 1;
8585
}
@@ -133,7 +133,7 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
133133
rowComponent: RowComponent,
134134
cellComponent,
135135
scopeCellComponent,
136-
getRowKey,
136+
rowKeys,
137137
} = props;
138138
const rowInfo = useRowInfo(record, rowKey, index, indent);
139139

@@ -186,7 +186,7 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
186186
colIndex,
187187
indent,
188188
index,
189-
getRowKey,
189+
rowKeys,
190190
);
191191

192192
return (

src/Body/index.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,15 @@ function Body<RecordType>(props: BodyProps<RecordType>) {
6262

6363
let rows: React.ReactNode;
6464
if (data.length) {
65+
const rowKeys = flattenData.map(item => item.rowKey);
6566
rows = flattenData.map((item, idx) => {
6667
const { record, indent, index: renderIndex, rowKey } = item;
6768

6869
return (
6970
<BodyRow
7071
key={rowKey}
7172
rowKey={rowKey}
72-
getRowKey={index => {
73-
const thisRecord = flattenData[index];
74-
return thisRecord?.rowKey;
75-
}}
73+
rowKeys={rowKeys}
7674
record={record}
7775
index={idx}
7876
renderIndex={renderIndex}

src/VirtualTable/BodyGrid.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
9595

9696
return obj;
9797
});
98-
98+
const rowKeys = flattenData.map(item => item.rowKey);
9999
// ======================= Col/Row Span =======================
100100
const getRowSpan = (column: ColumnType<any>, index: number): number => {
101101
const record = flattenData[index]?.record;
@@ -185,6 +185,7 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
185185
key={index}
186186
data={item}
187187
rowKey={rowKey}
188+
rowKeys={rowKeys}
188189
index={index}
189190
style={{
190191
top: -offsetY + sizeInfo.top,
@@ -243,7 +244,15 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
243244
>
244245
{(item, index, itemProps) => {
245246
const rowKey = getRowKey(item.record, index);
246-
return <BodyLine data={item} rowKey={rowKey} index={index} style={itemProps.style} />;
247+
return (
248+
<BodyLine
249+
data={item}
250+
rowKey={rowKey}
251+
rowKeys={rowKeys}
252+
index={index}
253+
style={itemProps.style}
254+
/>
255+
);
247256
}}
248257
</VirtualList>
249258
</GridContext.Provider>

src/VirtualTable/BodyLine.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ export interface BodyLineProps<RecordType = any> {
1515
className?: string;
1616
style?: React.CSSProperties;
1717
rowKey: React.Key;
18+
rowKeys: React.Key[];
1819

1920
/** Render cell only when it has `rowSpan > 1` */
2021
extra?: boolean;
2122
getHeight?: (rowSpan: number) => number;
2223
}
2324

2425
const BodyLine = React.forwardRef<HTMLDivElement, BodyLineProps>((props, ref) => {
25-
const { data, index, className, rowKey, style, extra, getHeight, ...restProps } = props;
26+
const { data, index, className, rowKey, style, extra, getHeight, rowKeys, ...restProps } = props;
2627
const { record, indent, index: renderIndex } = data;
2728

2829
const { scrollX, flattenColumns, prefixCls, fixColumn, componentWidth } = useContext(
@@ -114,6 +115,7 @@ const BodyLine = React.forwardRef<HTMLDivElement, BodyLineProps>((props, ref) =>
114115
record={record}
115116
inverse={extra}
116117
getHeight={getHeight}
118+
rowKeys={rowKeys}
117119
/>
118120
);
119121
})}

src/VirtualTable/VirtualCell.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface VirtualCellProps<RecordType> {
2525
/** Render cell only when it has `rowSpan > 1` */
2626
inverse?: boolean;
2727
getHeight?: (rowSpan: number) => number;
28+
rowKeys: React.Key[];
2829
}
2930

3031
/**
@@ -50,6 +51,7 @@ function VirtualCell<RecordType = any>(props: VirtualCellProps<RecordType>) {
5051
className,
5152
inverse,
5253
getHeight,
54+
rowKeys,
5355
} = props;
5456

5557
const { render, dataIndex, className: columnClassName, width: colWidth } = column;
@@ -62,6 +64,7 @@ function VirtualCell<RecordType = any>(props: VirtualCellProps<RecordType>) {
6264
colIndex,
6365
indent,
6466
index,
67+
rowKeys,
6568
);
6669

6770
const { style: cellStyle, colSpan = 1, rowSpan = 1 } = additionalCellProps;

0 commit comments

Comments
 (0)