From 7d706c01138241e2bab9f79dd31e2f5272c4e906 Mon Sep 17 00:00:00 2001 From: ada-kuhn Date: Sat, 6 Aug 2022 20:00:38 +0300 Subject: [PATCH 1/3] add external copy and paste handlers --- src/DataGrid.tsx | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/DataGrid.tsx b/src/DataGrid.tsx index db55f42474..1e9aa4325b 100644 --- a/src/DataGrid.tsx +++ b/src/DataGrid.tsx @@ -149,6 +149,8 @@ export interface DataGridProps extends Sha onFill?: Maybe<(event: FillEvent) => R>; onCopy?: Maybe<(event: CopyEvent) => void>; onPaste?: Maybe<(event: PasteEvent) => R>; + handleCopyExternally?: Maybe<() => void>; + handlePasteExternally?: Maybe<() => void>; /** * Event props @@ -216,6 +218,8 @@ function DataGrid( onFill, onCopy, onPaste, + handleCopyExternally, + handlePasteExternally, // Toggles and modes cellNavigationMode: rawCellNavigationMode, enableVirtualization, @@ -547,10 +551,12 @@ function DataGrid( const { key, keyCode } = event; const { rowIdx } = selectedPosition; + const hasCopyHandler = handleCopyExternally ?? onCopy; + const hasPasteHandler = handlePasteExternally ?? onPaste; if ( selectedCellIsWithinViewportBounds && - (onPaste != null || onCopy != null) && + (hasPasteHandler != null || hasCopyHandler != null) && isCtrlKeyHeldDown(event) && !isGroupRow(rows[rowIdx]) && selectedPosition.mode === 'SELECT' @@ -560,11 +566,13 @@ function DataGrid( const cKey = 67; const vKey = 86; if (keyCode === cKey) { - handleCopy(); + const copyHandler = handleCopyExternally ?? handleCopy + copyHandler(); return; } if (keyCode === vKey) { - handlePaste(); + const pastehandler = handlePasteExternally ?? handlePaste + pastehandler(); return; } } From 7193e144a82b7cf1b844fe4dac4db128965b0e6f Mon Sep 17 00:00:00 2001 From: ada-kuhn Date: Sat, 6 Aug 2022 20:01:28 +0300 Subject: [PATCH 2/3] add tests for external copy and paste handlers --- test/copyPaste.test.tsx | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/test/copyPaste.test.tsx b/test/copyPaste.test.tsx index c40caae53e..23a43c213e 100644 --- a/test/copyPaste.test.tsx +++ b/test/copyPaste.test.tsx @@ -45,13 +45,19 @@ const summaryRows: readonly Row[] = [ const copyCellClassName = 'rdg-cell-copied'; const onPasteSpy = jest.fn(); const onCopySpy = jest.fn(); +const externalPasteSpy = jest.fn(); +const externalCopySpy = jest.fn(); function CopyPasteTest({ onPasteCallback = true, - onCopyCallback = false + onCopyCallback = false, + externalCopy = false, + externalPaste = false, }: { onPasteCallback?: boolean; onCopyCallback?: boolean; + externalCopy?: boolean; + externalPaste?: boolean; }) { const [rows, setRows] = useState(initialRows); @@ -68,16 +74,21 @@ function CopyPasteTest({ onRowsChange={setRows} onPaste={onPasteCallback ? onPaste : undefined} onCopy={onCopyCallback ? onCopySpy : undefined} + handlePasteExternally={externalPaste ? externalPasteSpy : undefined} + handleCopyExternally={externalCopy ? externalCopySpy : undefined} /> ); } -function setup(onPasteCallback = true, onCopyCallback = false) { +function setup(onPasteCallback = true, onCopyCallback = false, externalCopy = false, externalPaste = false) { onPasteSpy.mockReset(); onCopySpy.mockReset(); + externalPasteSpy.mockReset(); + externalCopySpy.mockReset(); + render( - + ); } @@ -199,3 +210,19 @@ test('should not allow paste on header or summary cells', async () => { expect(getSelectedCell()).toHaveTextContent('s1'); expect(onPasteSpy).not.toHaveBeenCalled(); }); + +test('when provided external copy, it is used over the internal copy method', async () => { + setup(true, true, true); + await userEvent.click(getCellsAtRowIndex(0)[0]); + copySelectedCell(); + expect(externalCopySpy).toHaveBeenCalled(); + expect(onCopySpy).not.toHaveBeenCalled(); +}) + +test('when provided external paste, it is used over the internal paste method', async () => { + setup(true, true, true, true); + await userEvent.click(getCellsAtRowIndex(0)[0]); + pasteSelectedCell(); + expect(externalPasteSpy).toHaveBeenCalled(); + expect(onPasteSpy).not.toHaveBeenCalled(); +}) From b0c1cb3e9c84dda46413d9eb0a4b101cb74f0957 Mon Sep 17 00:00:00 2001 From: ada-kuhn Date: Sat, 6 Aug 2022 20:04:07 +0300 Subject: [PATCH 3/3] add external copy and paste handlers to readme --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 701eebe372..10c0c76cb5 100644 --- a/README.md +++ b/README.md @@ -185,6 +185,14 @@ A number defining the height of summary rows. ###### `onPaste?: Maybe<(event: PasteEvent) => R>` +###### `handleCopyExternally?: Maybe<() => void>` + +Handle the copy event outside of react data grid + +###### `handlePasteExternally?: Maybe<() => void>` + +Handle the paste event outside of react data grid + ###### `onRowClick?: Maybe<(row: R, column: CalculatedColumn) => void>` ###### `onRowDoubleClick?: Maybe<(row: R, column: CalculatedColumn) => void>`