es-grid-template 0.0.7 → 0.0.13
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/LICENSE +21 -19
- package/es/{CheckboxFilter.d.ts → grid-component/CheckboxFilter.d.ts} +1 -1
- package/es/{CheckboxFilter.js → grid-component/CheckboxFilter.js} +7 -11
- package/es/grid-component/ColumnsChoose.d.ts +9 -0
- package/es/{ColumnsChoose.js → grid-component/ColumnsChoose.js} +66 -70
- package/es/grid-component/EditableCell.d.ts +19 -0
- package/es/grid-component/EditableCell.js +842 -0
- package/es/{FilterSearch.js → grid-component/FilterSearch.js} +2 -2
- package/es/grid-component/GridStyle.d.ts +4 -0
- package/es/grid-component/GridStyle.js +5 -0
- package/es/grid-component/InternalTable.d.ts +6 -0
- package/es/grid-component/InternalTable.js +283 -0
- package/es/grid-component/LoadingSpinner.d.ts +3 -0
- package/es/grid-component/LoadingSpinner.js +20 -0
- package/es/grid-component/Message/Message.d.ts +2 -0
- package/es/grid-component/Message/Message.js +16 -0
- package/es/grid-component/Message/index.d.ts +1 -0
- package/es/grid-component/Message/index.js +1 -0
- package/es/grid-component/TableGrid.d.ts +10 -0
- package/es/grid-component/TableGrid.js +375 -0
- package/es/grid-component/async-table-select/index.d.ts +9 -0
- package/es/grid-component/async-table-select/index.js +37 -0
- package/es/{hooks → grid-component/hooks}/constant.d.ts +14 -0
- package/es/{hooks → grid-component/hooks}/constant.js +17 -2
- package/es/grid-component/hooks/useColumns/index.d.ts +6 -0
- package/es/grid-component/hooks/useColumns/index.js +422 -0
- package/es/grid-component/hooks/utils.d.ts +35 -0
- package/es/{hooks → grid-component/hooks}/utils.js +147 -19
- package/es/grid-component/index.d.ts +2 -0
- package/es/grid-component/index.js +2 -0
- package/es/grid-component/rc-table/Grid.d.ts +8 -0
- package/es/grid-component/rc-table/Grid.js +99 -0
- package/es/grid-component/rc-table/GridEdit.d.ts +9 -0
- package/es/grid-component/rc-table/GridEdit.js +706 -0
- package/es/grid-component/type.d.ts +225 -0
- package/es/grid-component/useContext.d.ts +27 -0
- package/es/grid-component/useContext.js +4 -0
- package/es/index.d.ts +1 -2
- package/es/index.js +2 -2
- package/es/table-grid/styles.scss +551 -0
- package/lib/{CheckboxFilter.d.ts → grid-component/CheckboxFilter.d.ts} +1 -1
- package/lib/{CheckboxFilter.js → grid-component/CheckboxFilter.js} +11 -14
- package/lib/grid-component/ColumnsChoose.d.ts +9 -0
- package/lib/{ColumnsChoose.js → grid-component/ColumnsChoose.js} +66 -70
- package/lib/grid-component/EditableCell.d.ts +19 -0
- package/lib/grid-component/EditableCell.js +844 -0
- package/lib/{FilterSearch.js → grid-component/FilterSearch.js} +3 -3
- package/lib/grid-component/GridStyle.d.ts +4 -0
- package/lib/grid-component/GridStyle.js +12 -0
- package/lib/grid-component/InternalTable.d.ts +6 -0
- package/lib/grid-component/InternalTable.js +292 -0
- package/lib/grid-component/LoadingSpinner.d.ts +3 -0
- package/lib/grid-component/LoadingSpinner.js +29 -0
- package/lib/grid-component/Message/Message.d.ts +2 -0
- package/lib/grid-component/Message/Message.js +25 -0
- package/lib/grid-component/Message/index.d.ts +1 -0
- package/lib/grid-component/Message/index.js +16 -0
- package/lib/grid-component/TableGrid.d.ts +10 -0
- package/lib/grid-component/TableGrid.js +382 -0
- package/lib/grid-component/async-table-select/index.d.ts +9 -0
- package/lib/grid-component/async-table-select/index.js +46 -0
- package/lib/{hooks → grid-component/hooks}/constant.d.ts +14 -0
- package/lib/{hooks → grid-component/hooks}/constant.js +18 -3
- package/lib/grid-component/hooks/useColumns/index.d.ts +6 -0
- package/lib/grid-component/hooks/useColumns/index.js +435 -0
- package/lib/grid-component/hooks/utils.d.ts +35 -0
- package/lib/{hooks → grid-component/hooks}/utils.js +164 -22
- package/lib/grid-component/index.d.ts +2 -0
- package/lib/grid-component/index.js +9 -0
- package/lib/grid-component/rc-table/Grid.d.ts +8 -0
- package/lib/grid-component/rc-table/Grid.js +108 -0
- package/lib/grid-component/rc-table/GridEdit.d.ts +9 -0
- package/lib/grid-component/rc-table/GridEdit.js +715 -0
- package/lib/grid-component/type.d.ts +225 -0
- package/lib/grid-component/useContext.d.ts +27 -0
- package/lib/grid-component/useContext.js +10 -0
- package/lib/index.d.ts +1 -2
- package/lib/index.js +7 -3
- package/lib/table-grid/styles.scss +551 -0
- package/package.json +17 -6
- package/es/ColumnsChoose.d.ts +0 -10
- package/es/GridTable.d.ts +0 -7
- package/es/GridTable.js +0 -927
- package/es/hooks/useColumns/index.d.ts +0 -2
- package/es/hooks/useColumns/index.js +0 -25
- package/es/hooks/utils.d.ts +0 -18
- package/es/styles.scss +0 -30
- package/es/type.d.ts +0 -88
- package/lib/ColumnsChoose.d.ts +0 -10
- package/lib/GridTable.d.ts +0 -7
- package/lib/GridTable.js +0 -936
- package/lib/hooks/useColumns/index.d.ts +0 -2
- package/lib/hooks/useColumns/index.js +0 -31
- package/lib/hooks/utils.d.ts +0 -18
- package/lib/styles.scss +0 -30
- package/lib/type.d.ts +0 -88
- /package/es/{ContextMenu.d.ts → grid-component/ContextMenu.d.ts} +0 -0
- /package/es/{ContextMenu.js → grid-component/ContextMenu.js} +0 -0
- /package/es/{FilterSearch.d.ts → grid-component/FilterSearch.d.ts} +0 -0
- /package/es/{hooks → grid-component/hooks}/index.d.ts +0 -0
- /package/es/{hooks → grid-component/hooks}/index.js +0 -0
- /package/es/{hooks → grid-component/hooks}/useIsOverflow.d.ts +0 -0
- /package/es/{hooks → grid-component/hooks}/useIsOverflow.js +0 -0
- /package/es/{hooks → grid-component/hooks}/useOnClickOutside.d.ts +0 -0
- /package/es/{hooks → grid-component/hooks}/useOnClickOutside.js +0 -0
- /package/es/{type.js → grid-component/type.js} +0 -0
- /package/lib/{ContextMenu.d.ts → grid-component/ContextMenu.d.ts} +0 -0
- /package/lib/{ContextMenu.js → grid-component/ContextMenu.js} +0 -0
- /package/lib/{FilterSearch.d.ts → grid-component/FilterSearch.d.ts} +0 -0
- /package/lib/{hooks → grid-component/hooks}/index.d.ts +0 -0
- /package/lib/{hooks → grid-component/hooks}/index.js +0 -0
- /package/lib/{hooks → grid-component/hooks}/useIsOverflow.d.ts +0 -0
- /package/lib/{hooks → grid-component/hooks}/useIsOverflow.js +0 -0
- /package/lib/{hooks → grid-component/hooks}/useOnClickOutside.d.ts +0 -0
- /package/lib/{hooks → grid-component/hooks}/useOnClickOutside.js +0 -0
- /package/lib/{type.js → grid-component/type.js} +0 -0
|
@@ -0,0 +1,706 @@
|
|
|
1
|
+
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
2
|
+
import React, { Fragment, useEffect, useRef, useState } from 'react';
|
|
3
|
+
import customParseFormat from 'dayjs/plugin/customParseFormat';
|
|
4
|
+
import classNames from "classnames";
|
|
5
|
+
import { Form } from "antd";
|
|
6
|
+
import { useForm } from 'react-hook-form';
|
|
7
|
+
import { Toaster } from "react-hot-toast";
|
|
8
|
+
import { renderContent } from "../hooks/useColumns";
|
|
9
|
+
import EditableCell from "../EditableCell";
|
|
10
|
+
import { GridStyle } from "../GridStyle";
|
|
11
|
+
import { TableContext } from "../useContext";
|
|
12
|
+
import dayjs from "dayjs";
|
|
13
|
+
import 'dayjs/locale/es';
|
|
14
|
+
import 'dayjs/locale/vi';
|
|
15
|
+
import TableGrid from "../TableGrid";
|
|
16
|
+
import { getEditType, isObjEmpty, totalFixedWidth } from "../hooks";
|
|
17
|
+
import Message from "./../Message/Message";
|
|
18
|
+
dayjs.extend(customParseFormat);
|
|
19
|
+
const toast = 'top-right';
|
|
20
|
+
const GridEdit = props => {
|
|
21
|
+
const {
|
|
22
|
+
tableRef,
|
|
23
|
+
t,
|
|
24
|
+
columns,
|
|
25
|
+
dataSource,
|
|
26
|
+
allowResizing,
|
|
27
|
+
rowKey = 'id',
|
|
28
|
+
selectionSettings,
|
|
29
|
+
height,
|
|
30
|
+
format,
|
|
31
|
+
onDataChange,
|
|
32
|
+
onCellPaste,
|
|
33
|
+
onCellChange,
|
|
34
|
+
components,
|
|
35
|
+
...rest
|
|
36
|
+
} = props;
|
|
37
|
+
const isSelecting = useRef(false);
|
|
38
|
+
const startCell = useRef(null);
|
|
39
|
+
const isSelectingRow = useRef(false);
|
|
40
|
+
const rowStart = useRef(null);
|
|
41
|
+
const [form] = Form.useForm();
|
|
42
|
+
const [editingKey, setEditingKey] = useState('');
|
|
43
|
+
const [selectedCells, setSelectedCells] = useState(new Set());
|
|
44
|
+
const [rowsSelected, setRowsSelected] = useState(new Set());
|
|
45
|
+
const [selectedCell, setSelectedCell] = useState(null);
|
|
46
|
+
const {
|
|
47
|
+
control,
|
|
48
|
+
handleSubmit,
|
|
49
|
+
setValue,
|
|
50
|
+
trigger,
|
|
51
|
+
getValues
|
|
52
|
+
// formState: { errors }
|
|
53
|
+
} = useForm({
|
|
54
|
+
mode: 'onChange'
|
|
55
|
+
// defaultValues,
|
|
56
|
+
// resolver: yupResolver(formSchema)
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// @ts-ignore
|
|
60
|
+
const isEditing = record => record[rowKey] === editingKey;
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
const handleClickOutside = event => {
|
|
63
|
+
const tableBodies = document.getElementsByClassName("ui-rc-table-tbody");
|
|
64
|
+
const element = event.target;
|
|
65
|
+
const container = document.querySelector(".be-popup-container");
|
|
66
|
+
// const containerHidden = document.querySelector(".be-popup-container.ant-picker-dropdown-hidden")
|
|
67
|
+
|
|
68
|
+
const isInsideContainer = element.closest(".be-popup-container") && container;
|
|
69
|
+
if (isInsideContainer) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (tableBodies.length > 0) {
|
|
73
|
+
const tableBody = tableBodies[0]; // Chọn tbody đầu tiên (nếu có nhiều)
|
|
74
|
+
|
|
75
|
+
if (!tableBody.contains(event.target)) {
|
|
76
|
+
setEditingKey('');
|
|
77
|
+
// isSelecting.current = false;
|
|
78
|
+
// startCell.current = null;
|
|
79
|
+
setSelectedCells(new Set());
|
|
80
|
+
} else {}
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
document.addEventListener("click", handleClickOutside);
|
|
84
|
+
return () => {
|
|
85
|
+
document.removeEventListener("click", handleClickOutside);
|
|
86
|
+
};
|
|
87
|
+
}, []);
|
|
88
|
+
const handleMouseDown = (row, col, e) => {
|
|
89
|
+
if (e.button === 2) {
|
|
90
|
+
e.stopPropagation();
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
setSelectedCell({
|
|
94
|
+
row,
|
|
95
|
+
col
|
|
96
|
+
});
|
|
97
|
+
isSelecting.current = true;
|
|
98
|
+
startCell.current = {
|
|
99
|
+
row,
|
|
100
|
+
col
|
|
101
|
+
};
|
|
102
|
+
setSelectedCells(new Set([`${row}-${col}`]));
|
|
103
|
+
setRowsSelected(new Set());
|
|
104
|
+
};
|
|
105
|
+
const handleMouseEnter = (row, col) => {
|
|
106
|
+
if (!isSelecting.current || !startCell.current) return;
|
|
107
|
+
const {
|
|
108
|
+
row: startRow,
|
|
109
|
+
col: startCol
|
|
110
|
+
} = startCell.current;
|
|
111
|
+
const newSelectedCells = new Set();
|
|
112
|
+
for (let r = Math.min(startRow, row); r <= Math.max(startRow, row); r++) {
|
|
113
|
+
for (let c = Math.min(startCol, col); c <= Math.max(startCol, col); c++) {
|
|
114
|
+
newSelectedCells.add(`${r}-${c}`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
setSelectedCells(newSelectedCells);
|
|
118
|
+
};
|
|
119
|
+
const handleMouseDownColIndex = (row, col, e) => {
|
|
120
|
+
if (e.button === 2) {
|
|
121
|
+
e.stopPropagation();
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
isSelectingRow.current = true;
|
|
125
|
+
rowStart.current = {
|
|
126
|
+
row,
|
|
127
|
+
col
|
|
128
|
+
};
|
|
129
|
+
setRowsSelected(new Set([`${row}-${col}`]));
|
|
130
|
+
};
|
|
131
|
+
const handleMouseEnterColIndex = (row, col, e) => {
|
|
132
|
+
// dừng sự kiện kéo để chọn từ các column khác khi vừa gặp column index
|
|
133
|
+
if (isSelecting) {
|
|
134
|
+
startCell.current = null;
|
|
135
|
+
e.stopPropagation();
|
|
136
|
+
}
|
|
137
|
+
if (!isSelectingRow.current || !rowStart.current) return;
|
|
138
|
+
// const { row: startRow, col: startCol } = rowStart.current;
|
|
139
|
+
const {
|
|
140
|
+
row: startRow,
|
|
141
|
+
col: startCol
|
|
142
|
+
} = rowStart.current;
|
|
143
|
+
const newSelectedRows = new Set();
|
|
144
|
+
for (let r = Math.min(startRow, row); r <= Math.max(startRow, row); r++) {
|
|
145
|
+
for (let c = Math.min(startCol, col); c <= Math.max(startCol, col); c++) {
|
|
146
|
+
newSelectedRows.add(`${r}-${c}`);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
const newSelectedCells = new Set();
|
|
150
|
+
for (let r = Math.min(startRow, row); r <= Math.max(startRow, row); r++) {
|
|
151
|
+
for (let c = Math.min(columns.length, col) + 1; c <= Math.max(columns.length, col); c++) {
|
|
152
|
+
newSelectedCells.add(`${r}-${c}`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
setRowsSelected(newSelectedRows);
|
|
156
|
+
setSelectedCells(newSelectedCells);
|
|
157
|
+
};
|
|
158
|
+
const handleMouseUp = () => {
|
|
159
|
+
isSelecting.current = false;
|
|
160
|
+
startCell.current = null;
|
|
161
|
+
isSelectingRow.current = false;
|
|
162
|
+
rowStart.current = null;
|
|
163
|
+
};
|
|
164
|
+
const handleCopy = e => {
|
|
165
|
+
const selectedArray = Array.from(selectedCells).map(key => {
|
|
166
|
+
const [row, col] = key.split("-").map(Number);
|
|
167
|
+
// @ts-ignore
|
|
168
|
+
const columnKey = columns[col].dataIndex;
|
|
169
|
+
// @ts-ignore
|
|
170
|
+
return {
|
|
171
|
+
row,
|
|
172
|
+
col,
|
|
173
|
+
value: data[row][columnKey]
|
|
174
|
+
};
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
// Xác định min/max row và col để sắp xếp dữ liệu
|
|
178
|
+
const minRow = Math.min(...selectedArray.map(cell => cell.row));
|
|
179
|
+
const maxRow = Math.max(...selectedArray.map(cell => cell.row));
|
|
180
|
+
const minCol = Math.min(...selectedArray.map(cell => cell.col));
|
|
181
|
+
const maxCol = Math.max(...selectedArray.map(cell => cell.col));
|
|
182
|
+
|
|
183
|
+
// Tạo dữ liệu dạng bảng (mảng 2D)
|
|
184
|
+
const table = Array.from({
|
|
185
|
+
length: maxRow - minRow + 1
|
|
186
|
+
}, () => Array(maxCol - minCol + 1).fill(""));
|
|
187
|
+
|
|
188
|
+
// Gán giá trị vào bảng
|
|
189
|
+
selectedArray.forEach(({
|
|
190
|
+
row,
|
|
191
|
+
col,
|
|
192
|
+
value
|
|
193
|
+
}) => {
|
|
194
|
+
table[row - minRow][col - minCol] = value;
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
// Chuyển mảng 2D thành chuỗi định dạng Excel
|
|
198
|
+
const copyText = table.map(row => row.join("\t")).join("\n");
|
|
199
|
+
|
|
200
|
+
// Copy vào clipboard
|
|
201
|
+
e.preventDefault();
|
|
202
|
+
e.clipboardData.setData("text/plain", copyText);
|
|
203
|
+
Message(t ? t('CopySuccessful') : 'Copy Successful');
|
|
204
|
+
};
|
|
205
|
+
const handlePaste = (e, indexCol, indexRow) => {
|
|
206
|
+
// const clipboard: any = (e.clipboardData || (window && window?.Clipboard)).getData("text")
|
|
207
|
+
const pasteData = e.clipboardData.getData("text/plain").trim();
|
|
208
|
+
|
|
209
|
+
// Chuyển đổi dữ liệu từ clipboard thành mảng
|
|
210
|
+
const rows = pasteData.split("\n").map(row => row.replace(/\r/g, "").split("\t"));
|
|
211
|
+
|
|
212
|
+
// Lấy vị trí bắt đầu
|
|
213
|
+
// const { row: startRow, col: startCol } = selectedCell;
|
|
214
|
+
const startRow = indexRow;
|
|
215
|
+
const startCol = indexCol;
|
|
216
|
+
|
|
217
|
+
// Cập nhật data mới
|
|
218
|
+
const newData = [...dataSource];
|
|
219
|
+
const pastedRows = [];
|
|
220
|
+
const pastedColumns = new Set();
|
|
221
|
+
rows.forEach((rowValues, rowIndex) => {
|
|
222
|
+
const targetRow = startRow + rowIndex;
|
|
223
|
+
|
|
224
|
+
// Nếu vượt quá số dòng hiện có, thêm dòng mới
|
|
225
|
+
if (targetRow >= newData.length) {
|
|
226
|
+
// @ts-ignore
|
|
227
|
+
// newData.push({ id: newGuid()});
|
|
228
|
+
newData.push({
|
|
229
|
+
id: newGuid()
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
rowValues.forEach((cellValue, colIndex) => {
|
|
233
|
+
const targetCol = startCol + colIndex;
|
|
234
|
+
if (targetCol >= columns.length) return; // Không vượt quá số cột
|
|
235
|
+
|
|
236
|
+
// @ts-ignore
|
|
237
|
+
const columnKey = columns[targetCol].dataIndex;
|
|
238
|
+
// @ts-ignore
|
|
239
|
+
newData[targetRow] = {
|
|
240
|
+
...newData[targetRow],
|
|
241
|
+
[columnKey]: cellValue.trim()
|
|
242
|
+
};
|
|
243
|
+
pastedColumns.add(columnKey);
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
// Lưu dòng được paste
|
|
247
|
+
pastedRows.push(newData[targetRow]);
|
|
248
|
+
});
|
|
249
|
+
const pastedColumnsArray = Array.from(pastedColumns) ?? [];
|
|
250
|
+
console.log(pastedColumnsArray);
|
|
251
|
+
console.log(pastedRows);
|
|
252
|
+
|
|
253
|
+
// // trigger DataChange + trigger paste
|
|
254
|
+
|
|
255
|
+
// const handlePasteCallback = (callbackData: RecordType[]) => {
|
|
256
|
+
//
|
|
257
|
+
// const newDataUpdate = updateData(dataSource, callbackData, rowKey as any)
|
|
258
|
+
//
|
|
259
|
+
// // setData(newDataUpdate)
|
|
260
|
+
// // onDataChange?.(newData)
|
|
261
|
+
// onCellPaste?.dataChange?.(newDataUpdate)
|
|
262
|
+
// }
|
|
263
|
+
//
|
|
264
|
+
//
|
|
265
|
+
// if (onCellPaste && onCellPaste.onPasted) {
|
|
266
|
+
//
|
|
267
|
+
// if (onCellPaste.onPasted.length > 1) {
|
|
268
|
+
// onCellPaste.onPasted({
|
|
269
|
+
// pasteData: pastedRows,
|
|
270
|
+
// type: 'onPaste',
|
|
271
|
+
// data: dataSource,
|
|
272
|
+
// pastedColumns: pastedColumnsArray as string[]
|
|
273
|
+
// }, handlePasteCallback)
|
|
274
|
+
//
|
|
275
|
+
// } else {
|
|
276
|
+
// onCellPaste.onPasted({
|
|
277
|
+
// pasteData: pastedRows,
|
|
278
|
+
// type: 'onPaste',
|
|
279
|
+
// data: dataSource,
|
|
280
|
+
// pastedColumns: ['']
|
|
281
|
+
// }, handlePasteCallback)
|
|
282
|
+
//
|
|
283
|
+
// onCellPaste.dataChange?.(newData)
|
|
284
|
+
//
|
|
285
|
+
// // setData(newData);
|
|
286
|
+
// // onDataChange?.(newData)
|
|
287
|
+
// }
|
|
288
|
+
//
|
|
289
|
+
// }
|
|
290
|
+
};
|
|
291
|
+
const onSubmit = formData => {
|
|
292
|
+
console.log('onSubmit', formData);
|
|
293
|
+
try {
|
|
294
|
+
// const record = (await form.validateFields()) as RecordType;
|
|
295
|
+
const row = {
|
|
296
|
+
...formData
|
|
297
|
+
};
|
|
298
|
+
// const rowData = { ...record };
|
|
299
|
+
|
|
300
|
+
// columns.forEach(column => {
|
|
301
|
+
//
|
|
302
|
+
// // @ts-ignore
|
|
303
|
+
// if (column.editType === 'date' && rowData[column.dataIndex]) {
|
|
304
|
+
// // @ts-ignore
|
|
305
|
+
// rowData[column.dataIndex] = !isEmpty(record[column.dataIndex]) ? moment(row[column.dataIndex]).format() : null
|
|
306
|
+
// }
|
|
307
|
+
// });
|
|
308
|
+
|
|
309
|
+
const newData = [...dataSource];
|
|
310
|
+
// @ts-ignore
|
|
311
|
+
const index = newData.findIndex(item => formData[rowKey] === item[rowKey]);
|
|
312
|
+
|
|
313
|
+
// setIsManualUpdate(true);
|
|
314
|
+
|
|
315
|
+
if (index > -1) {
|
|
316
|
+
const item = newData[index];
|
|
317
|
+
newData.splice(index, 1, {
|
|
318
|
+
...item,
|
|
319
|
+
...row
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
// trigger DataChange
|
|
323
|
+
// setData(newData);
|
|
324
|
+
// onDataChange?.(newData)
|
|
325
|
+
} else {
|
|
326
|
+
newData.push(row);
|
|
327
|
+
// trigger DataChange
|
|
328
|
+
|
|
329
|
+
// setData(newData);
|
|
330
|
+
// onDataChange?.(newData)
|
|
331
|
+
}
|
|
332
|
+
} catch (errInfo) {
|
|
333
|
+
console.log('Validate Failed:', errInfo);
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
const handleCellChange = args => {
|
|
337
|
+
const {
|
|
338
|
+
record,
|
|
339
|
+
type,
|
|
340
|
+
newState,
|
|
341
|
+
prevState
|
|
342
|
+
} = args;
|
|
343
|
+
|
|
344
|
+
// console.log('newState', newState)
|
|
345
|
+
//
|
|
346
|
+
// console.log('getValues()', getValues())
|
|
347
|
+
|
|
348
|
+
// const record = getValues()
|
|
349
|
+
|
|
350
|
+
if (type === 'enter') {
|
|
351
|
+
console.log('aaaaaa enter');
|
|
352
|
+
}
|
|
353
|
+
if (type === 'blur') {
|
|
354
|
+
// onCellChange
|
|
355
|
+
|
|
356
|
+
const handleChangeCallback = callbackData => {
|
|
357
|
+
// const newDataUpdate = updateData(data, callbackData, rowKey as any)
|
|
358
|
+
|
|
359
|
+
console.log('callbackData', callbackData);
|
|
360
|
+
const newRecord = callbackData;
|
|
361
|
+
onSubmit(newRecord);
|
|
362
|
+
};
|
|
363
|
+
if (onCellChange) {
|
|
364
|
+
if (onCellChange.length > 1) {
|
|
365
|
+
console.log('onCellChange Callback');
|
|
366
|
+
onCellChange({
|
|
367
|
+
field: '',
|
|
368
|
+
indexCol: 1,
|
|
369
|
+
type: 'onChange',
|
|
370
|
+
rows: [],
|
|
371
|
+
value: newState,
|
|
372
|
+
indexRow: 1,
|
|
373
|
+
rowData: record,
|
|
374
|
+
rowId: '',
|
|
375
|
+
rowsData: [],
|
|
376
|
+
sumValue: []
|
|
377
|
+
}, handleChangeCallback);
|
|
378
|
+
} else {
|
|
379
|
+
console.log('onCellChange');
|
|
380
|
+
onCellChange({
|
|
381
|
+
field: '',
|
|
382
|
+
indexCol: 1,
|
|
383
|
+
type: 'onChange',
|
|
384
|
+
rows: [],
|
|
385
|
+
value: newState,
|
|
386
|
+
indexRow: 1,
|
|
387
|
+
rowData: record,
|
|
388
|
+
rowId: '',
|
|
389
|
+
rowsData: [],
|
|
390
|
+
sumValue: []
|
|
391
|
+
}, handleChangeCallback);
|
|
392
|
+
onSubmit(record);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
console.log('aaaaaa blur');
|
|
396
|
+
}
|
|
397
|
+
if (prevState && newState && prevState !== newState && type === 'enter') {
|
|
398
|
+
console.log('enter');
|
|
399
|
+
onSubmit(record);
|
|
400
|
+
}
|
|
401
|
+
};
|
|
402
|
+
const handleEdit = (record, col, editType, e) => {
|
|
403
|
+
// @ts-ignore
|
|
404
|
+
setEditingKey(record[rowKey]);
|
|
405
|
+
|
|
406
|
+
// const formattedRecord = { ...record };
|
|
407
|
+
|
|
408
|
+
if (!isObjEmpty(record)) {
|
|
409
|
+
Object.entries(record).forEach(([name, value]) => {
|
|
410
|
+
setValue(name, value);
|
|
411
|
+
|
|
412
|
+
// if (listObjectDate.includes(name) && value) {
|
|
413
|
+
// setValue(name, new Date(value))
|
|
414
|
+
// } else {
|
|
415
|
+
// setValue(name, value)
|
|
416
|
+
// }
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// columns.forEach(column => {
|
|
421
|
+
//
|
|
422
|
+
// const dateFormat = getDatepickerFormat(editType, column)
|
|
423
|
+
//
|
|
424
|
+
// // @ts-ignore
|
|
425
|
+
// if ((column.editType === 'date' || column.editType === 'datetime') && record[column.dataIndex]) {
|
|
426
|
+
//
|
|
427
|
+
// // @ts-ignore
|
|
428
|
+
// formattedRecord[column.dataIndex] = !isEmpty(record[column.dataIndex]) ? convertDateToDayjs(new Date(record[column.dataIndex]), dateFormat) : null
|
|
429
|
+
// }
|
|
430
|
+
//
|
|
431
|
+
// });
|
|
432
|
+
|
|
433
|
+
if (e.key === 'Enter' || editType === 'date' || e.type === 'dblclick') {} else {
|
|
434
|
+
setValue(col.dataIndex, e.key);
|
|
435
|
+
}
|
|
436
|
+
if (editType === 'select') {
|
|
437
|
+
// setSearchValue(e.key)
|
|
438
|
+
// setOpen(true)
|
|
439
|
+
}
|
|
440
|
+
if (selectedCell?.row !== undefined && selectedCell?.col !== undefined) {
|
|
441
|
+
setTimeout(() => {
|
|
442
|
+
// const input = document.querySelector(`.ui-rc-table-row[data-row-key="${selectedCell.row}"] .cell-editing[data-col-index="${selectedCell.col}"] input`) as HTMLInputElement;
|
|
443
|
+
const input = document.querySelector(`.ui-rc-table-row .cell-editing[data-row-index="${selectedCell.row}"].cell-editing[data-col-index="${selectedCell.col}"] input`);
|
|
444
|
+
// const input = document.getElementById(`col${selectedCell.col}-record${selectedCell.row}`) as HTMLInputElement;
|
|
445
|
+
|
|
446
|
+
const select = document.querySelector(`div.cell-editing[tabindex="${selectedCell?.col}"] .ant-select-selection-search input`);
|
|
447
|
+
if (input) {
|
|
448
|
+
input.focus();
|
|
449
|
+
}
|
|
450
|
+
if (select) {
|
|
451
|
+
// @ts-ignore
|
|
452
|
+
select.searchValue = e.key;
|
|
453
|
+
// @ts-ignore
|
|
454
|
+
select.focus();
|
|
455
|
+
}
|
|
456
|
+
}, 0);
|
|
457
|
+
}
|
|
458
|
+
};
|
|
459
|
+
useEffect(() => {
|
|
460
|
+
const tableBody = document.querySelector(".ui-rc-table-tbody");
|
|
461
|
+
const tableHeader = document.querySelector(".ui-rc-table-header");
|
|
462
|
+
if (tableBody && tableHeader) {
|
|
463
|
+
tableBody.addEventListener("scroll", () => {
|
|
464
|
+
tableHeader.scrollLeft = tableBody.scrollLeft;
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
}, []);
|
|
468
|
+
const scrollToCell = (rowIndex, colIndex, cell, type) => {
|
|
469
|
+
const fixedWidth = totalFixedWidth(columns, 'left', selectionSettings);
|
|
470
|
+
if (type === 'horizontal') {
|
|
471
|
+
if (tableRef.current) {
|
|
472
|
+
tableRef.current.scrollTo({
|
|
473
|
+
left: cell.offsetLeft - fixedWidth,
|
|
474
|
+
behavior: "smooth"
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
// Cuộn header
|
|
478
|
+
// const tableHeader = document?.querySelector(".ui-rc-table-header");
|
|
479
|
+
// if (tableHeader) {
|
|
480
|
+
// tableHeader.scrollLeft = cell.offsetLeft - fixedWidth
|
|
481
|
+
// }
|
|
482
|
+
}
|
|
483
|
+
} else {
|
|
484
|
+
tableRef?.current?.scrollTo({
|
|
485
|
+
index: rowIndex,
|
|
486
|
+
behavior: 'smooth'
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
};
|
|
490
|
+
const handleFocusCell = (rowIndex, colIndex, col, type, e) => {
|
|
491
|
+
const isEdit = editingKey !== '';
|
|
492
|
+
// const cellEdit: any = document.querySelector(`.ui-rc-table-row[data-row-key="${rowIndex}"] .cell-editing[data-col-index="${colIndex}"]`);
|
|
493
|
+
// const cell: any = document.querySelector(`.ui-rc-table-row[data-row-key="${rowIndex}"] .cell-editable[data-col-index="${colIndex}"]`);
|
|
494
|
+
|
|
495
|
+
const cellEdit = document.querySelector(`.ui-rc-table-row .cell-editing[data-row-index="${rowIndex}"].cell-editing[data-col-index="${colIndex}"]`);
|
|
496
|
+
const cell = document.querySelector(`.ui-rc-table-row .cell-editable[data-row-index="${rowIndex}"].cell-editable[data-col-index="${colIndex}"]`);
|
|
497
|
+
const updateSelection = () => {
|
|
498
|
+
setSelectedCell({
|
|
499
|
+
row: rowIndex,
|
|
500
|
+
col: colIndex
|
|
501
|
+
});
|
|
502
|
+
setSelectedCells(new Set([`${rowIndex}-${colIndex}`]));
|
|
503
|
+
};
|
|
504
|
+
if (isEdit) {
|
|
505
|
+
if (cellEdit) {
|
|
506
|
+
updateSelection();
|
|
507
|
+
if (e?.key !== 'Tab') {
|
|
508
|
+
cellEdit.focus();
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
if (type === 'horizontal' && cellEdit) {
|
|
512
|
+
scrollToCell(rowIndex, colIndex, cellEdit, 'horizontal');
|
|
513
|
+
}
|
|
514
|
+
if (type === 'vertical' && cell) {
|
|
515
|
+
setEditingKey('');
|
|
516
|
+
updateSelection();
|
|
517
|
+
scrollToCell(rowIndex, colIndex, cellEdit, 'vertical');
|
|
518
|
+
if (e?.key !== 'Tab') {
|
|
519
|
+
cell.focus();
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
} else {
|
|
523
|
+
if (cell) {
|
|
524
|
+
updateSelection();
|
|
525
|
+
if (e?.key !== 'Tab') {
|
|
526
|
+
cell.focus();
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
if (cell) {
|
|
530
|
+
scrollToCell(rowIndex, colIndex, cell, type);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
};
|
|
534
|
+
const addResizeHandlers = (cols, parentPath = []) => {
|
|
535
|
+
return cols.map((col, colIndex) => {
|
|
536
|
+
const currentPath = [...parentPath, colIndex];
|
|
537
|
+
if (!col?.dataIndex && !col.key) {
|
|
538
|
+
return col;
|
|
539
|
+
}
|
|
540
|
+
if (col.children) {
|
|
541
|
+
return {
|
|
542
|
+
...col,
|
|
543
|
+
children: addResizeHandlers(col.children, currentPath)
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
if (col.dataIndex === 'index' || col.field === 'index' || col.dataIndex === '#' || col.dataIndex === '#') {
|
|
547
|
+
return {
|
|
548
|
+
...col,
|
|
549
|
+
render: (value, record, rowIndex) => {
|
|
550
|
+
return /*#__PURE__*/React.createElement("div", {
|
|
551
|
+
className: classNames('ui-rc_cell-content ui-rc_cell-content--index', {
|
|
552
|
+
selected: rowsSelected.has(`${rowIndex}-${colIndex}`),
|
|
553
|
+
focus: selectedCells && Array.from(selectedCells).some(item => item.startsWith(`${rowIndex}-`))
|
|
554
|
+
}),
|
|
555
|
+
onMouseDown: event => handleMouseDownColIndex(rowIndex, colIndex, event),
|
|
556
|
+
onMouseEnter: event => handleMouseEnterColIndex(rowIndex, colIndex, event)
|
|
557
|
+
}, /*#__PURE__*/React.createElement("div", {
|
|
558
|
+
className: 'ui-rc_content'
|
|
559
|
+
}, rowIndex + 1));
|
|
560
|
+
}
|
|
561
|
+
};
|
|
562
|
+
}
|
|
563
|
+
return {
|
|
564
|
+
...col,
|
|
565
|
+
onCell: (record, rowIndex) => ({
|
|
566
|
+
onKeyDown: event => {
|
|
567
|
+
if (event.key.length === 1 && !event.ctrlKey && !event.metaKey || event.key === 'Enter') {
|
|
568
|
+
if (!isEditing(record) && record[rowKey] !== editingKey) {
|
|
569
|
+
handleEdit(record, col, col.editType, event);
|
|
570
|
+
}
|
|
571
|
+
if (editingKey && editingKey !== '' && (rowIndex ?? 0) + 1 < dataSource.length && event.key === 'Enter') {
|
|
572
|
+
handleFocusCell((rowIndex ?? 0) + 1, colIndex, col, 'vertical', event);
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
if (event.key === 'Tab') {
|
|
576
|
+
if (colIndex + 1 !== cols.length) {
|
|
577
|
+
handleFocusCell(rowIndex, colIndex + 1, col, 'horizontal', event);
|
|
578
|
+
} else {
|
|
579
|
+
event.stopPropagation();
|
|
580
|
+
event.preventDefault();
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
if (event.key === 'ArrowRight' && colIndex + 1 !== cols.length) {
|
|
584
|
+
if (editingKey !== '') {} else {
|
|
585
|
+
handleFocusCell(rowIndex, colIndex + 1, col, 'horizontal', event);
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
if (event.key === 'ArrowLeft' && colIndex > 0) {
|
|
589
|
+
if (!col.dataIndex && !col.key || col.field === 'index' || col.field === '#' || col.dataIndex === 'index' || col.dataIndex === '#') {
|
|
590
|
+
event.stopPropagation();
|
|
591
|
+
event.preventDefault();
|
|
592
|
+
} else {
|
|
593
|
+
if (editingKey !== '') {} else {
|
|
594
|
+
handleFocusCell(rowIndex, colIndex - 1, col, 'horizontal', event);
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
if (event.key === 'ArrowDown' && (rowIndex ?? 0) + 1 < dataSource.length) {
|
|
599
|
+
handleFocusCell((rowIndex ?? 0) + 1, colIndex, col, 'vertical', event);
|
|
600
|
+
}
|
|
601
|
+
if (event.key === 'ArrowUp' && (rowIndex ?? 0) > 0) {
|
|
602
|
+
handleFocusCell((rowIndex ?? 0) - 1, colIndex, col, 'vertical', event);
|
|
603
|
+
}
|
|
604
|
+
},
|
|
605
|
+
onPaste: event => {
|
|
606
|
+
if (editingKey === '') {
|
|
607
|
+
handlePaste(event, colIndex, rowIndex);
|
|
608
|
+
event.preventDefault();
|
|
609
|
+
}
|
|
610
|
+
},
|
|
611
|
+
onCopy: e => {
|
|
612
|
+
if (editingKey === '') {
|
|
613
|
+
handleCopy(e);
|
|
614
|
+
e.preventDefault();
|
|
615
|
+
}
|
|
616
|
+
},
|
|
617
|
+
onDoubleClick: event => {
|
|
618
|
+
if (!isEditing(record) && record[rowKey] !== editingKey) {
|
|
619
|
+
handleEdit(record, col, col.editType, event);
|
|
620
|
+
}
|
|
621
|
+
},
|
|
622
|
+
onClick: () => {
|
|
623
|
+
if (record[rowKey] !== editingKey && editingKey !== '') {
|
|
624
|
+
setEditingKey('');
|
|
625
|
+
}
|
|
626
|
+
},
|
|
627
|
+
className: isEditing(record) ? 'cell-editing' : 'cell-editable',
|
|
628
|
+
record,
|
|
629
|
+
column: col,
|
|
630
|
+
editType: getEditType(col, record),
|
|
631
|
+
dataIndex: col.dataIndex,
|
|
632
|
+
title: col.title,
|
|
633
|
+
'data-col-index': colIndex,
|
|
634
|
+
'data-row-index': rowIndex,
|
|
635
|
+
colIndex: colIndex,
|
|
636
|
+
indexCol: colIndex,
|
|
637
|
+
indexRow: rowIndex,
|
|
638
|
+
editing: isEditing(record),
|
|
639
|
+
tabIndex: (rowIndex ?? 0) * columns.length + colIndex
|
|
640
|
+
}),
|
|
641
|
+
render: (value, record, rowIndex) => {
|
|
642
|
+
return /*#__PURE__*/React.createElement("div", {
|
|
643
|
+
className: classNames('ui-rc_cell-content', {
|
|
644
|
+
selected: selectedCells.has(`${rowIndex}-${colIndex}`)
|
|
645
|
+
}),
|
|
646
|
+
onMouseDown: event => handleMouseDown(rowIndex, colIndex, event),
|
|
647
|
+
onMouseEnter: () => handleMouseEnter(rowIndex, colIndex),
|
|
648
|
+
onMouseLeave: () => {}
|
|
649
|
+
}, /*#__PURE__*/React.createElement("div", {
|
|
650
|
+
className: 'ui-rc_content'
|
|
651
|
+
}, renderContent(col, value, record, rowIndex)));
|
|
652
|
+
}
|
|
653
|
+
};
|
|
654
|
+
});
|
|
655
|
+
};
|
|
656
|
+
const resizableColumns = addResizeHandlers(columns);
|
|
657
|
+
return /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(GridStyle, {
|
|
658
|
+
heightTable: height,
|
|
659
|
+
style: {
|
|
660
|
+
position: 'relative'
|
|
661
|
+
},
|
|
662
|
+
onMouseUp: handleMouseUp
|
|
663
|
+
}, /*#__PURE__*/React.createElement(TableContext.Provider, {
|
|
664
|
+
value: {
|
|
665
|
+
rowKey,
|
|
666
|
+
form,
|
|
667
|
+
format,
|
|
668
|
+
control,
|
|
669
|
+
trigger,
|
|
670
|
+
getValues,
|
|
671
|
+
handleCellChange
|
|
672
|
+
}
|
|
673
|
+
}, /*#__PURE__*/React.createElement("form", {
|
|
674
|
+
onSubmit: handleSubmit(onSubmit)
|
|
675
|
+
}, /*#__PURE__*/React.createElement(TableGrid, _extends({}, rest, {
|
|
676
|
+
t: t,
|
|
677
|
+
tableRef: tableRef,
|
|
678
|
+
dataSource: dataSource,
|
|
679
|
+
components: {
|
|
680
|
+
...components,
|
|
681
|
+
body: {
|
|
682
|
+
cell: EditableCell
|
|
683
|
+
}
|
|
684
|
+
},
|
|
685
|
+
className: 'grid-editable',
|
|
686
|
+
rowKey: rowKey,
|
|
687
|
+
columns: resizableColumns,
|
|
688
|
+
showSorterTooltip: {
|
|
689
|
+
target: 'sorter-icon'
|
|
690
|
+
},
|
|
691
|
+
format: format,
|
|
692
|
+
selectionSettings: selectionSettings ? {
|
|
693
|
+
...selectionSettings,
|
|
694
|
+
checkboxOnly: true
|
|
695
|
+
} : undefined,
|
|
696
|
+
style: {
|
|
697
|
+
minHeight: height
|
|
698
|
+
}
|
|
699
|
+
}))))), /*#__PURE__*/React.createElement(Toaster, {
|
|
700
|
+
position: toast,
|
|
701
|
+
toastOptions: {
|
|
702
|
+
className: 'react-hot-toast'
|
|
703
|
+
}
|
|
704
|
+
}));
|
|
705
|
+
};
|
|
706
|
+
export default GridEdit;
|