@hzab/edit-table 0.1.0 → 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 CHANGED
@@ -1,4 +1,12 @@
1
- # @hzab/edit-table@0.0.2
1
+ # @hzab/edit-table@0.1.2
2
+
3
+ fix: checkbox 选择子项时拿到对应行的数据
4
+
5
+ # @hzab/edit-table@0.1.1
6
+
7
+ fix: 无数据时取消聚焦框
8
+
9
+ # @hzab/edit-table@0.1.0
2
10
 
3
11
  feat: 组件优化,批量提交
4
12
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hzab/edit-table",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "",
5
5
  "main": "src",
6
6
  "scripts": {
package/src/index.tsx CHANGED
@@ -206,11 +206,12 @@ export default function CellEditTable(props: CellEditTableProps) {
206
206
  }),
207
207
  };
208
208
  });
209
- if (firstRender.current) {
210
- initRowsRef.current = [...headerList, ...tempDataSource];
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([...headerList, ...tempDataSource]);
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 == dataSource?.length && dataSource?.length > 0)
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 < dataSource?.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 = dataSource?.filter((el) => newSelectedRowKeys?.includes(el[rowKey]));
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 = dataSource?.filter((el) => newSelectedRowKeys?.includes(el[rowKey]));
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 = dataSource?.map((el) => el[rowKey]);
328
- const rows = dataSource?.filter((el) => newSelectedRowKeys?.includes(el[rowKey]));
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
- dataSource?.map((el) => el[rowKey]),
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 rows = dataSource?.filter((el) => selectedRowKeys?.includes(el[rowKey]));
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,8 @@ export default function CellEditTable(props: CellEditTableProps) {
362
377
  <div className={`grid-container ${className}`} style={reactGridStyle}>
363
378
  <ReactGrid
364
379
  ref={gridRef}
365
- rows={loading ? initRowsRef.current : rows}
380
+ rows={rows}
381
+ key={dataSource?.length == 0 ? 0 : 1}
366
382
  columns={tableColumns}
367
383
  onCellsChanged={handleCellsChanged}
368
384
  enableFillHandle={true}