-
Notifications
You must be signed in to change notification settings - Fork 573
/
BodyRow.tsx
234 lines (214 loc) · 6.69 KB
/
BodyRow.tsx
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import * as React from 'react';
import classNames from 'classnames';
import Cell from '../Cell';
import TableContext from '../context/TableContext';
import BodyContext from '../context/BodyContext';
import { getColumnsKey } from '../utils/valueUtil';
import { ColumnType, CustomizeComponent, GetComponentProps, Key, GetRowKey } from '../interface';
import ExpandedRow from './ExpandedRow';
export interface BodyRowProps<RecordType> {
record: RecordType;
index: number;
className?: string;
style?: React.CSSProperties;
recordKey: Key;
expandedKeys: Set<Key>;
rowComponent: CustomizeComponent;
cellComponent: CustomizeComponent;
onRow: GetComponentProps<RecordType>;
rowExpandable: (record: RecordType) => boolean;
indent?: number;
rowKey: React.Key;
getRowKey: GetRowKey<RecordType>;
childrenColumnName: string;
}
function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
props: BodyRowProps<RecordType>,
) {
const {
className,
style,
record,
index,
rowKey,
getRowKey,
rowExpandable,
expandedKeys,
onRow,
indent = 0,
rowComponent: RowComponent,
cellComponent,
childrenColumnName,
} = props;
const { prefixCls, fixedInfoList } = React.useContext(TableContext);
const {
fixHeader,
fixColumn,
horizonScroll,
componentWidth,
flattenColumns,
expandableType,
expandRowByClick,
onTriggerExpand,
rowClassName,
expandedRowClassName,
indentSize,
expandIcon,
expandedRowRender,
expandIconColumnIndex,
} = React.useContext(BodyContext);
const [expandRended, setExpandRended] = React.useState(false);
const expanded = expandedKeys && expandedKeys.has(props.recordKey);
React.useEffect(() => {
if (expanded) {
setExpandRended(true);
}
}, [expanded]);
const rowSupportExpand = expandableType === 'row' && (!rowExpandable || rowExpandable(record));
// Only when row is not expandable and `children` exist in record
const nestExpandable = expandableType === 'nest';
const hasNestChildren = childrenColumnName && record && record[childrenColumnName];
const mergedExpandable = rowSupportExpand || nestExpandable;
// =========================== onRow ===========================
let additionalProps: React.HTMLAttributes<HTMLElement>;
if (onRow) {
additionalProps = onRow(record, index);
}
const onClick: React.MouseEventHandler<HTMLElement> = (event, ...args) => {
if (expandRowByClick && mergedExpandable) {
onTriggerExpand(record, event);
}
if (additionalProps && additionalProps.onClick) {
additionalProps.onClick(event, ...args);
}
};
// ======================== Base tr row ========================
let computeRowClassName: string;
if (typeof rowClassName === 'string') {
computeRowClassName = rowClassName;
} else if (typeof rowClassName === 'function') {
computeRowClassName = rowClassName(record, index, indent);
}
const columnsKey = getColumnsKey(flattenColumns);
const baseRowNode = (
<RowComponent
{...additionalProps}
data-row-key={rowKey}
className={classNames(
className,
`${prefixCls}-row`,
`${prefixCls}-row-level-${indent}`,
computeRowClassName,
additionalProps && additionalProps.className,
)}
style={{
...style,
...(additionalProps ? additionalProps.style : null),
}}
onClick={onClick}
>
{flattenColumns.map((column: ColumnType<RecordType>, colIndex) => {
const { render, dataIndex, className: columnClassName } = column;
const key = columnsKey[colIndex];
const fixedInfo = fixedInfoList[colIndex];
// ============= Used for nest expandable =============
let appendCellNode: React.ReactNode;
if (colIndex === (expandIconColumnIndex || 0) && nestExpandable) {
appendCellNode = (
<>
<span
style={{ paddingLeft: `${indentSize * indent}px` }}
className={`${prefixCls}-row-indent indent-level-${indent}`}
/>
{expandIcon({
prefixCls,
expanded,
expandable: hasNestChildren,
record,
onExpand: onTriggerExpand,
})}
</>
);
}
let additionalCellProps: React.HTMLAttributes<HTMLElement>;
if (column.onCell) {
additionalCellProps = column.onCell(record, index);
}
return (
<Cell
className={columnClassName}
ellipsis={column.ellipsis}
align={column.align}
component={cellComponent}
prefixCls={prefixCls}
key={key}
record={record}
index={index}
dataIndex={dataIndex}
render={render}
shouldCellUpdate={column.shouldCellUpdate}
{...fixedInfo}
appendNode={appendCellNode}
additionalProps={additionalCellProps}
/>
);
})}
</RowComponent>
);
// ======================== Expand Row =========================
let expandRowNode: React.ReactElement;
if (rowSupportExpand && (expandRended || expanded)) {
const expandContent = expandedRowRender(record, index, indent + 1, expanded);
const computedExpandedRowClassName =
expandedRowClassName && expandedRowClassName(record, index, indent);
expandRowNode = (
<ExpandedRow
expanded={expanded}
className={classNames(
`${prefixCls}-expanded-row`,
`${prefixCls}-expanded-row-level-${indent + 1}`,
computedExpandedRowClassName,
)}
prefixCls={prefixCls}
fixHeader={fixHeader}
fixColumn={fixColumn}
horizonScroll={horizonScroll}
component={RowComponent}
componentWidth={componentWidth}
cellComponent={cellComponent}
colSpan={flattenColumns.length}
>
{expandContent}
</ExpandedRow>
);
}
// ========================= Nest Row ==========================
let nestRowNode: React.ReactElement[];
if (hasNestChildren && expanded) {
nestRowNode = (record[childrenColumnName] || []).map(
(subRecord: RecordType, subIndex: number): React.ReactElement => {
const subKey = getRowKey(subRecord, subIndex);
return (
<BodyRow
{...props}
key={subKey}
rowKey={subKey}
record={subRecord}
recordKey={subKey}
index={subIndex}
indent={indent + 1}
/>
);
},
);
}
return (
<>
{baseRowNode}
{expandRowNode}
{nestRowNode}
</>
);
}
BodyRow.displayName = 'BodyRow';
export default BodyRow;