@@ -39,18 +39,19 @@ import {
39
39
import type {
40
40
CalculatedColumn ,
41
41
CellClickArgs ,
42
+ CellClipboardEvent ,
43
+ CellCopyEvent ,
42
44
CellKeyboardEvent ,
43
45
CellKeyDownArgs ,
44
46
CellMouseEvent ,
45
47
CellNavigationMode ,
48
+ CellPasteEvent ,
46
49
CellSelectArgs ,
47
50
Column ,
48
51
ColumnOrColumnGroup ,
49
- CopyEvent ,
50
52
Direction ,
51
53
FillEvent ,
52
54
Maybe ,
53
- PasteEvent ,
54
55
Position ,
55
56
Renderers ,
56
57
RowsChangeData ,
@@ -175,8 +176,6 @@ export interface DataGridProps<R, SR = unknown, K extends Key = Key> extends Sha
175
176
onSortColumnsChange ?: Maybe < ( sortColumns : SortColumn [ ] ) => void > ;
176
177
defaultColumnOptions ?: Maybe < DefaultColumnOptions < NoInfer < R > , NoInfer < SR > > > ;
177
178
onFill ?: Maybe < ( event : FillEvent < NoInfer < R > > ) => NoInfer < R > > ;
178
- onCopy ?: Maybe < ( event : CopyEvent < NoInfer < R > > ) => void > ;
179
- onPaste ?: Maybe < ( event : PasteEvent < NoInfer < R > > ) => NoInfer < R > > ;
180
179
181
180
/**
182
181
* Event props
@@ -196,6 +195,12 @@ export interface DataGridProps<R, SR = unknown, K extends Key = Key> extends Sha
196
195
onCellKeyDown ?: Maybe <
197
196
( args : CellKeyDownArgs < NoInfer < R > , NoInfer < SR > > , event : CellKeyboardEvent ) => void
198
197
> ;
198
+ onCellCopy ?: Maybe <
199
+ ( args : CellCopyEvent < NoInfer < R > , NoInfer < SR > > , event : CellClipboardEvent ) => void
200
+ > ;
201
+ onCellPaste ?: Maybe <
202
+ ( args : CellPasteEvent < NoInfer < R > , NoInfer < SR > > , event : CellClipboardEvent ) => NoInfer < R >
203
+ > ;
199
204
/** Function called whenever cell selection is changed */
200
205
onSelectedCellChange ?: Maybe < ( args : CellSelectArgs < NoInfer < R > , NoInfer < SR > > ) => void > ;
201
206
/** Called when the grid is scrolled */
@@ -260,8 +265,8 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
260
265
onColumnResize,
261
266
onColumnsReorder,
262
267
onFill,
263
- onCopy ,
264
- onPaste ,
268
+ onCellCopy ,
269
+ onCellPaste ,
265
270
// Toggles and modes
266
271
enableVirtualization : rawEnableVirtualization ,
267
272
// Miscellaneous
@@ -310,7 +315,6 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
310
315
const [ measuredColumnWidths , setMeasuredColumnWidths ] = useState (
311
316
( ) : ReadonlyMap < string , number > => new Map ( )
312
317
) ;
313
- const [ copiedCell , setCopiedCell ] = useState < { row : R ; columnKey : string } | null > ( null ) ;
314
318
const [ isDragging , setDragging ] = useState ( false ) ;
315
319
const [ draggedOverRowIdx , setOverRowIdx ] = useState < number | undefined > ( undefined ) ;
316
320
const [ scrollToPosition , setScrollToPosition ] = useState < PartialPosition | null > ( null ) ;
@@ -608,39 +612,13 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
608
612
) ;
609
613
if ( cellEvent . isGridDefaultPrevented ( ) ) return ;
610
614
}
615
+
611
616
if ( ! ( event . target instanceof Element ) ) return ;
612
617
const isCellEvent = event . target . closest ( '.rdg-cell' ) !== null ;
613
618
const isRowEvent = isTreeGrid && event . target === focusSinkRef . current ;
614
619
if ( ! isCellEvent && ! isRowEvent ) return ;
615
620
616
- // eslint-disable-next-line @typescript-eslint/no-deprecated
617
- const { keyCode } = event ;
618
-
619
- if (
620
- selectedCellIsWithinViewportBounds &&
621
- ( onPaste != null || onCopy != null ) &&
622
- isCtrlKeyHeldDown ( event )
623
- ) {
624
- // event.key may differ by keyboard input language, so we use event.keyCode instead
625
- // event.nativeEvent.code cannot be used either as it would break copy/paste for the DVORAK layout
626
- const cKey = 67 ;
627
- const vKey = 86 ;
628
- if ( keyCode === cKey ) {
629
- // copy highlighted text only
630
- if ( window . getSelection ( ) ?. isCollapsed === false ) return ;
631
- handleCopy ( ) ;
632
- return ;
633
- }
634
- if ( keyCode === vKey ) {
635
- handlePaste ( ) ;
636
- return ;
637
- }
638
- }
639
-
640
621
switch ( event . key ) {
641
- case 'Escape' :
642
- setCopiedCell ( null ) ;
643
- return ;
644
622
case 'ArrowUp' :
645
623
case 'ArrowDown' :
646
624
case 'ArrowLeft' :
@@ -684,31 +662,21 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
684
662
updateRow ( columns [ selectedPosition . idx ] , selectedPosition . rowIdx , selectedPosition . row ) ;
685
663
}
686
664
687
- function handleCopy ( ) {
665
+ function handleCellCopy ( event : CellClipboardEvent ) {
666
+ if ( ! selectedCellIsWithinViewportBounds ) return ;
688
667
const { idx, rowIdx } = selectedPosition ;
689
- const sourceRow = rows [ rowIdx ] ;
690
- const sourceColumnKey = columns [ idx ] . key ;
691
- setCopiedCell ( { row : sourceRow , columnKey : sourceColumnKey } ) ;
692
- onCopy ?.( { sourceRow, sourceColumnKey } ) ;
668
+ onCellCopy ?.( { row : rows [ rowIdx ] , column : columns [ idx ] } , event ) ;
693
669
}
694
670
695
- function handlePaste ( ) {
696
- if ( ! onPaste || ! onRowsChange || copiedCell === null || ! isCellEditable ( selectedPosition ) ) {
671
+ function handleCellPaste ( event : CellClipboardEvent ) {
672
+ if ( ! onCellPaste || ! onRowsChange || ! isCellEditable ( selectedPosition ) ) {
697
673
return ;
698
674
}
699
675
700
676
const { idx, rowIdx } = selectedPosition ;
701
- const targetColumn = columns [ idx ] ;
702
- const targetRow = rows [ rowIdx ] ;
703
-
704
- const updatedTargetRow = onPaste ( {
705
- sourceRow : copiedCell . row ,
706
- sourceColumnKey : copiedCell . columnKey ,
707
- targetRow,
708
- targetColumnKey : targetColumn . key
709
- } ) ;
710
-
711
- updateRow ( targetColumn , rowIdx , updatedTargetRow ) ;
677
+ const column = columns [ idx ] ;
678
+ const updatedRow = onCellPaste ( { row : rows [ rowIdx ] , column } , event ) ;
679
+ updateRow ( column , rowIdx , updatedRow ) ;
712
680
}
713
681
714
682
function handleCellInput ( event : KeyboardEvent < HTMLDivElement > ) {
@@ -726,7 +694,7 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
726
694
return ;
727
695
}
728
696
729
- if ( isCellEditable ( selectedPosition ) && isDefaultCellInput ( event ) ) {
697
+ if ( isCellEditable ( selectedPosition ) && isDefaultCellInput ( event , onCellPaste != null ) ) {
730
698
setSelectedPosition ( ( { idx, rowIdx } ) => ( {
731
699
idx,
732
700
rowIdx,
@@ -1051,11 +1019,6 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
1051
1019
onCellContextMenu : onCellContextMenuLatest ,
1052
1020
rowClass,
1053
1021
gridRowStart,
1054
- copiedCellIdx :
1055
- copiedCell !== null && copiedCell . row === row
1056
- ? columns . findIndex ( ( c ) => c . key === copiedCell . columnKey )
1057
- : undefined ,
1058
-
1059
1022
selectedCellIdx : selectedRowIdx === rowIdx ? selectedIdx : undefined ,
1060
1023
draggedOverCellIdx : getDraggedOverCellIdx ( rowIdx ) ,
1061
1024
setDraggedOverRowIdx : isDragging ? setDraggedOverRowIdx : undefined ,
@@ -1135,6 +1098,8 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
1135
1098
ref = { gridRef }
1136
1099
onScroll = { handleScroll }
1137
1100
onKeyDown = { handleKeyDown }
1101
+ onCopy = { handleCellCopy }
1102
+ onPaste = { handleCellPaste }
1138
1103
data-testid = { testId }
1139
1104
data-cy = { dataCy }
1140
1105
>
0 commit comments