-
-
Notifications
You must be signed in to change notification settings - Fork 619
Expand file tree
/
Copy pathuseRowInfo.tsx
More file actions
125 lines (112 loc) · 3.3 KB
/
useRowInfo.tsx
File metadata and controls
125 lines (112 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { useContext } from '@rc-component/context';
import type { TableContextProps } from '../context/TableContext';
import TableContext from '../context/TableContext';
import { getColumnsKey } from '../utils/valueUtil';
import { useEvent } from '@rc-component/util';
import classNames from 'classnames';
export default function useRowInfo<RecordType>(
record: RecordType,
rowKey: React.Key,
recordIndex: number,
indent: number,
): Pick<
TableContextProps,
| 'prefixCls'
| 'fixedInfoList'
| 'flattenColumns'
| 'colWidths'
| 'expandableType'
| 'expandRowByClick'
| 'onTriggerExpand'
| 'rowClassName'
| 'expandedRowClassName'
| 'indentSize'
| 'expandIcon'
| 'expandedRowRender'
| 'expandIconColumnIndex'
| 'expandedKeys'
| 'childrenColumnName'
| 'onRow'
> & {
columnsKey: React.Key[];
nestExpandable: boolean;
expanded: boolean;
hasNestChildren: boolean;
record: RecordType;
rowSupportExpand: boolean;
expandable: boolean;
rowProps: React.HTMLAttributes<any> & React.TdHTMLAttributes<any>;
} {
const context: TableContextProps = useContext(TableContext, [
'prefixCls',
'fixedInfoList',
'flattenColumns',
'colWidths',
'expandableType',
'expandRowByClick',
'onTriggerExpand',
'rowClassName',
'expandedRowClassName',
'indentSize',
'expandIcon',
'expandedRowRender',
'expandIconColumnIndex',
'expandedKeys',
'childrenColumnName',
'rowExpandable',
'onRow',
]);
const {
flattenColumns,
expandableType,
expandedKeys,
childrenColumnName,
onTriggerExpand,
rowExpandable,
onRow,
expandRowByClick,
rowClassName,
} = context;
// ======================= Expandable =======================
// Only when row is not expandable and `children` exist in record
const nestExpandable = expandableType === 'nest';
const rowSupportExpand = expandableType === 'row' && (!rowExpandable || rowExpandable(record));
const mergedExpandable = rowSupportExpand || nestExpandable;
const expanded = expandedKeys && expandedKeys.has(rowKey);
const hasNestChildren = childrenColumnName && record && record[childrenColumnName];
const onInternalTriggerExpand = useEvent(onTriggerExpand);
// ========================= onRow ==========================
const rowProps = onRow?.(record, recordIndex);
const onRowClick = rowProps?.onClick;
const onClick: React.MouseEventHandler<HTMLElement> = (event, ...args) => {
if (expandRowByClick && mergedExpandable) {
onTriggerExpand(record, event);
}
onRowClick?.(event, ...args);
};
// ====================== RowClassName ======================
let computeRowClassName: string;
if (typeof rowClassName === 'string') {
computeRowClassName = rowClassName;
} else if (typeof rowClassName === 'function') {
computeRowClassName = rowClassName(record, recordIndex, indent);
}
// ========================= Column =========================
const columnsKey = getColumnsKey(flattenColumns);
return {
...context,
columnsKey,
nestExpandable,
expanded,
hasNestChildren,
record,
onTriggerExpand: onInternalTriggerExpand,
rowSupportExpand,
expandable: mergedExpandable,
rowProps: {
...rowProps,
className: classNames(computeRowClassName, rowProps?.className),
onClick,
},
};
}