Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include KeyboardEvent to onCopy and onPaste callbacks #3270

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -543,11 +543,11 @@ function DataGrid<R, SR, K extends Key>(
const cKey = 67;
const vKey = 86;
if (keyCode === cKey) {
handleCopy();
handleCopy(event);
return;
}
if (keyCode === vKey) {
handlePaste();
handlePaste(event);
return;
}
}
Expand Down Expand Up @@ -599,15 +599,15 @@ function DataGrid<R, SR, K extends Key>(
updateRow(columns[selectedPosition.idx], selectedPosition.rowIdx, selectedPosition.row);
}

function handleCopy() {
function handleCopy(event: KeyboardEvent<HTMLDivElement>) {
const { idx, rowIdx } = selectedPosition;
const sourceRow = rows[rowIdx];
const sourceColumnKey = columns[idx].key;
setCopiedCell({ row: sourceRow, columnKey: sourceColumnKey });
onCopy?.({ sourceRow, sourceColumnKey });
onCopy?.({ event, sourceRow, sourceColumnKey });
}

function handlePaste() {
function handlePaste(event: KeyboardEvent<HTMLDivElement>) {
if (!onPaste || !onRowsChange || copiedCell === null || !isCellEditable(selectedPosition)) {
return;
}
Expand All @@ -617,6 +617,7 @@ function DataGrid<R, SR, K extends Key>(
const targetRow = rows[rowIdx];

const updatedTargetRow = onPaste({
event,
sourceRow: copiedCell.row,
sourceColumnKey: copiedCell.columnKey,
targetRow,
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,13 @@ export interface FillEvent<TRow> {
}

export interface CopyEvent<TRow> {
event: React.KeyboardEvent<HTMLDivElement>;
sourceColumnKey: string;
sourceRow: TRow;
}

export interface PasteEvent<TRow> {
event: React.KeyboardEvent<HTMLDivElement>;
sourceColumnKey: string;
sourceRow: TRow;
targetColumnKey: string;
Expand Down