diff --git a/src/DataGrid.tsx b/src/DataGrid.tsx index da14f70786..25f9f2e0c8 100644 --- a/src/DataGrid.tsx +++ b/src/DataGrid.tsx @@ -34,6 +34,7 @@ import type { CalculatedColumn, CellClickArgs, CellClipboardEvent, + CellCopyPasteEvent, CellKeyboardEvent, CellKeyDownArgs, CellMouseEvent, @@ -41,7 +42,6 @@ import type { CellSelectArgs, Column, ColumnOrColumnGroup, - CopyPasteEvent, Direction, FillEvent, Maybe, @@ -161,12 +161,6 @@ export interface DataGridProps extends Sha onSortColumnsChange?: Maybe<(sortColumns: SortColumn[]) => void>; defaultColumnOptions?: Maybe, NoInfer>>; onFill?: Maybe<(event: FillEvent>) => NoInfer>; - onCopy?: Maybe< - (args: CopyPasteEvent, NoInfer>, event: CellClipboardEvent) => void - >; - onPaste?: Maybe< - (args: CopyPasteEvent, NoInfer>, event: CellClipboardEvent) => NoInfer - >; /** * Event props @@ -186,6 +180,12 @@ export interface DataGridProps extends Sha onCellKeyDown?: Maybe< (args: CellKeyDownArgs, NoInfer>, event: CellKeyboardEvent) => void >; + onCellCopy?: Maybe< + (args: CellCopyPasteEvent, NoInfer>, event: CellClipboardEvent) => void + >; + onCellPaste?: Maybe< + (args: CellCopyPasteEvent, NoInfer>, event: CellClipboardEvent) => NoInfer + >; /** Function called whenever cell selection is changed */ onSelectedCellChange?: Maybe<(args: CellSelectArgs, NoInfer>) => void>; /** Called when the grid is scrolled */ @@ -251,8 +251,8 @@ function DataGrid( onColumnResize, onColumnsReorder, onFill, - onCopy, - onPaste, + onCellCopy, + onCellPaste, // Toggles and modes enableVirtualization: rawEnableVirtualization, // Miscellaneous @@ -647,20 +647,20 @@ function DataGrid( updateRow(columns[selectedPosition.idx], selectedPosition.rowIdx, selectedPosition.row); } - function handleCopy(event: CellClipboardEvent) { + function handleCellCopy(event: CellClipboardEvent) { if (!selectedCellIsWithinViewportBounds) return; const { idx, rowIdx } = selectedPosition; - onCopy?.({ row: rows[rowIdx], column: columns[idx] }, event); + onCellCopy?.({ row: rows[rowIdx], column: columns[idx] }, event); } - function handlePaste(event: CellClipboardEvent) { - if (!onPaste || !onRowsChange || !isCellEditable(selectedPosition)) { + function handleCellPaste(event: CellClipboardEvent) { + if (!onCellPaste || !onRowsChange || !isCellEditable(selectedPosition)) { return; } const { idx, rowIdx } = selectedPosition; const column = columns[idx]; - const updatedRow = onPaste({ row: rows[rowIdx], column }, event); + const updatedRow = onCellPaste({ row: rows[rowIdx], column }, event); updateRow(column, rowIdx, updatedRow); } @@ -679,7 +679,7 @@ function DataGrid( return; } - if (isCellEditable(selectedPosition) && isDefaultCellInput(event, onPaste != null)) { + if (isCellEditable(selectedPosition) && isDefaultCellInput(event, onCellPaste != null)) { setSelectedPosition(({ idx, rowIdx }) => ({ idx, rowIdx, @@ -1083,8 +1083,8 @@ function DataGrid( ref={gridRef} onScroll={handleScroll} onKeyDown={handleKeyDown} - onCopy={handleCopy} - onPaste={handlePaste} + onCopy={handleCellCopy} + onPaste={handleCellPaste} data-testid={testId} > diff --git a/src/index.ts b/src/index.ts index a3845ef26e..be00221294 100644 --- a/src/index.ts +++ b/src/index.ts @@ -29,7 +29,7 @@ export type { SelectHeaderRowEvent, SelectRowEvent, FillEvent, - CopyPasteEvent, + CellCopyPasteEvent, SortDirection, SortColumn, ColSpanArgs, diff --git a/src/types.ts b/src/types.ts index b588593692..b1130e876d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -249,7 +249,7 @@ export interface FillEvent { targetRow: TRow; } -export interface CopyPasteEvent { +export interface CellCopyPasteEvent { row: TRow; column: CalculatedColumn; } diff --git a/test/browser/TreeDataGrid.test.tsx b/test/browser/TreeDataGrid.test.tsx index c5073b4617..8744e564c5 100644 --- a/test/browser/TreeDataGrid.test.tsx +++ b/test/browser/TreeDataGrid.test.tsx @@ -99,7 +99,7 @@ function TestGrid({ groupBy }: { groupBy: string[] }) { (): ReadonlySet => new Set([]) ); - function onPaste(event: PasteEvent) { + function onCellPaste(event: PasteEvent) { return { ...event.targetRow, [event.targetColumnKey]: event.sourceRow[event.sourceColumnKey as keyof Row] @@ -120,7 +120,7 @@ function TestGrid({ groupBy }: { groupBy: string[] }) { expandedGroupIds={expandedGroupIds} onExpandedGroupIdsChange={setExpandedGroupIds} onRowsChange={setRows} - onPaste={onPaste} + onCellPaste={onCellPaste} /> ); } diff --git a/test/browser/copyPaste.test.tsx b/test/browser/copyPaste.test.tsx index f78c389f67..520533acfb 100644 --- a/test/browser/copyPaste.test.tsx +++ b/test/browser/copyPaste.test.tsx @@ -70,8 +70,8 @@ function CopyPasteTest({ rows={rows} bottomSummaryRows={bottomSummaryRows} onRowsChange={setRows} - onPaste={onPasteCallback ? onPaste : undefined} - onCopy={onCopyCallback ? onCopySpy : undefined} + onCellPaste={onPasteCallback ? onPaste : undefined} + onCellCopy={onCopyCallback ? onCopySpy : undefined} /> ); } diff --git a/website/routes/AllFeatures.lazy.tsx b/website/routes/AllFeatures.lazy.tsx index db65f9298f..5903819ef9 100644 --- a/website/routes/AllFeatures.lazy.tsx +++ b/website/routes/AllFeatures.lazy.tsx @@ -4,7 +4,7 @@ import { css } from '@linaria/core'; import clsx from 'clsx'; import DataGrid, { SelectColumn, textEditor } from '../../src'; -import type { CalculatedColumn, Column, CopyPasteEvent, FillEvent } from '../../src'; +import type { CalculatedColumn, CellCopyPasteEvent, Column, FillEvent } from '../../src'; import { textEditorClassname } from '../../src/editors/textEditor'; import { useDirection } from '../directionContext'; @@ -182,7 +182,7 @@ function AllFeatures() { return { ...targetRow, [columnKey]: sourceRow[columnKey as keyof Row] }; } - function handlePaste({ row, column }: CopyPasteEvent): Row { + function handleCellPaste({ row, column }: CellCopyPasteEvent): Row { if (!copiedCell) { return row; } @@ -205,8 +205,8 @@ function AllFeatures() { return { ...row, [targetColumnKey]: sourceRow[sourceColumnKey as keyof Row] }; } - function handleCopy( - { row, column }: CopyPasteEvent, + function handleCellCopy( + { row, column }: CellCopyPasteEvent, event: React.ClipboardEvent ): void { // copy highlighted text only @@ -241,8 +241,8 @@ function AllFeatures() { rowKeyGetter={rowKeyGetter} onRowsChange={setRows} onFill={handleFill} - onCopy={handleCopy} - onPaste={handlePaste} + onCellCopy={handleCellCopy} + onCellPaste={handleCellPaste} rowHeight={30} selectedRows={selectedRows} isRowSelectionDisabled={(row) => row.id === 'id_2'}