-
Notifications
You must be signed in to change notification settings - Fork 574
/
useColumns.tsx
182 lines (161 loc) · 4.98 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
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,
} from '../interface';
import { INTERNAL_COL_DEFINE } from '../utils/legacyUtil';
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;
if ('children' in column) {
return [
...list,
...flatColumns(column.children).map(subColum => ({
fixed: parsedFixed,
...subColum,
})),
];
}
return [
...list,
{
...column,
fixed: parsedFixed,
},
];
}, []);
}
function warningFixed(flattenColumns: { 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;
}
}
}
/**
* Parse `columns` & `children` into `columns`.
*/
function useColumns<RecordType>(
{
prefixCls,
columns,
children,
expandable,
expandedKeys,
getRowKey,
onTriggerExpand,
expandIcon,
rowExpandable,
expandIconColumnIndex,
}: {
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;
},
transformColumns: (columns: ColumnsType<RecordType>) => ColumnsType<RecordType>,
): [ColumnsType<RecordType>, 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`,
render: (_, record, index) => {
const rowKey = getRowKey(record, index);
const expanded = expandedKeys.has(rowKey);
const recordExpandable = rowExpandable ? rowExpandable(record) : true;
return expandIcon({
prefixCls,
expanded,
expandable: recordExpandable,
record,
onExpand: onTriggerExpand,
});
},
};
// Insert expand column in the target position
const cloneColumns = baseColumns.slice();
cloneColumns.splice(expandColIndex, 0, expandColumn);
return cloneColumns;
}
return baseColumns;
}, [expandable, baseColumns, getRowKey, expandedKeys, expandIcon]);
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]);
const flattenColumns = React.useMemo(() => flatColumns(mergedColumns), [mergedColumns]);
// 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;