-
Notifications
You must be signed in to change notification settings - Fork 572
/
useColumns.tsx
219 lines (199 loc) · 6.06 KB
/
useColumns.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
import * as React from 'react';
import warning from 'rc-util/lib/warning';
import toArray from 'rc-util/lib/Children/toArray';
import {
ColumnsType,
ColumnType,
FixedType,
Key,
GetRowKey,
TriggerEventHandler,
RenderExpandIcon,
ColumnGroupType,
} from '../interface';
import { INTERNAL_COL_DEFINE } from '../utils/legacyUtil';
export function convertChildrenToColumns<RecordType>(
children: React.ReactNode,
): ColumnsType<RecordType> {
return toArray(children)
.filter(node => React.isValidElement(node))
.map(({ key, props }: React.ReactElement) => {
const { children: nodeChildren, ...restProps } = props;
const column = {
key,
...restProps,
};
if (nodeChildren) {
column.children = convertChildrenToColumns(nodeChildren);
}
return column;
});
}
function flatColumns<RecordType>(columns: ColumnsType<RecordType>): ColumnType<RecordType>[] {
return columns.reduce((list, column) => {
const { fixed } = column;
// Convert `fixed='true'` to `fixed='left'` instead
const parsedFixed = fixed === true ? 'left' : fixed;
const subColumns = (column as ColumnGroupType<RecordType>).children;
if (subColumns && subColumns.length > 0) {
return [
...list,
...flatColumns(subColumns).map(subColum => ({
fixed: parsedFixed,
...subColum,
})),
];
}
return [
...list,
{
...column,
fixed: parsedFixed,
},
];
}, []);
}
function warningFixed(flattenColumns: readonly { fixed?: FixedType }[]) {
let allFixLeft = true;
for (let i = 0; i < flattenColumns.length; i += 1) {
const col = flattenColumns[i];
if (allFixLeft && col.fixed !== 'left') {
allFixLeft = false;
} else if (!allFixLeft && col.fixed === 'left') {
warning(false, `Index ${i - 1} of \`columns\` missing \`fixed='left'\` prop.`);
break;
}
}
let allFixRight = true;
for (let i = flattenColumns.length - 1; i >= 0; i -= 1) {
const col = flattenColumns[i];
if (allFixRight && col.fixed !== 'right') {
allFixRight = false;
} else if (!allFixRight && col.fixed === 'right') {
warning(false, `Index ${i + 1} of \`columns\` missing \`fixed='right'\` prop.`);
break;
}
}
}
function revertForRtl<RecordType>(columns: ColumnsType<RecordType>): ColumnsType<RecordType> {
return columns.map(column => {
const { fixed, ...restProps } = column;
// Convert `fixed='left'` to `fixed='right'` instead
let parsedFixed = fixed;
if (fixed === 'left') {
parsedFixed = 'right';
} else if (fixed === 'right') {
parsedFixed = 'left';
}
return {
fixed: parsedFixed,
...restProps,
};
});
}
/**
* Parse `columns` & `children` into `columns`.
*/
function useColumns<RecordType>(
{
prefixCls,
columns,
children,
expandable,
expandedKeys,
getRowKey,
onTriggerExpand,
expandIcon,
rowExpandable,
expandIconColumnIndex,
direction,
expandRowByClick,
columnWidth,
}: {
prefixCls?: string;
columns?: ColumnsType<RecordType>;
children?: React.ReactNode;
expandable: boolean;
expandedKeys: Set<Key>;
getRowKey: GetRowKey<RecordType>;
onTriggerExpand: TriggerEventHandler<RecordType>;
expandIcon?: RenderExpandIcon<RecordType>;
rowExpandable?: (record: RecordType) => boolean;
expandIconColumnIndex?: number;
direction?: 'ltr' | 'rtl';
expandRowByClick?: boolean;
columnWidth?: number | string;
},
transformColumns: (columns: ColumnsType<RecordType>) => ColumnsType<RecordType>,
): [ColumnsType<RecordType>, readonly ColumnType<RecordType>[]] {
const baseColumns = React.useMemo<ColumnsType<RecordType>>(
() => columns || convertChildrenToColumns(children),
[columns, children],
);
// Add expand column
const withExpandColumns = React.useMemo<ColumnsType<RecordType>>(() => {
if (expandable) {
const expandColIndex = expandIconColumnIndex || 0;
const prevColumn = baseColumns[expandColIndex];
const expandColumn = {
[INTERNAL_COL_DEFINE]: {
className: `${prefixCls}-expand-icon-col`,
},
title: '',
fixed: prevColumn ? prevColumn.fixed : null,
className: `${prefixCls}-row-expand-icon-cell`,
width: columnWidth,
render: (_, record, index) => {
const rowKey = getRowKey(record, index);
const expanded = expandedKeys.has(rowKey);
const recordExpandable = rowExpandable ? rowExpandable(record) : true;
const icon = expandIcon({
prefixCls,
expanded,
expandable: recordExpandable,
record,
onExpand: onTriggerExpand,
});
if (expandRowByClick) {
return <span onClick={e => e.stopPropagation()}>{icon}</span>;
}
return icon;
},
};
// Insert expand column in the target position
const cloneColumns = baseColumns.slice();
if (expandColIndex >= 0) {
cloneColumns.splice(expandColIndex, 0, expandColumn);
}
return cloneColumns;
}
return baseColumns;
}, [expandable, baseColumns, getRowKey, expandedKeys, expandIcon, direction]);
const mergedColumns = React.useMemo(() => {
let finalColumns = withExpandColumns;
if (transformColumns) {
finalColumns = transformColumns(finalColumns);
}
// Always provides at least one column for table display
if (!finalColumns.length) {
finalColumns = [
{
render: () => null,
},
];
}
return finalColumns;
}, [transformColumns, withExpandColumns, direction]);
const flattenColumns = React.useMemo(() => {
if (direction === 'rtl') {
return revertForRtl(flatColumns(mergedColumns));
}
return flatColumns(mergedColumns);
}, [mergedColumns, direction]);
// Only check out of production since it's waste for each render
if (process.env.NODE_ENV !== 'production') {
warningFixed(flattenColumns);
}
return [mergedColumns, flattenColumns];
}
export default useColumns;