@@ -14,6 +14,7 @@ import {
14
14
DataSheetGridRef ,
15
15
HeaderContextType ,
16
16
ListItemData ,
17
+ Operation ,
17
18
Selection ,
18
19
SelectionContextType ,
19
20
} from '../types'
@@ -316,11 +317,20 @@ export const DataSheetGrid = React.memo(
316
317
setSelectionCell ( null )
317
318
setEditing ( false )
318
319
319
- onChange ( [
320
- ...dataRef . current . slice ( 0 , row + 1 ) ,
321
- ...new Array ( count ) . fill ( 0 ) . map ( createRow ) ,
322
- ...dataRef . current . slice ( row + 1 ) ,
323
- ] )
320
+ onChange (
321
+ [
322
+ ...dataRef . current . slice ( 0 , row + 1 ) ,
323
+ ...new Array ( count ) . fill ( 0 ) . map ( createRow ) ,
324
+ ...dataRef . current . slice ( row + 1 ) ,
325
+ ] ,
326
+ [
327
+ {
328
+ type : 'CREATE' ,
329
+ fromRowIndex : row + 1 ,
330
+ toRowIndex : row + 1 + count ,
331
+ } ,
332
+ ]
333
+ )
324
334
setActiveCell ( ( a ) => ( { col : a ?. col || 0 , row : row + count } ) )
325
335
} ,
326
336
[ createRow , lockRows , onChange , setActiveCell , setSelectionCell ]
@@ -332,15 +342,24 @@ export const DataSheetGrid = React.memo(
332
342
return
333
343
}
334
344
335
- onChange ( [
336
- ...dataRef . current . slice ( 0 , rowMax + 1 ) ,
337
- ...dataRef . current
338
- . slice ( rowMin , rowMax + 1 )
339
- . map ( ( rowData , i ) =>
340
- duplicateRow ( { rowData, rowIndex : i + rowMin } )
341
- ) ,
342
- ...dataRef . current . slice ( rowMax + 1 ) ,
343
- ] )
345
+ onChange (
346
+ [
347
+ ...dataRef . current . slice ( 0 , rowMax + 1 ) ,
348
+ ...dataRef . current
349
+ . slice ( rowMin , rowMax + 1 )
350
+ . map ( ( rowData , i ) =>
351
+ duplicateRow ( { rowData, rowIndex : i + rowMin } )
352
+ ) ,
353
+ ...dataRef . current . slice ( rowMax + 1 ) ,
354
+ ] ,
355
+ [
356
+ {
357
+ type : 'CREATE' ,
358
+ fromRowIndex : rowMax + 1 ,
359
+ toRowIndex : rowMax + 2 + rowMax - rowMin ,
360
+ } ,
361
+ ]
362
+ )
344
363
setActiveCell ( { col : 0 , row : rowMax + 1 } )
345
364
setSelectionCell ( {
346
365
col : columns . length - ( hasStickyRightColumn ? 3 : 2 ) ,
@@ -430,11 +449,20 @@ export const DataSheetGrid = React.memo(
430
449
431
450
const setRowData = useCallback (
432
451
( rowIndex : number , item : T ) => {
433
- onChange ( [
434
- ...dataRef . current ?. slice ( 0 , rowIndex ) ,
435
- item ,
436
- ...dataRef . current ?. slice ( rowIndex + 1 ) ,
437
- ] )
452
+ onChange (
453
+ [
454
+ ...dataRef . current ?. slice ( 0 , rowIndex ) ,
455
+ item ,
456
+ ...dataRef . current ?. slice ( rowIndex + 1 ) ,
457
+ ] ,
458
+ [
459
+ {
460
+ type : 'UPDATE' ,
461
+ fromRowIndex : rowIndex ,
462
+ toRowIndex : rowIndex + 1 ,
463
+ } ,
464
+ ]
465
+ )
438
466
} ,
439
467
[ onChange ]
440
468
)
@@ -459,10 +487,19 @@ export const DataSheetGrid = React.memo(
459
487
return a && { ...a , row }
460
488
} )
461
489
setSelectionCell ( null )
462
- onChange ( [
463
- ...dataRef . current . slice ( 0 , rowMin ) ,
464
- ...dataRef . current . slice ( rowMax + 1 ) ,
465
- ] )
490
+ onChange (
491
+ [
492
+ ...dataRef . current . slice ( 0 , rowMin ) ,
493
+ ...dataRef . current . slice ( rowMax + 1 ) ,
494
+ ] ,
495
+ [
496
+ {
497
+ type : 'DELETE' ,
498
+ fromRowIndex : rowMin ,
499
+ toRowIndex : rowMax + 1 ,
500
+ } ,
501
+ ]
502
+ )
466
503
} ,
467
504
[ lockRows , onChange , setActiveCell , setSelectionCell ]
468
505
)
@@ -515,7 +552,13 @@ export const DataSheetGrid = React.memo(
515
552
return
516
553
}
517
554
518
- onChange ( newData )
555
+ onChange ( newData , [
556
+ {
557
+ type : 'UPDATE' ,
558
+ fromRowIndex : min . row ,
559
+ toRowIndex : max . row + 1 ,
560
+ } ,
561
+ ] )
519
562
} ,
520
563
[
521
564
activeCell ,
@@ -666,7 +709,13 @@ export const DataSheetGrid = React.memo(
666
709
}
667
710
}
668
711
669
- onChange ( newData )
712
+ onChange ( newData , [
713
+ {
714
+ type : 'UPDATE' ,
715
+ fromRowIndex : min . row ,
716
+ toRowIndex : max . row + 1 ,
717
+ } ,
718
+ ] )
670
719
setActiveCell ( { col : min . col , row : min . row } )
671
720
setSelectionCell ( {
672
721
col : Math . min (
@@ -723,7 +772,26 @@ export const DataSheetGrid = React.memo(
723
772
}
724
773
}
725
774
726
- onChange ( newData )
775
+ const operations : Operation [ ] = [
776
+ {
777
+ type : 'UPDATE' ,
778
+ fromRowIndex : min . row ,
779
+ toRowIndex :
780
+ min . row +
781
+ pasteData . length -
782
+ ( ! lockRows && missingRows > 0 ? missingRows : 0 ) ,
783
+ } ,
784
+ ]
785
+
786
+ if ( missingRows > 0 && ! lockRows ) {
787
+ operations . push ( {
788
+ type : 'CREATE' ,
789
+ fromRowIndex : min . row + pasteData . length - missingRows ,
790
+ toRowIndex : min . row + pasteData . length ,
791
+ } )
792
+ }
793
+
794
+ onChange ( newData , operations )
727
795
setActiveCell ( { col : min . col , row : min . row } )
728
796
setSelectionCell ( {
729
797
col : Math . min (
@@ -1009,7 +1077,13 @@ export const DataSheetGrid = React.memo(
1009
1077
}
1010
1078
}
1011
1079
1012
- onChange ( newData )
1080
+ onChange ( newData , [
1081
+ {
1082
+ type : 'UPDATE' ,
1083
+ fromRowIndex : max . row + 1 ,
1084
+ toRowIndex : max . row + 1 + expandSelectionRowsCount ,
1085
+ } ,
1086
+ ] )
1013
1087
setExpandSelectionRowsCount ( 0 )
1014
1088
setActiveCell ( {
1015
1089
col : Math . min (
0 commit comments