@hzab/edit-table 0.1.1 → 0.1.2
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 +4 -0
- package/package.json +1 -1
- package/src/index.tsx +28 -13
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
package/src/index.tsx
CHANGED
|
@@ -206,11 +206,12 @@ export default function CellEditTable(props: CellEditTableProps) {
|
|
|
206
206
|
}),
|
|
207
207
|
};
|
|
208
208
|
});
|
|
209
|
-
|
|
210
|
-
|
|
209
|
+
const mergerList = [...headerList, ...tempDataSource];
|
|
210
|
+
if (firstRender.current && tempDataSource?.length > 0) {
|
|
211
|
+
initRowsRef.current = mergerList;
|
|
211
212
|
firstRender.current = false;
|
|
212
213
|
}
|
|
213
|
-
setRows(
|
|
214
|
+
setRows(mergerList);
|
|
214
215
|
}, [newDataSource, tableColumns, headerList]);
|
|
215
216
|
|
|
216
217
|
// 单元格编辑回调
|
|
@@ -294,41 +295,54 @@ export default function CellEditTable(props: CellEditTableProps) {
|
|
|
294
295
|
</div>
|
|
295
296
|
);
|
|
296
297
|
};
|
|
297
|
-
|
|
298
|
+
function flattenDataSource(arr: any) {
|
|
299
|
+
const result: any = [];
|
|
300
|
+
function loop(list: any) {
|
|
301
|
+
for (const item of list) {
|
|
302
|
+
result.push(item);
|
|
303
|
+
if (item.children && item.children.length) {
|
|
304
|
+
loop(item.children);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
loop(arr);
|
|
309
|
+
return result;
|
|
310
|
+
}
|
|
298
311
|
const CheckBoxDom = (props) => {
|
|
299
312
|
const { record, rowId } = props?.cell;
|
|
313
|
+
const newDataSource = flattenDataSource(dataSource);
|
|
300
314
|
return (
|
|
301
315
|
<Checkbox
|
|
302
316
|
{...props?.componentProps}
|
|
303
317
|
checked={
|
|
304
318
|
rowSelection?.selectedRowKeys?.includes((record && record[rowKey]) || "") ||
|
|
305
|
-
(rowSelection?.selectedRowKeys?.length ==
|
|
319
|
+
(rowSelection?.selectedRowKeys?.length == newDataSource?.length && newDataSource?.length > 0)
|
|
306
320
|
}
|
|
307
321
|
indeterminate={
|
|
308
322
|
!rowId &&
|
|
309
323
|
rowSelection?.selectedRowKeys?.length > 0 &&
|
|
310
|
-
rowSelection?.selectedRowKeys?.length <
|
|
324
|
+
rowSelection?.selectedRowKeys?.length < newDataSource?.length
|
|
311
325
|
}
|
|
312
326
|
onChange={(e) => {
|
|
313
327
|
if (e.target.checked && rowId) {
|
|
314
328
|
const newSelectedRowKeys = [...rowSelection?.selectedRowKeys, record[rowKey]];
|
|
315
|
-
const rows =
|
|
329
|
+
const rows = newDataSource?.filter((el) => newSelectedRowKeys?.includes(el[rowKey]));
|
|
316
330
|
|
|
317
331
|
rowSelection?.onChange(newSelectedRowKeys, rows);
|
|
318
332
|
} else if (!e.target.checked && rowId) {
|
|
319
333
|
const newSelectedRowKeys = rowSelection?.selectedRowKeys?.filter((el) => el != record[rowKey]);
|
|
320
|
-
const rows =
|
|
334
|
+
const rows = newDataSource?.filter((el) => newSelectedRowKeys?.includes(el[rowKey]));
|
|
321
335
|
|
|
322
336
|
rowSelection?.onChange(
|
|
323
337
|
rowSelection?.selectedRowKeys?.filter((el) => el != record[rowKey]),
|
|
324
338
|
rows,
|
|
325
339
|
);
|
|
326
340
|
} else if (e.target.checked && !rowId) {
|
|
327
|
-
const newSelectedRowKeys =
|
|
328
|
-
const rows =
|
|
341
|
+
const newSelectedRowKeys = newDataSource?.map((el) => el[rowKey]);
|
|
342
|
+
const rows = newDataSource?.filter((el) => newSelectedRowKeys?.includes(el[rowKey]));
|
|
329
343
|
|
|
330
344
|
rowSelection?.onChange(
|
|
331
|
-
|
|
345
|
+
newDataSource?.map((el) => el[rowKey]),
|
|
332
346
|
rows,
|
|
333
347
|
);
|
|
334
348
|
} else if (!e.target.checked && !rowId) {
|
|
@@ -340,7 +354,8 @@ export default function CellEditTable(props: CellEditTableProps) {
|
|
|
340
354
|
};
|
|
341
355
|
useEffect(() => {
|
|
342
356
|
if (rowSelection) {
|
|
343
|
-
const
|
|
357
|
+
const newDataSource = flattenDataSource(dataSource);
|
|
358
|
+
const rows = newDataSource?.filter((el) => selectedRowKeys?.includes(el[rowKey]));
|
|
344
359
|
rowSelection?.onChange(selectedRowKeys, rows);
|
|
345
360
|
}
|
|
346
361
|
}, [selectedRowKeys, dataSource]);
|
|
@@ -362,7 +377,7 @@ export default function CellEditTable(props: CellEditTableProps) {
|
|
|
362
377
|
<div className={`grid-container ${className}`} style={reactGridStyle}>
|
|
363
378
|
<ReactGrid
|
|
364
379
|
ref={gridRef}
|
|
365
|
-
rows={
|
|
380
|
+
rows={rows}
|
|
366
381
|
key={dataSource?.length == 0 ? 0 : 1}
|
|
367
382
|
columns={tableColumns}
|
|
368
383
|
onCellsChanged={handleCellsChanged}
|