Skip to content

Commit e2453e8

Browse files
committed
refactor: move useRowInfo in a single file
1 parent 03e33c9 commit e2453e8

File tree

7 files changed

+98
-79
lines changed

7 files changed

+98
-79
lines changed

docs/examples/virtual.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ const Demo = () => {
192192
scroll={{ x: 1200, y: scrollY ? 200 : null }}
193193
data={data}
194194
rowKey="indexKey"
195+
expandable={{}}
195196
/>
196197
</div>
197198
);

src/Body/BodyRow.tsx

Lines changed: 10 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { useContext } from '@rc-component/context';
22
import classNames from 'classnames';
3-
import { useEvent } from 'rc-util';
43
import * as React from 'react';
54
import Cell from '../Cell';
6-
import TableContext, { responseImmutable, type TableContextProps } from '../context/TableContext';
5+
import TableContext, { responseImmutable } from '../context/TableContext';
76
import devRenderTimes from '../hooks/useRenderTimes';
87
import type { ColumnType, CustomizeComponent, GetComponentProps, GetRowKey } from '../interface';
9-
import { getColumnsKey } from '../utils/valueUtil';
108
import ExpandedRow from './ExpandedRow';
9+
import useRowInfo from '../hooks/useRowInfo';
1110

1211
export interface BodyRowProps<RecordType> {
1312
record: RecordType;
@@ -19,80 +18,14 @@ export interface BodyRowProps<RecordType> {
1918
cellComponent: CustomizeComponent;
2019
scopeCellComponent: CustomizeComponent;
2120
onRow: GetComponentProps<RecordType>;
22-
rowExpandable: (record: RecordType) => boolean;
2321
indent?: number;
2422
rowKey: React.Key;
2523
getRowKey: GetRowKey<RecordType>;
2624
}
2725

28-
export function useRowInfo<RecordType>(
29-
record: RecordType,
30-
rowKey: React.Key,
31-
): Pick<
32-
TableContextProps,
33-
| 'prefixCls'
34-
| 'fixedInfoList'
35-
| 'flattenColumns'
36-
| 'expandableType'
37-
| 'expandRowByClick'
38-
| 'onTriggerExpand'
39-
| 'rowClassName'
40-
| 'expandedRowClassName'
41-
| 'indentSize'
42-
| 'expandIcon'
43-
| 'expandedRowRender'
44-
| 'expandIconColumnIndex'
45-
| 'expandedKeys'
46-
| 'childrenColumnName'
47-
> & {
48-
columnsKey: React.Key[];
49-
nestExpandable: boolean;
50-
expanded: boolean;
51-
hasNestChildren: boolean;
52-
record: RecordType;
53-
} {
54-
const context: TableContextProps = useContext(TableContext, [
55-
'prefixCls',
56-
'fixedInfoList',
57-
'flattenColumns',
58-
'expandableType',
59-
'expandRowByClick',
60-
'onTriggerExpand',
61-
'rowClassName',
62-
'expandedRowClassName',
63-
'indentSize',
64-
'expandIcon',
65-
'expandedRowRender',
66-
'expandIconColumnIndex',
67-
'expandedKeys',
68-
'childrenColumnName',
69-
]);
70-
71-
const { flattenColumns, expandableType, expandedKeys, childrenColumnName, onTriggerExpand } =
72-
context;
73-
74-
const columnsKey = getColumnsKey(flattenColumns);
75-
76-
// Only when row is not expandable and `children` exist in record
77-
const nestExpandable = expandableType === 'nest';
78-
79-
const expanded = expandedKeys && expandedKeys.has(rowKey);
80-
81-
const hasNestChildren = childrenColumnName && record && record[childrenColumnName];
82-
83-
const onInternalTriggerExpand = useEvent(onTriggerExpand);
84-
85-
return {
86-
...context,
87-
columnsKey,
88-
nestExpandable,
89-
expanded,
90-
hasNestChildren,
91-
record,
92-
onTriggerExpand: onInternalTriggerExpand,
93-
};
94-
}
95-
26+
// ==================================================================================
27+
// == getCellProps ==
28+
// ==================================================================================
9629
export function getCellProps<RecordType>(
9730
rowInfo: ReturnType<typeof useRowInfo<RecordType>>,
9831
column: ColumnType<RecordType>,
@@ -150,6 +83,9 @@ export function getCellProps<RecordType>(
15083
};
15184
}
15285

86+
// ==================================================================================
87+
// == getCellProps ==
88+
// ==================================================================================
15389
function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
15490
props: BodyRowProps<RecordType>,
15591
) {
@@ -164,7 +100,6 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
164100
index,
165101
renderIndex,
166102
rowKey,
167-
rowExpandable,
168103
onRow,
169104
indent = 0,
170105
rowComponent: RowComponent,
@@ -187,6 +122,8 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
187122
expanded,
188123
} = rowInfo;
189124

125+
const { rowExpandable } = useContext(TableContext, ['rowExpandable']);
126+
190127
const [expandRended, setExpandRended] = React.useState(false);
191128

