Skip to content

Commit 408a543

Browse files
committed
Merge remote-tracking branch 'origin/fix-context-menu-keyboard-support' into pre-release
2 parents 9684027 + 80d78d3 commit 408a543

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

packages/core/src/internal/data-grid/data-grid.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,25 @@ const DataGrid: React.ForwardRefRenderFunction<DataGridRef, DataGridProps> = (p,
12831283
);
12841284
useEventListener("contextmenu", onContextMenuImpl, eventTargetRef?.current ?? null, false);
12851285

1286+
const onContextMenuWithKeyboardImpl = React.useCallback(
1287+
(ev: MouseEvent) => {
1288+
const canvas = ref.current;
1289+
const currentSelectedCell = selectionRef.current.current?.cell
1290+
if (canvas === null || onContextMenu === undefined || currentSelectedCell === undefined)
1291+
return;
1292+
const bounds = getBoundsForItem(canvas, ...currentSelectedCell);
1293+
if (bounds) {
1294+
const args = getMouseArgsForPosition(canvas, bounds.x + bounds.width / 2, bounds.y + bounds.height / 2, ev);
1295+
onContextMenu(args, () => {
1296+
if (ev.cancelable) ev.preventDefault();
1297+
});
1298+
}
1299+
},
1300+
[getMouseArgsForPosition, onContextMenu, getBoundsForItem]
1301+
);
1302+
1303+
useEventListener("contextmenu", onContextMenuWithKeyboardImpl, ref?.current ?? null, false);
1304+
12861305
const onAnimationFrame = React.useCallback<StepCallback>(values => {
12871306
damageRegion.current = new CellSet(values.map(x => x.item));
12881307
hoverValues.current = values;

packages/core/test/data-editor.test.tsx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,53 @@ describe("data-editor", () => {
196196
expect(spySelection).not.toHaveBeenCalled();
197197
});
198198

199+
test("opens contextmenu at selected cell position when fired on canvas", async () => {
200+
const spy = vi.fn();
201+
202+
vi.useFakeTimers();
203+
render(<DataEditor
204+
{...basicProps}
205+
gridSelection={{
206+
columns: CompactSelection.empty(),
207+
rows: CompactSelection.empty(),
208+
current: {
209+
cell: [1, 1],
210+
range: { x: 1, y: 1, width: 1, height: 1 },
211+
rangeStack: [],
212+
},
213+
}}
214+
onCellContextMenu={spy}
215+
/>, { wrapper: Context });
216+
217+
prep();
218+
219+
const canvas = screen.getByTestId("data-grid-canvas");
220+
221+
fireEvent.contextMenu(canvas);
222+
223+
expect(spy).toHaveBeenCalledWith([1, 1], expect.anything());
224+
});
225+
226+
test("does not open contextmenu when no cell selected", async () => {
227+
const spy = vi.fn();
228+
229+
vi.useFakeTimers();
230+
render(<DataEditor
231+
{...basicProps}
232+
gridSelection={{
233+
columns: CompactSelection.empty(),
234+
rows: CompactSelection.empty(),
235+
}}
236+
onCellContextMenu={spy}
237+
/>, { wrapper: Context });
238+
239+
prep();
240+
const canvas = screen.getByTestId("data-grid-canvas");
241+
fireEvent.contextMenu(canvas);
242+
243+
expect(spy).not.toHaveBeenCalled();
244+
});
245+
199246
test("middle click does not change selection", async () => {
200247
const spySelection = vi.fn();
201248

0 commit comments

Comments
 (0)