1
1
import { useContext } from '@rc-component/context' ;
2
2
import classNames from 'classnames' ;
3
- import { useEvent } from 'rc-util' ;
4
3
import * as React from 'react' ;
5
4
import Cell from '../Cell' ;
6
- import TableContext , { responseImmutable , type TableContextProps } from '../context/TableContext' ;
5
+ import TableContext , { responseImmutable } from '../context/TableContext' ;
7
6
import devRenderTimes from '../hooks/useRenderTimes' ;
8
7
import type { ColumnType , CustomizeComponent , GetComponentProps , GetRowKey } from '../interface' ;
9
- import { getColumnsKey } from '../utils/valueUtil' ;
10
8
import ExpandedRow from './ExpandedRow' ;
9
+ import useRowInfo from '../hooks/useRowInfo' ;
11
10
12
11
export interface BodyRowProps < RecordType > {
13
12
record : RecordType ;
@@ -19,80 +18,14 @@ export interface BodyRowProps<RecordType> {
19
18
cellComponent : CustomizeComponent ;
20
19
scopeCellComponent : CustomizeComponent ;
21
20
onRow : GetComponentProps < RecordType > ;
22
- rowExpandable : ( record : RecordType ) => boolean ;
23
21
indent ?: number ;
24
22
rowKey : React . Key ;
25
23
getRowKey : GetRowKey < RecordType > ;
26
24
}
27
25
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
+ // ==================================================================================
96
29
export function getCellProps < RecordType > (
97
30
rowInfo : ReturnType < typeof useRowInfo < RecordType > > ,
98
31
column : ColumnType < RecordType > ,
@@ -150,6 +83,9 @@ export function getCellProps<RecordType>(
150
83
} ;
151
84
}
152
85
86
+ // ==================================================================================
87
+ // == getCellProps ==
88
+ // ==================================================================================
153
89
function BodyRow < RecordType extends { children ?: readonly RecordType [ ] } > (
154
90
props : BodyRowProps < RecordType > ,
155
91
) {
@@ -164,7 +100,6 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
164
100
index,
165
101
renderIndex,
166
102
rowKey,
167
- rowExpandable,
168
103
onRow,
169
104
indent = 0 ,
170
105
rowComponent : RowComponent ,
@@ -187,6 +122,8 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
187
122
expanded,
188
123
} = rowInfo ;
189
124
125
+ const { rowExpandable } = useContext ( TableContext , [ 'rowExpandable' ] ) ;
126
+
190
127
const [ expandRended , setExpandRended ] = React . useState ( false ) ;
191
128
192
129
if ( process . env . NODE_ENV !== 'production' ) {
0 commit comments