192129
if (process.env.NODE_ENV !== 'production') {

src/Body/index.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export interface BodyProps<RecordType> {
1515
data: readonly RecordType[];
1616
measureColumnWidth: boolean;
1717
onRow: GetComponentProps<RecordType>;
18-
rowExpandable: (record: RecordType) => boolean;
1918
emptyNode: React.ReactNode;
2019
}
2120

@@ -24,7 +23,7 @@ function Body<RecordType>(props: BodyProps<RecordType>) {
2423
devRenderTimes(props);
2524
}
2625

27-
const { data, measureColumnWidth, onRow, rowExpandable, emptyNode } = props;
26+
const { data, measureColumnWidth, onRow, emptyNode } = props;
2827

2928
const {
3029
prefixCls,
@@ -77,7 +76,6 @@ function Body<RecordType>(props: BodyProps<RecordType>) {
7776
scopeCellComponent={thComponent}
7877
onRow={onRow}
7978
getRowKey={getRowKey}
80-
rowExpandable={rowExpandable}
8179
indent={indent}
8280
/>
8381
);

src/StaticTable/BodyLine.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { useContext } from '@rc-component/context';
22
import classNames from 'classnames';
33
import * as React from 'react';
4-
import { useRowInfo } from '../Body/BodyRow';
54
import TableContext, { responseImmutable } from '../context/TableContext';
65
import type { FlattenData } from '../hooks/useFlattenRecords';
76
import { StaticContext } from './context';
87
import VirtualCell from './VirtualCell';
8+
import useRowInfo from '../hooks/useRowInfo';
99

1010
export interface BodyLineProps<RecordType = any> {
1111
data: FlattenData<RecordType>;
@@ -23,11 +23,19 @@ const BodyLine = React.forwardRef<HTMLDivElement, BodyLineProps>((props, ref) =>
2323
const { data, index, className, rowKey, style, extra, getHeight, ...restProps } = props;
2424
const { record, indent } = data;
2525

26-
const { flattenColumns, prefixCls } = useContext(TableContext);
26+
const { flattenColumns, prefixCls, expandableType, rowExpandable } = useContext(TableContext, [
27+
'prefixCls',
28+
'flattenColumns',
29+
'expandableType',
30+
'rowExpandable',
31+
]);
2732
const { scrollX } = useContext(StaticContext, ['scrollX']);
2833

2934
const rowInfo = useRowInfo(record, rowKey);
3035

36+
// ========================== Expand ==========================
37+
const rowSupportExpand = expandableType === 'row' && (!rowExpandable || rowExpandable(record));
38+
3139
// ========================== Render ==========================
3240

3341
const rowStyle: React.CSSProperties = {

src/Table.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,6 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
535535
<Body
536536
data={mergedData}
537537
measureColumnWidth={fixHeader || horizonScroll || isSticky}
538-
rowExpandable={expandableConfig.rowExpandable}
539538
onRow={onRow}
540539
emptyNode={emptyNode}
541540
/>
@@ -769,6 +768,7 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
769768
hoverStartRow: startRow,
770769
hoverEndRow: endRow,
771770
onHover,
771+
rowExpandable: expandableConfig.rowExpandable,
772772

773773
getRowKey,
774774
expandedKeys: mergedExpandedKeys,
@@ -810,6 +810,7 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
810810
startRow,
811811
endRow,
812812
onHover,
813+
expandableConfig.rowExpandable,
813814

814815
getRowKey,
815816
mergedExpandedKeys,

src/context/TableContext.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export interface TableContextProps<RecordType = any> {
5555
hoverStartRow: number;
5656
hoverEndRow: number;
5757
onHover: (start: number, end: number) => void;
58+
rowExpandable: (record: RecordType) => boolean;
5859

5960
expandedKeys: Set<React.Key>;
6061
getRowKey: GetRowKey<RecordType>;

src/hooks/useRowInfo.tsx

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { useContext } from '@rc-component/context';
2+
import type { TableContextProps } from '../context/TableContext';
3+
import TableContext from '../context/TableContext';
4+
import { getColumnsKey } from '../utils/valueUtil';
5+
import { useEvent } from 'rc-util';
6+
7+
export default function useRowInfo<RecordType>(
8+
record: RecordType,
9+
rowKey: React.Key,
10+
): Pick<
11+
TableContextProps,
12+
| 'prefixCls'
13+
| 'fixedInfoList'
14+
| 'flattenColumns'
15+
| 'expandableType'
16+
| 'expandRowByClick'
17+
| 'onTriggerExpand'
18+
| 'rowClassName'
19+
| 'expandedRowClassName'
20+
| 'indentSize'
21+
| 'expandIcon'
22+
| 'expandedRowRender'
23+
| 'expandIconColumnIndex'
24+
| 'expandedKeys'
25+
| 'childrenColumnName'
26+
> & {
27+
columnsKey: React.Key[];
28+
nestExpandable: boolean;
29+
expanded: boolean;
30+
hasNestChildren: boolean;
31+
record: RecordType;
32+
} {
33+
const context: TableContextProps = useContext(TableContext, [
34+
'prefixCls',
35+
'fixedInfoList',
36+
'flattenColumns',
37+
'expandableType',
38+
'expandRowByClick',
39+
'onTriggerExpand',
40+
'rowClassName',
41+
'expandedRowClassName',
42+
'indentSize',
43+
'expandIcon',
44+
'expandedRowRender',
45+
'expandIconColumnIndex',
46+
'expandedKeys',
47+
'childrenColumnName',
48+
]);
49+
50+
const { flattenColumns, expandableType, expandedKeys, childrenColumnName, onTriggerExpand } =
51+
context;
52+
53+
const columnsKey = getColumnsKey(flattenColumns);
54+
55+
// Only when row is not expandable and `children` exist in record
56+
const nestExpandable = expandableType === 'nest';
57+
58+
const expanded = expandedKeys && expandedKeys.has(rowKey);
59+
60+
const hasNestChildren = childrenColumnName && record && record[childrenColumnName];
61+
62+
const onInternalTriggerExpand = useEvent(onTriggerExpand);
63+
64+
return {
65+
...context,
66+
columnsKey,
67+
nestExpandable,
68+
expanded,
69+
hasNestChildren,
70+
record,
71+
onTriggerExpand: onInternalTriggerExpand,
72+
};
73+
}

0 commit comments

Comments
 (0)