@hzab/edit-table 0.1.9 → 0.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +59 -28
- package/LICENSE +20 -20
- package/README.md +170 -170
- package/dist/index.cjs +1 -1
- package/dist/index.js +263 -216
- package/dist/index.js.map +1 -0
- package/dist/project-manifest.json +28 -0
- package/dist/project.json +8 -0
- package/package.json +6 -5
- package/src/Template/ExpandListTemplate/index.less +16 -18
- package/src/Template/ExpandListTemplate/index.tsx +58 -58
- package/src/Template/GetBaseTemplate.tsx +37 -37
- package/src/Template/NumberCellTemplate/index.less +5 -5
- package/src/Template/NumberCellTemplate/index.tsx +16 -16
- package/src/Template/TextCellTemplate/index.less +5 -5
- package/src/Template/TextCellTemplate/index.tsx +8 -8
- package/src/common/WidthWrapHoc.tsx +255 -242
- package/src/common/childAntdComponent.ts +24 -16
- package/src/common/utils.ts +295 -288
- package/src/index.less +98 -104
- package/src/index.tsx +489 -464
- package/src/vite-env.d.ts +1 -1
package/src/common/utils.ts
CHANGED
|
@@ -1,288 +1,295 @@
|
|
|
1
|
-
export const getNavigatorLanguage = (): string => navigator.language || "en-US";
|
|
2
|
-
|
|
3
|
-
function getLocaleSeparators(locale: string) {
|
|
4
|
-
const testNumber = 123456.789;
|
|
5
|
-
const localeFormattedNumber = Intl.NumberFormat(locale).format(testNumber);
|
|
6
|
-
|
|
7
|
-
// Get the thousands separator of the locale
|
|
8
|
-
const thousandsSeparator = localeFormattedNumber.split("123")[1][0];
|
|
9
|
-
|
|
10
|
-
// Get the decimal separator of the locale
|
|
11
|
-
const decimalSeparator = localeFormattedNumber.split("123")[1][4];
|
|
12
|
-
return { thousandsSeparator, decimalSeparator };
|
|
13
|
-
}
|
|
14
|
-
export function parseLocaleNumber(stringNumber: string, locale = getNavigatorLanguage()): number {
|
|
15
|
-
if (!stringNumber.trim()) return NaN;
|
|
16
|
-
const { thousandsSeparator, decimalSeparator } = getLocaleSeparators(locale);
|
|
17
|
-
const normalizedStringNumber = stringNumber.replace(/\u00A0/g, " "); // Replace non-breaking space with normal space
|
|
18
|
-
const numberString = normalizedStringNumber
|
|
19
|
-
.replace(new RegExp(`[${thousandsSeparator}\\s]`, "g"), "") // Replace thousands separator and white-space
|
|
20
|
-
.replace(new RegExp(`\\${decimalSeparator}`, "g"), "."); // Replace decimal separator
|
|
21
|
-
|
|
22
|
-
const trimmedNumberString = numberString.replace(/^(?!-)\D+|\D+$/g, ""); // Remove characters before first and after last number, but keep negative sign
|
|
23
|
-
if (trimmedNumberString === null || trimmedNumberString.trim().length === 0) {
|
|
24
|
-
return NaN;
|
|
25
|
-
}
|
|
26
|
-
return Number(trimmedNumberString);
|
|
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
|
-
const
|
|
63
|
-
const
|
|
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
|
-
if (node.sorter) cell.sorter = node.sorter;
|
|
130
|
-
if (node.key) cell.key = node.key;
|
|
131
|
-
rows[rowIndex].cells.push(cell);
|
|
132
|
-
} else {
|
|
133
|
-
// 父节点:添加colspan
|
|
134
|
-
rows[rowIndex].cells.push({
|
|
135
|
-
...node,
|
|
136
|
-
type: handleType(node),
|
|
137
|
-
text: getTitle(node),
|
|
138
|
-
colspan: node.colspan,
|
|
139
|
-
style: node?.style
|
|
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
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
}
|
|
1
|
+
export const getNavigatorLanguage = (): string => navigator.language || "en-US";
|
|
2
|
+
|
|
3
|
+
function getLocaleSeparators(locale: string) {
|
|
4
|
+
const testNumber = 123456.789;
|
|
5
|
+
const localeFormattedNumber = Intl.NumberFormat(locale).format(testNumber);
|
|
6
|
+
|
|
7
|
+
// Get the thousands separator of the locale
|
|
8
|
+
const thousandsSeparator = localeFormattedNumber.split("123")[1][0];
|
|
9
|
+
|
|
10
|
+
// Get the decimal separator of the locale
|
|
11
|
+
const decimalSeparator = localeFormattedNumber.split("123")[1][4];
|
|
12
|
+
return { thousandsSeparator, decimalSeparator };
|
|
13
|
+
}
|
|
14
|
+
export function parseLocaleNumber(stringNumber: string, locale = getNavigatorLanguage()): number {
|
|
15
|
+
if (!stringNumber.trim()) return NaN;
|
|
16
|
+
const { thousandsSeparator, decimalSeparator } = getLocaleSeparators(locale);
|
|
17
|
+
const normalizedStringNumber = stringNumber.replace(/\u00A0/g, " "); // Replace non-breaking space with normal space
|
|
18
|
+
const numberString = normalizedStringNumber
|
|
19
|
+
.replace(new RegExp(`[${thousandsSeparator}\\s]`, "g"), "") // Replace thousands separator and white-space
|
|
20
|
+
.replace(new RegExp(`\\${decimalSeparator}`, "g"), "."); // Replace decimal separator
|
|
21
|
+
|
|
22
|
+
const trimmedNumberString = numberString.replace(/^(?!-)\D+|\D+$/g, ""); // Remove characters before first and after last number, but keep negative sign
|
|
23
|
+
if (trimmedNumberString === null || trimmedNumberString.trim().length === 0) {
|
|
24
|
+
return NaN;
|
|
25
|
+
}
|
|
26
|
+
return Number(trimmedNumberString);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function isReactElement(value) {
|
|
30
|
+
return value !== null && typeof value === "object";
|
|
31
|
+
}
|
|
32
|
+
const getTitle = (item) => {
|
|
33
|
+
return typeof item?.title == "function"
|
|
34
|
+
? isReactElement(item?.title())
|
|
35
|
+
? item?.title()?.props.children
|
|
36
|
+
: String(item?.title()) || ""
|
|
37
|
+
: String(item?.title) || "";
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export function convertColumnsToRows(columns, headerRowHeight = 40) {
|
|
41
|
+
// 获取所有叶子节点
|
|
42
|
+
const getLeaves = (cols) => {
|
|
43
|
+
return cols.reduce((acc, col) => {
|
|
44
|
+
if (col.children?.length) {
|
|
45
|
+
return [...acc, ...getLeaves(col.children)];
|
|
46
|
+
}
|
|
47
|
+
return [...acc, col];
|
|
48
|
+
}, []);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// 计算最大深度
|
|
52
|
+
const getDepth = (cols, depth = 1) => {
|
|
53
|
+
return cols.reduce((max, col) => {
|
|
54
|
+
if (col.children?.length) {
|
|
55
|
+
return Math.max(max, getDepth(col.children, depth + 1));
|
|
56
|
+
}
|
|
57
|
+
return Math.max(max, depth);
|
|
58
|
+
}, depth);
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const leafColumns = getLeaves(columns);
|
|
62
|
+
const totalDepth = getDepth(columns);
|
|
63
|
+
const totalCols = leafColumns.length;
|
|
64
|
+
|
|
65
|
+
// 构建表头树
|
|
66
|
+
const buildTree = (cols, depth = 0) => {
|
|
67
|
+
return cols.map((col) => {
|
|
68
|
+
const hasChildren = col.children?.length > 0;
|
|
69
|
+
if (!hasChildren) {
|
|
70
|
+
return {
|
|
71
|
+
...col,
|
|
72
|
+
rowspan: totalDepth - depth,
|
|
73
|
+
colspan: 1,
|
|
74
|
+
_depth: depth,
|
|
75
|
+
_isLeaf: true,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
const leafCount = getLeaves(col.children).length;
|
|
79
|
+
return {
|
|
80
|
+
...col,
|
|
81
|
+
rowspan: 1,
|
|
82
|
+
colspan: leafCount,
|
|
83
|
+
_depth: depth,
|
|
84
|
+
_isLeaf: false,
|
|
85
|
+
children: buildTree(col.children, depth + 1),
|
|
86
|
+
};
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const tree = buildTree(columns, 0);
|
|
91
|
+
|
|
92
|
+
// 初始化行
|
|
93
|
+
const rows: any = Array.from({ length: totalDepth }, (_, i) => ({
|
|
94
|
+
rowId: `header-${i}`,
|
|
95
|
+
height: headerRowHeight,
|
|
96
|
+
cells: [],
|
|
97
|
+
}));
|
|
98
|
+
|
|
99
|
+
const handleType = (item) => {
|
|
100
|
+
if (["rowSelection", "ExpandListTemplate"]?.includes(item?.type)) {
|
|
101
|
+
return item?.type;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (isReactElement(typeof item?.title == "function" ? item?.title() : null)) {
|
|
105
|
+
return `${item?.dataIndex}`;
|
|
106
|
+
}
|
|
107
|
+
return "header";
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
// 递归填充行
|
|
111
|
+
const fillRow = (nodes, rowIndex = 0) => {
|
|
112
|
+
nodes.forEach((node) => {
|
|
113
|
+
if (node._isLeaf) {
|
|
114
|
+
const cell = {
|
|
115
|
+
...node,
|
|
116
|
+
type: handleType(node),
|
|
117
|
+
text: getTitle(node),
|
|
118
|
+
dataIndex: node.dataIndex,
|
|
119
|
+
width: node.width,
|
|
120
|
+
rowspan: node.rowspan,
|
|
121
|
+
style: node?.style
|
|
122
|
+
? node?.style
|
|
123
|
+
: {
|
|
124
|
+
background: "rgba(128, 128, 128, 0.1)",
|
|
125
|
+
display: "flex",
|
|
126
|
+
justifyContent: "center",
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
if (node.sorter) cell.sorter = node.sorter;
|
|
130
|
+
if (node.key) cell.key = node.key;
|
|
131
|
+
rows[rowIndex].cells.push(cell);
|
|
132
|
+
} else {
|
|
133
|
+
// 父节点:添加colspan
|
|
134
|
+
rows[rowIndex].cells.push({
|
|
135
|
+
...node,
|
|
136
|
+
type: handleType(node),
|
|
137
|
+
text: getTitle(node),
|
|
138
|
+
colspan: node.colspan,
|
|
139
|
+
style: node?.style
|
|
140
|
+
? node?.style
|
|
141
|
+
: {
|
|
142
|
+
background: "rgba(128, 128, 128, 0.1)",
|
|
143
|
+
display: "flex",
|
|
144
|
+
justifyContent: "center",
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
if (node.children) {
|
|
148
|
+
fillRow(node.children, rowIndex + 1);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
fillRow(tree, 0);
|
|
155
|
+
|
|
156
|
+
// 补全每一行的cell,使长度一致
|
|
157
|
+
rows.forEach((row, rowIndex) => {
|
|
158
|
+
const newCells = [];
|
|
159
|
+
let colIndex = 0;
|
|
160
|
+
let cellIndex = 0;
|
|
161
|
+
|
|
162
|
+
while (colIndex < totalCols) {
|
|
163
|
+
const currentCell = row.cells[cellIndex];
|
|
164
|
+
|
|
165
|
+
if (!currentCell) {
|
|
166
|
+
// 如果当前行没有足够的cell,从叶子节点补充
|
|
167
|
+
const leaf = leafColumns[colIndex];
|
|
168
|
+
newCells.push({
|
|
169
|
+
...leaf,
|
|
170
|
+
type: handleType(leaf),
|
|
171
|
+
text: getTitle(leaf),
|
|
172
|
+
dataIndex: leaf.dataIndex,
|
|
173
|
+
width: leaf.width,
|
|
174
|
+
rowspan: rowIndex === totalDepth - 1 ? 1 : undefined,
|
|
175
|
+
style: leaf?.style
|
|
176
|
+
? leaf?.style
|
|
177
|
+
: {
|
|
178
|
+
background: "rgba(128, 128, 128, 0.1)",
|
|
179
|
+
display: "flex",
|
|
180
|
+
justifyContent: "center",
|
|
181
|
+
},
|
|
182
|
+
});
|
|
183
|
+
colIndex++;
|
|
184
|
+
cellIndex++;
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const colspan = currentCell.colspan || 1;
|
|
189
|
+
const rowspan = currentCell.rowspan || 1;
|
|
190
|
+
|
|
191
|
+
// 检查当前cell是否应该出现在这一行
|
|
192
|
+
// 如果rowspan > 1,表示它跨多行,但只在第一行出现
|
|
193
|
+
if (rowspan > 1 && rowIndex > 0) {
|
|
194
|
+
// 这个cell不应该出现在当前行,跳过
|
|
195
|
+
colIndex += colspan;
|
|
196
|
+
cellIndex++;
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// 如果当前cell有colspan,需要补全后面的空cell
|
|
201
|
+
newCells.push(currentCell);
|
|
202
|
+
colIndex += colspan;
|
|
203
|
+
cellIndex++;
|
|
204
|
+
|
|
205
|
+
// 如果colspan > 1,后面补充空cell
|
|
206
|
+
for (let i = 1; i < colspan; i++) {
|
|
207
|
+
newCells.push({
|
|
208
|
+
type: "header",
|
|
209
|
+
text: "",
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
row.cells = newCells;
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
// 最后一行的特殊处理:确保所有叶子节点都有对应的cell
|
|
218
|
+
const lastRow = rows[totalDepth - 1];
|
|
219
|
+
const lastRowCells = [];
|
|
220
|
+
const leafIndex = 0;
|
|
221
|
+
|
|
222
|
+
// 遍历所有叶子节点
|
|
223
|
+
leafColumns.forEach((leaf, index) => {
|
|
224
|
+
// 检查是否已经在lastRow中有对应的cell
|
|
225
|
+
const existingCell = lastRow.cells.find(
|
|
226
|
+
(cell) => cell.dataIndex === leaf.dataIndex && cell.text === leaf.title,
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
if (existingCell) {
|
|
230
|
+
lastRowCells.push(existingCell);
|
|
231
|
+
} else {
|
|
232
|
+
// 创建新的cell
|
|
233
|
+
lastRowCells.push({
|
|
234
|
+
...leaf,
|
|
235
|
+
type: handleType(leaf),
|
|
236
|
+
text: getTitle(leaf),
|
|
237
|
+
dataIndex: leaf.dataIndex,
|
|
238
|
+
width: leaf.width,
|
|
239
|
+
rowspan: 1,
|
|
240
|
+
style: leaf?.style
|
|
241
|
+
? leaf?.style
|
|
242
|
+
: {
|
|
243
|
+
background: "rgba(128, 128, 128, 0.1)",
|
|
244
|
+
display: "flex",
|
|
245
|
+
justifyContent: "center",
|
|
246
|
+
},
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
lastRow.cells = lastRowCells;
|
|
252
|
+
|
|
253
|
+
// 确保所有行的cell数量等于totalCols
|
|
254
|
+
rows.forEach((row) => {
|
|
255
|
+
// 如果cell数量少于totalCols,补充空cell
|
|
256
|
+
while (row.cells.length < totalCols) {
|
|
257
|
+
row.cells.push({
|
|
258
|
+
type: "header",
|
|
259
|
+
text: "",
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
return rows;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export function flattenColumns(columns: any[]) {
|
|
268
|
+
const result: any[] = [];
|
|
269
|
+
|
|
270
|
+
function traverse(cols) {
|
|
271
|
+
cols.forEach((col) => {
|
|
272
|
+
if (col.children && col.children.length > 0) {
|
|
273
|
+
traverse(col.children);
|
|
274
|
+
} else {
|
|
275
|
+
// 创建新对象,只保留需要的属性
|
|
276
|
+
const { title, dataIndex, key, width, sorter, k, ...rest } = col;
|
|
277
|
+
|
|
278
|
+
result.push({
|
|
279
|
+
type: "Input",
|
|
280
|
+
dataIndex,
|
|
281
|
+
title,
|
|
282
|
+
...(key && { key }),
|
|
283
|
+
...(width && { width }),
|
|
284
|
+
...(sorter && { sorter }),
|
|
285
|
+
...(k && { k }),
|
|
286
|
+
// 如果需要保留其他属性
|
|
287
|
+
...rest,
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
traverse(columns);
|
|
294
|
+
return result;
|
|
295
|
+
}
|