Skip to content

Commit ba03688

Browse files
committed
refactor: row in
1 parent 3d35faf commit ba03688

File tree

6 files changed

+34
-29
lines changed

6 files changed

+34
-29
lines changed

src/Body/BodyRow.tsx

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as React from 'react';
33
import Cell from '../Cell';
44
import { responseImmutable } from '../context/TableContext';
55
import devRenderTimes from '../hooks/useRenderTimes';
6-
import type { ColumnType, CustomizeComponent, GetComponentProps, GetRowKey } from '../interface';
6+
import type { ColumnType, CustomizeComponent, GetRowKey } from '../interface';
77
import ExpandedRow from './ExpandedRow';
88
import useRowInfo from '../hooks/useRowInfo';
99

@@ -16,7 +16,6 @@ export interface BodyRowProps<RecordType> {
1616
rowComponent: CustomizeComponent;
1717
cellComponent: CustomizeComponent;
1818
scopeCellComponent: CustomizeComponent;
19-
onRow: GetComponentProps<RecordType>;
2019
indent?: number;
2120
rowKey: React.Key;
2221
getRowKey: GetRowKey<RecordType>;
@@ -99,26 +98,23 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
9998
index,
10099
renderIndex,
101100
rowKey,
102-
onRow,
103101
indent = 0,
104102
rowComponent: RowComponent,
105103
cellComponent,
106104
scopeCellComponent,
107105
} = props;
108-
const rowInfo = useRowInfo(record, rowKey);
106+
const rowInfo = useRowInfo(record, rowKey, index);
109107
const {
110108
prefixCls,
111109
flattenColumns,
112-
expandRowByClick,
113-
onTriggerExpand,
114110
rowClassName,
115111
expandedRowClassName,
116112
expandedRowRender,
113+
rowProps,
117114

118115
// Misc
119116
expanded,
120117
rowSupportExpand,
121-
expandable,
122118
} = rowInfo;
123119

124120
const [expandRended, setExpandRended] = React.useState(false);
@@ -133,17 +129,6 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
133129
}
134130
}, [expanded]);
135131

