Skip to content

Commit

Permalink
Fix onDelete
Browse files Browse the repository at this point in the history
  • Loading branch information
jassmith committed Mar 20, 2022
1 parent a65e3c3 commit 1074d01
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
41 changes: 41 additions & 0 deletions packages/core/src/data-editor/data-editor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,47 @@ describe("data-editor", () => {
expect(spy).toHaveBeenCalled();
});

test("Delete range", async () => {
const spy = jest.fn();

jest.useFakeTimers();
render(
<DataEditor
{...basicProps}
onDelete={spy}
gridSelection={{
columns: CompactSelection.empty(),
rows: CompactSelection.empty(),
current: {
cell: [2, 2],
range: { x: 2, y: 2, width: 4, height: 10 },
rangeStack: [],
},
}}
rowMarkers="both"
/>,
{
wrapper: Context,
}
);
prep();

const canvas = screen.getByTestId("data-grid-canvas");
fireEvent.keyDown(canvas, {
key: "Delete",
});

expect(spy).toHaveBeenCalledWith({
columns: CompactSelection.empty(),
rows: CompactSelection.empty(),
current: {
cell: [2, 2],
range: { x: 2, y: 2, width: 4, height: 10 },
rangeStack: [],
},
});
});

test("Open and close overlay", async () => {
jest.useFakeTimers();
render(<DataEditor {...basicProps} />, {
Expand Down
12 changes: 11 additions & 1 deletion packages/core/src/data-editor/data-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ const DataEditorImpl: React.ForwardRefRenderFunction<DataEditorRef, DataEditorPr
rangeSelectionBlending = "exclusive",
columnSelectionBlending = "exclusive",
rowSelectionBlending = "exclusive",
onDelete,
onDelete: onDeleteIn,
onDragStart,
onMouseMove,
onPaste,
Expand Down Expand Up @@ -408,6 +408,16 @@ const DataEditorImpl: React.ForwardRefRenderFunction<DataEditorRef, DataEditorPr
[onGridSelectionChange, rowMarkerOffset, expandSelection]
);

const onDelete = React.useCallback<NonNullable<DataEditorProps["onDelete"]>>(
sel => {
if (onDeleteIn !== undefined) {
return onDeleteIn(shiftSelection(sel, -rowMarkerOffset));
}
return true;
},
[onDeleteIn, rowMarkerOffset]
);

const [setCurrent, setSelectedRows, setSelectedColumns] = useSelectionBehavior(
gridSelection,
setGridSelection,
Expand Down

0 comments on commit 1074d01

Please sign in to comment.