@@ -39,18 +39,19 @@ import {
3939import type {
4040 CalculatedColumn ,
4141 CellClickArgs ,
42+ CellClipboardEvent ,
43+ CellCopyEvent ,
4244 CellKeyboardEvent ,
4345 CellKeyDownArgs ,
4446 CellMouseEvent ,
4547 CellNavigationMode ,
48+ CellPasteEvent ,
4649 CellSelectArgs ,
4750 Column ,
4851 ColumnOrColumnGroup ,
49- CopyEvent ,
5052 Direction ,
5153 FillEvent ,
5254 Maybe ,
53- PasteEvent ,
5455 Position ,
5556 Renderers ,
5657 RowsChangeData ,
@@ -175,8 +176,6 @@ export interface DataGridProps<R, SR = unknown, K extends Key = Key> extends Sha
175176 onSortColumnsChange ?: Maybe < ( sortColumns : SortColumn [ ] ) => void > ;
176177 defaultColumnOptions ?: Maybe < DefaultColumnOptions < NoInfer < R > , NoInfer < SR > > > ;
177178 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 > > ;
180179
181180 /**
182181 * Event props
@@ -196,6 +195,12 @@ export interface DataGridProps<R, SR = unknown, K extends Key = Key> extends Sha
196195 onCellKeyDown ?: Maybe <
197196 ( args : CellKeyDownArgs < NoInfer < R > , NoInfer < SR > > , event : CellKeyboardEvent ) => void
198197 > ;
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+ > ;
199204 /** Function called whenever cell selection is changed */
200205 onSelectedCellChange ?: Maybe < ( args : CellSelectArgs < NoInfer < R > , NoInfer < SR > > ) => void > ;
201206 /** Called when the grid is scrolled */
@@ -260,8 +265,8 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
260265 onColumnResize,
261266 onColumnsReorder,
262267 onFill,
263- onCopy ,
264- onPaste ,
268+ onCellCopy ,
269+ onCellPaste ,
265270 // Toggles and modes
266271 enableVirtualization : rawEnableVirtualization ,
267272 // Miscellaneous
@@ -310,7 +315,6 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
310315 const [ measuredColumnWidths , setMeasuredColumnWidths ] = useState (
311316 ( ) : ReadonlyMap < string , number > => new Map ( )
312317 ) ;
313- const [ copiedCell , setCopiedCell ] = useState < { row : R ; columnKey : string } | null > ( null ) ;
314318 const [ isDragging , setDragging ] = useState ( false ) ;
315319 const [ draggedOverRowIdx , setOverRowIdx ] = useState < number | undefined > ( undefined ) ;
316320 const [ scrollToPosition , setScrollToPosition ] = useState < PartialPosition | null > ( null ) ;
@@ -608,39 +612,13 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
608612 ) ;
609613 if ( cellEvent . isGridDefaultPrevented ( ) ) return ;
610614 }
615+
611616 if ( ! ( event . target instanceof Element ) ) return ;
612617 const isCellEvent = event . target . closest ( '.rdg-cell' ) !== null ;
613618 const isRowEvent = isTreeGrid && event . target === focusSinkRef . current ;
614619 if ( ! isCellEvent && ! isRowEvent ) return ;
615620
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-
640621 switch ( event . key ) {
641- case 'Escape' :
642- setCopiedCell ( null ) ;
643- return ;
644622 case 'ArrowUp' :
645623 case 'ArrowDown' :
646624 case 'ArrowLeft' :
@@ -684,31 +662,21 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
684662 updateRow ( columns [ selectedPosition . idx ] , selectedPosition . rowIdx , selectedPosition . row ) ;
685663 }
686664
687- function handleCopy ( ) {
665+ function handleCellCopy ( event : CellClipboardEvent ) {
666+ if ( ! selectedCellIsWithinViewportBounds ) return ;
688667 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 ) ;
693669 }
694670
695- function handlePaste ( ) {
696- if ( ! onPaste || ! onRowsChange || copiedCell === null || ! isCellEditable ( selectedPosition ) ) {
671+ function handleCellPaste ( event : CellClipboardEvent ) {
672+ if ( ! onCellPaste || ! onRowsChange || ! isCellEditable ( selectedPosition ) ) {
697673 return ;
698674 }
699675
700676 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 ) ;
712680 }
713681
714682 function handleCellInput ( event : KeyboardEvent < HTMLDivElement > ) {
@@ -726,7 +694,7 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
726694 return ;
727695 }
728696
729- if ( isCellEditable ( selectedPosition ) && isDefaultCellInput ( event ) ) {
697+ if ( isCellEditable ( selectedPosition ) && isDefaultCellInput ( event , onCellPaste != null ) ) {
730698 setSelectedPosition ( ( { idx, rowIdx } ) => ( {
731699 idx,
732700 rowIdx,
@@ -1051,11 +1019,6 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
10511019 onCellContextMenu : onCellContextMenuLatest ,
10521020 rowClass,
10531021 gridRowStart,
1054- copiedCellIdx :
1055- copiedCell !== null && copiedCell . row === row
1056- ? columns . findIndex ( ( c ) => c . key === copiedCell . columnKey )
1057- : undefined ,
1058-
10591022 selectedCellIdx : selectedRowIdx === rowIdx ? selectedIdx : undefined ,
10601023 draggedOverCellIdx : getDraggedOverCellIdx ( rowIdx ) ,
10611024 setDraggedOverRowIdx : isDragging ? setDraggedOverRowIdx : undefined ,
@@ -1135,6 +1098,8 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
11351098 ref = { gridRef }
11361099 onScroll = { handleScroll }
11371100 onKeyDown = { handleKeyDown }
1101+ onCopy = { handleCellCopy }
1102+ onPaste = { handleCellPaste }
11381103 data-testid = { testId }
11391104 data-cy = { dataCy }
11401105 >
0 commit comments