@@ -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 ) ;
@@ -611,39 +615,13 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
611615 ) ;
612616 if ( cellEvent . isGridDefaultPrevented ( ) ) return ;
613617 }
618+
614619 if ( ! ( event . target instanceof Element ) ) return ;
615620 const isCellEvent = event . target . closest ( '.rdg-cell' ) !== null ;
616621 const isRowEvent = isTreeGrid && event . target === focusSinkRef . current ;
617622 if ( ! isCellEvent && ! isRowEvent ) return ;
618623
619- // eslint-disable-next-line @typescript-eslint/no-deprecated
620- const { keyCode } = event ;
621-
622- if (
623- selectedCellIsWithinViewportBounds &&
624- ( onPaste != null || onCopy != null ) &&
625- isCtrlKeyHeldDown ( event )
626- ) {
627- // event.key may differ by keyboard input language, so we use event.keyCode instead
628- // event.nativeEvent.code cannot be used either as it would break copy/paste for the DVORAK layout
629- const cKey = 67 ;
630- const vKey = 86 ;
631- if ( keyCode === cKey ) {
632- // copy highlighted text only
633- if ( window . getSelection ( ) ?. isCollapsed === false ) return ;
634- handleCopy ( ) ;
635- return ;
636- }
637- if ( keyCode === vKey ) {
638- handlePaste ( ) ;
639- return ;
640- }
641- }
642-
643624 switch ( event . key ) {
644- case 'Escape' :
645- setCopiedCell ( null ) ;
646- return ;
647625 case 'ArrowUp' :
648626 case 'ArrowDown' :
649627 case 'ArrowLeft' :
@@ -687,31 +665,21 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
687665 updateRow ( columns [ selectedPosition . idx ] , selectedPosition . rowIdx , selectedPosition . row ) ;
688666 }
689667
690- function handleCopy ( ) {
668+ function handleCellCopy ( event : CellClipboardEvent ) {
669+ if ( ! selectedCellIsWithinViewportBounds ) return ;
691670 const { idx, rowIdx } = selectedPosition ;
692- const sourceRow = rows [ rowIdx ] ;
693- const sourceColumnKey = columns [ idx ] . key ;
694- setCopiedCell ( { row : sourceRow , columnKey : sourceColumnKey } ) ;
695- onCopy ?.( { sourceRow, sourceColumnKey } ) ;
671+ onCellCopy ?.( { row : rows [ rowIdx ] , column : columns [ idx ] } , event ) ;
696672 }
697673
698- function handlePaste ( ) {
699- if ( ! onPaste || ! onRowsChange || copiedCell === null || ! isCellEditable ( selectedPosition ) ) {
674+ function handleCellPaste ( event : CellClipboardEvent ) {
675+ if ( ! onCellPaste || ! onRowsChange || ! isCellEditable ( selectedPosition ) ) {
700676 return ;
701677 }
702678
703679 const { idx, rowIdx } = selectedPosition ;
704- const targetColumn = columns [ idx ] ;
705- const targetRow = rows [ rowIdx ] ;
706-
707- const updatedTargetRow = onPaste ( {
708- sourceRow : copiedCell . row ,
709- sourceColumnKey : copiedCell . columnKey ,
710- targetRow,
711- targetColumnKey : targetColumn . key
712- } ) ;
713-
714- updateRow ( targetColumn , rowIdx , updatedTargetRow ) ;
680+ const column = columns [ idx ] ;
681+ const updatedRow = onCellPaste ( { row : rows [ rowIdx ] , column } , event ) ;
682+ updateRow ( column , rowIdx , updatedRow ) ;
715683 }
716684
717685 function handleCellInput ( event : KeyboardEvent < HTMLDivElement > ) {
@@ -729,7 +697,7 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
729697 return ;
730698 }
731699
732- if ( isCellEditable ( selectedPosition ) && isDefaultCellInput ( event ) ) {
700+ if ( isCellEditable ( selectedPosition ) && isDefaultCellInput ( event , onCellPaste != null ) ) {
733701 setSelectedPosition ( ( { idx, rowIdx } ) => ( {
734702 idx,
735703 rowIdx,
@@ -1054,11 +1022,6 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
10541022 onCellContextMenu : onCellContextMenuLatest ,
10551023 rowClass,
10561024 gridRowStart,
1057- copiedCellIdx :
1058- copiedCell !== null && copiedCell . row === row
1059- ? columns . findIndex ( ( c ) => c . key === copiedCell . columnKey )
1060- : undefined ,
1061-
10621025 selectedCellIdx : selectedRowIdx === rowIdx ? selectedIdx : undefined ,
10631026 draggedOverCellIdx : getDraggedOverCellIdx ( rowIdx ) ,
10641027 setDraggedOverRowIdx : isDragging ? setDraggedOverRowIdx : undefined ,
@@ -1138,6 +1101,8 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
11381101 ref = { gridRef }
11391102 onScroll = { handleScroll }
11401103 onKeyDown = { handleKeyDown }
1104+ onCopy = { handleCellCopy }
1105+ onPaste = { handleCellPaste }
11411106 data-testid = { testId }
11421107 data-cy = { dataCy }
11431108 >
0 commit comments