136-
// =========================== onRow ===========================
137-
const additionalProps = onRow?.(record, index);
138-
139-
const onClick: React.MouseEventHandler<HTMLElement> = (event, ...args) => {
140-
if (expandRowByClick && expandable) {
141-
onTriggerExpand(record, event);
142-
}
143-
144-
additionalProps?.onClick?.(event, ...args);
145-
};
146-
147132
// ======================== Base tr row ========================
148133
let computeRowClassName: string;
149134
if (typeof rowClassName === 'string') {
@@ -154,20 +139,19 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
154139

155140
const baseRowNode = (
156141
<RowComponent
157-
{...additionalProps}
142+
{...rowProps}
158143
data-row-key={rowKey}
159144
className={classNames(
160145
className,
161146
`${prefixCls}-row`,
162147
`${prefixCls}-row-level-${indent}`,
163148
computeRowClassName,
164-
additionalProps && additionalProps.className,
149+
rowProps?.className,
165150
)}
166151
style={{
167152
...style,
168-
...(additionalProps ? additionalProps.style : null),
153+
...rowProps?.style,
169154
}}
170-
onClick={onClick}
171155
>
172156
{flattenColumns.map((column: ColumnType<RecordType>, colIndex) => {
173157
const { render, dataIndex, className: columnClassName } = column;

src/Body/index.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import PerfContext from '../context/PerfContext';
55
import TableContext, { responseImmutable } from '../context/TableContext';
66
import useFlattenRecords from '../hooks/useFlattenRecords';
77
import devRenderTimes from '../hooks/useRenderTimes';
8-
import type { GetComponentProps } from '../interface';
98
import { getColumnsKey } from '../utils/valueUtil';
109
import BodyRow from './BodyRow';
1110
import ExpandedRow from './ExpandedRow';
@@ -14,7 +13,6 @@ import MeasureRow from './MeasureRow';
1413
export interface BodyProps<RecordType> {
1514
data: readonly RecordType[];
1615
measureColumnWidth: boolean;
17-
onRow: GetComponentProps<RecordType>;
1816
emptyNode: React.ReactNode;
1917
}
2018

@@ -23,7 +21,7 @@ function Body<RecordType>(props: BodyProps<RecordType>) {
2321
devRenderTimes(props);
2422
}
2523

26-
const { data, measureColumnWidth, onRow, emptyNode } = props;
24+
const { data, measureColumnWidth, emptyNode } = props;
2725

2826
const {
2927
prefixCls,
@@ -74,7 +72,6 @@ function Body<RecordType>(props: BodyProps<RecordType>) {
7472
rowComponent={trComponent}
7573
cellComponent={tdComponent}
7674
scopeCellComponent={thComponent}
77-
onRow={onRow}
7875
getRowKey={getRowKey}
7976
indent={indent}
8077
/>

src/StaticTable/BodyLine.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const BodyLine = React.forwardRef<HTMLDivElement, BodyLineProps>((props, ref) =>
3131
]);
3232
const { scrollX } = useContext(StaticContext, ['scrollX']);
3333

34-
const rowInfo = useRowInfo(record, rowKey);
34+
const rowInfo = useRowInfo(record, rowKey, index);
3535

3636
// ========================== Expand ==========================
3737
const { rowSupportExpand, expanded } = rowInfo;

src/Table.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,6 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
537537
<Body
538538
data={mergedData}
539539
measureColumnWidth={fixHeader || horizonScroll || isSticky}
540-
onRow={onRow}
541540
emptyNode={emptyNode}
542541
/>
543542
);
@@ -748,7 +747,6 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
748747
horizonScroll,
749748

750749
// Body
751-
752750
tableLayout: mergedTableLayout,
753751
rowClassName,
754752
expandedRowClassName: expandableConfig.expandedRowClassName,
@@ -771,6 +769,7 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
771769
hoverEndRow: endRow,
772770
onHover,
773771
rowExpandable: expandableConfig.rowExpandable,
772+
onRow,
774773

775774
getRowKey,
776775
expandedKeys: mergedExpandedKeys,
@@ -813,6 +812,7 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
813812
endRow,
814813
onHover,
815814
expandableConfig.rowExpandable,
815+
onRow,
816816

817817
getRowKey,
818818
mergedExpandedKeys,

src/context/TableContext.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
ExpandableType,
77
ExpandedRowRender,
88
GetComponent,
9+
GetComponentProps,
910
GetRowKey,
1011
RenderExpandIcon,
1112
RowClassName,
@@ -34,6 +35,7 @@ export interface TableContextProps<RecordType = any> {
3435
// Body
3536
rowClassName: string | RowClassName<RecordType>;
3637
expandedRowClassName: RowClassName<RecordType>;
38+
onRow?: GetComponentProps<RecordType>;
3739

3840
tableLayout: TableLayout;
3941

src/hooks/useRowInfo.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { useEvent } from 'rc-util';
77
export default function useRowInfo<RecordType>(
88
record: RecordType,
99
rowKey: React.Key,
10+
recordIndex: number,
1011
): Pick<
1112
TableContextProps,
1213
| 'prefixCls'
@@ -23,6 +24,7 @@ export default function useRowInfo<RecordType>(
2324
| 'expandIconColumnIndex'
2425
| 'expandedKeys'
2526
| 'childrenColumnName'
27+
| 'onRow'
2628
> & {
2729
columnsKey: React.Key[];
2830
nestExpandable: boolean;
@@ -31,6 +33,7 @@ export default function useRowInfo<RecordType>(
3133
record: RecordType;
3234
rowSupportExpand: boolean;
3335
expandable: boolean;
36+
rowProps: React.HTMLAttributes<any> & React.TdHTMLAttributes<any>;
3437
} {
3538
const context: TableContextProps = useContext(TableContext, [
3639
'prefixCls',
@@ -48,6 +51,7 @@ export default function useRowInfo<RecordType>(
4851
'expandedKeys',
4952
'childrenColumnName',
5053
'rowExpandable',
54+
'onRow',
5155
]);
5256

5357
const {
@@ -57,6 +61,8 @@ export default function useRowInfo<RecordType>(
5761
childrenColumnName,
5862
onTriggerExpand,
5963
rowExpandable,
64+
onRow,
65+
expandRowByClick,
6066
} = context;
6167

6268
// ======================= Expandable =======================
@@ -72,6 +78,18 @@ export default function useRowInfo<RecordType>(
7278

7379
const onInternalTriggerExpand = useEvent(onTriggerExpand);
7480

81+
// ========================= onRow ==========================
82+
const rowProps = onRow?.(record, recordIndex);
83+
const onRowClick = rowProps?.onClick;
84+
85+
const onClick: React.MouseEventHandler<HTMLElement> = (event, ...args) => {
86+
if (expandRowByClick && mergedExpandable) {
87+
onTriggerExpand(record, event);
88+
}
89+
90+
onRowClick?.(event, ...args);
91+
};
92+
7593
// ========================= Column =========================
7694
const columnsKey = getColumnsKey(flattenColumns);
7795

@@ -85,5 +103,9 @@ export default function useRowInfo<RecordType>(
85103
onTriggerExpand: onInternalTriggerExpand,
86104
rowSupportExpand,
87105
expandable: mergedExpandable,
106+
rowProps: {
107+
...rowProps,
108+
onClick,
109+
},
88110
};
89111
}

0 commit comments

Comments
 (0)