diff --git a/packages/core/src/internal/click-outside-container/click-outside-container.tsx b/packages/core/src/internal/click-outside-container/click-outside-container.tsx index 6ce195be0..e06edbb4b 100644 --- a/packages/core/src/internal/click-outside-container/click-outside-container.tsx +++ b/packages/core/src/internal/click-outside-container/click-outside-container.tsx @@ -8,14 +8,12 @@ export default class ClickOutsideContainer extends React.PureComponent { private wrapperRef = React.createRef(); public componentDidMount() { - document.addEventListener("touchend", this.clickOutside, true); - document.addEventListener("mousedown", this.clickOutside, true); + document.addEventListener("pointerdown", this.clickOutside, true); document.addEventListener("contextmenu", this.clickOutside, true); } public componentWillUnmount() { - document.removeEventListener("touchend", this.clickOutside, true); - document.removeEventListener("mousedown", this.clickOutside, true); + document.removeEventListener("pointerdown", this.clickOutside, true); document.removeEventListener("contextmenu", this.clickOutside, true); } diff --git a/packages/core/src/internal/data-grid/data-grid.tsx b/packages/core/src/internal/data-grid/data-grid.tsx index 52338a786..5f68c0ff7 100644 --- a/packages/core/src/internal/data-grid/data-grid.tsx +++ b/packages/core/src/internal/data-grid/data-grid.tsx @@ -501,7 +501,7 @@ const DataGrid: React.ForwardRefRenderFunction = (p, ); const getMouseArgsForPosition = React.useCallback( - (canvas: HTMLCanvasElement, posX: number, posY: number, ev?: MouseEvent | TouchEvent): GridMouseEventArgs => { + (canvas: HTMLCanvasElement, posX: number, posY: number, ev?: MouseEvent | TouchEvent | PointerEvent): GridMouseEventArgs => { const rect = canvas.getBoundingClientRect(); const scale = rect.width / width; const x = (posX - rect.left) / scale; @@ -512,7 +512,11 @@ const DataGrid: React.ForwardRefRenderFunction = (p, let button = 0; let buttons = 0; - if (ev instanceof MouseEvent) { + + const isMouse = ev instanceof MouseEvent || (ev instanceof PointerEvent && ev.pointerType === 'mouse') + const isTouch = ev instanceof TouchEvent || (ev instanceof PointerEvent && ev.pointerType === 'touch') + + if (isMouse) { button = ev.button; buttons = ev.buttons; } @@ -538,7 +542,6 @@ const DataGrid: React.ForwardRefRenderFunction = (p, const shiftKey = ev?.shiftKey === true; const ctrlKey = ev?.ctrlKey === true; const metaKey = ev?.metaKey === true; - const isTouch = (ev !== undefined && !(ev instanceof MouseEvent)) || (ev as any)?.pointerType === "touch"; const scrollEdge: GridMouseEventArgs["scrollEdge"] = [ x < 0 ? -1 : width < x ? 1 : 0, @@ -1030,22 +1033,16 @@ const DataGrid: React.ForwardRefRenderFunction = (p, const downTime = React.useRef(0); const downPosition = React.useRef(); const mouseDown = React.useRef(false); - const onMouseDownImpl = React.useCallback( - (ev: MouseEvent | TouchEvent) => { + const onPointerDown = React.useCallback( + (ev: PointerEvent) => { const canvas = ref.current; const eventTarget = eventTargetRef?.current; if (canvas === null || (ev.target !== canvas && ev.target !== eventTarget)) return; mouseDown.current = true; - let clientX: number; - let clientY: number; - if (ev instanceof MouseEvent) { - clientX = ev.clientX; - clientY = ev.clientY; - } else { - clientX = ev.touches[0].clientX; - clientY = ev.touches[0].clientY; - } + const clientX = ev.clientX; + const clientY = ev.clientY; + if (ev.target === eventTarget && eventTarget !== null) { const bounds = eventTarget.getBoundingClientRect(); if (clientX > bounds.right || clientY > bounds.bottom) return; @@ -1094,13 +1091,12 @@ const DataGrid: React.ForwardRefRenderFunction = (p, onMouseDown, ] ); - useEventListener("touchstart", onMouseDownImpl, windowEventTarget, false); - useEventListener("mousedown", onMouseDownImpl, windowEventTarget, false); + useEventListener("pointerdown", onPointerDown, windowEventTarget, false); const lastUpTime = React.useRef(0); - const onMouseUpImpl = React.useCallback( - (ev: MouseEvent | TouchEvent) => { + const onPointerUp = React.useCallback( + (ev: PointerEvent) => { const lastUpTimeValue = lastUpTime.current; lastUpTime.current = Date.now(); const canvas = ref.current; @@ -1109,21 +1105,9 @@ const DataGrid: React.ForwardRefRenderFunction = (p, const eventTarget = eventTargetRef?.current; const isOutside = ev.target !== canvas && ev.target !== eventTarget; - - let clientX: number; - let clientY: number; - let canCancel = true; - if (ev instanceof MouseEvent) { - clientX = ev.clientX; - clientY = ev.clientY; - canCancel = ev.button < 3; - if ((ev as any).pointerType === "touch") { - return; - } - } else { - clientX = ev.changedTouches[0].clientX; - clientY = ev.changedTouches[0].clientY; - } + const clientX = ev.clientX; + const clientY = ev.clientY; + const canCancel = ev.pointerType === 'mouse' ? ev.button < 3 : true; let args = getMouseArgsForPosition(canvas, clientX, clientY, ev); @@ -1171,8 +1155,7 @@ const DataGrid: React.ForwardRefRenderFunction = (p, }, [onMouseUp, eventTargetRef, getMouseArgsForPosition, isOverHeaderElement, groupHeaderActionForEvent] ); - useEventListener("mouseup", onMouseUpImpl, windowEventTarget, false); - useEventListener("touchend", onMouseUpImpl, windowEventTarget, false); + useEventListener("pointerup", onPointerUp, windowEventTarget, false); const onClickImpl = React.useCallback( (ev: MouseEvent | TouchEvent) => { @@ -1277,7 +1260,7 @@ const DataGrid: React.ForwardRefRenderFunction = (p, }, [getCellContent, getCellRenderer, hoveredItem]); const hoveredRef = React.useRef(); - const onMouseMoveImpl = React.useCallback( + const onPointerMove = React.useCallback( (ev: MouseEvent) => { const canvas = ref.current; if (canvas === null) return; @@ -1360,7 +1343,7 @@ const DataGrid: React.ForwardRefRenderFunction = (p, damageInternal, ] ); - useEventListener("mousemove", onMouseMoveImpl, windowEventTarget, true); + useEventListener("pointermove", onPointerMove, windowEventTarget, true); const onKeyDownImpl = React.useCallback( (event: React.KeyboardEvent) => { diff --git a/packages/core/test/data-editor-input.test.tsx b/packages/core/test/data-editor-input.test.tsx index fde89fb50..2fc20c3c9 100644 --- a/packages/core/test/data-editor-input.test.tsx +++ b/packages/core/test/data-editor-input.test.tsx @@ -12,6 +12,7 @@ import { import type { DataEditorRef } from "../src/data-editor/data-editor.js"; import { CompactSelection } from "../src/internal/data-grid/data-grid-types.js"; import { vi, expect, describe, test, beforeEach, afterEach } from "vitest"; +import { standardBeforeEach } from "./test-utils.js"; const makeCell = (cell: Item): GridCell => { const [col, row] = cell; @@ -223,6 +224,8 @@ describe("data-editor-input", () => { }); beforeEach(() => { + standardBeforeEach(); + Element.prototype.scrollTo = vi.fn() as any; Element.prototype.scrollBy = vi.fn() as any; Object.assign(navigator, { @@ -267,17 +270,17 @@ a new line char ""more quotes"" plus a tab ." https://google.com`) prep(); const canvas = screen.getByTestId("data-grid-canvas"); - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { clientX: 350, clientY: 36 + 32 * 2 + 16, }); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 650, clientY: 36 + 32 * 12 + 16, }); - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { clientX: 650, clientY: 36 + 32 * 12 + 16, }); @@ -316,19 +319,19 @@ a new line char ""more quotes"" plus a tab ." https://google.com`) prep(); const canvas = screen.getByTestId("data-grid-canvas"); - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { ctrlKey: true, clientX: 350, clientY: 36 + 32 * 2 + 16, }); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { ctrlKey: true, clientX: 650, clientY: 36 + 32 * 12 + 16, }); - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { ctrlKey: true, clientX: 650, clientY: 36 + 32 * 12 + 16, @@ -372,19 +375,19 @@ a new line char ""more quotes"" plus a tab ." https://google.com`) prep(); const canvas = screen.getByTestId("data-grid-canvas"); - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { ctrlKey: true, clientX: 20, clientY: 36 + 32 * 3 + 16, }); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { ctrlKey: true, clientX: 20, clientY: 36 + 32 * 3 + 16, }); - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { ctrlKey: true, clientX: 20, clientY: 36 + 32 * 3 + 16, @@ -429,19 +432,19 @@ a new line char ""more quotes"" plus a tab ." https://google.com`) prep(); const canvas = screen.getByTestId("data-grid-canvas"); - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { ctrlKey: true, clientX: 20, clientY: 36 + 32 * 3 + 16, }); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { ctrlKey: true, clientX: 20, clientY: 36 + 32 * 3 + 16, }); - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { ctrlKey: true, clientX: 20, clientY: 36 + 32 * 3 + 16, @@ -485,19 +488,19 @@ a new line char ""more quotes"" plus a tab ." https://google.com`) prep(); const canvas = screen.getByTestId("data-grid-canvas"); - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { ctrlKey: true, clientX: 220, clientY: 16, }); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { ctrlKey: true, clientX: 220, clientY: 16, }); - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { ctrlKey: true, clientX: 220, clientY: 16, @@ -542,19 +545,19 @@ a new line char ""more quotes"" plus a tab ." https://google.com`) prep(); const canvas = screen.getByTestId("data-grid-canvas"); - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { ctrlKey: true, clientX: 220, clientY: 16, }); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { ctrlKey: true, clientX: 220, clientY: 16, }); - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { ctrlKey: true, clientX: 220, clientY: 16, diff --git a/packages/core/test/data-editor-resize.test.tsx b/packages/core/test/data-editor-resize.test.tsx index 650e31ed7..bfa71e157 100644 --- a/packages/core/test/data-editor-resize.test.tsx +++ b/packages/core/test/data-editor-resize.test.tsx @@ -30,18 +30,18 @@ describe("data-editor", () => { prep(); const canvas = screen.getByTestId("data-grid-canvas"); - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { clientX: 310, // Col B Right Edge clientY: 16, // Header }); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 350, clientY: 16, buttons: 1, }); - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { clientX: 350, clientY: 16, }); @@ -63,22 +63,22 @@ describe("data-editor", () => { prep(); const canvas = screen.getByTestId("data-grid-canvas"); - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { clientX: 310, // Col B Right Edge clientY: 16, // Header }); - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { clientX: 310, clientY: 16, }); - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { clientX: 310, // Col B Right Edge clientY: 16, // Header }); - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { clientX: 310, clientY: 16, }); @@ -114,18 +114,18 @@ describe("data-editor", () => { prep(); const canvas = screen.getByTestId("data-grid-canvas"); - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { clientX: 310, // Col B Right Edge clientY: 16, // Header }); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 350, clientY: 16, buttons: 1, }); - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { clientX: 350, clientY: 16, }); @@ -159,18 +159,18 @@ describe("data-editor", () => { prep(); const canvas = screen.getByTestId("data-grid-canvas"); - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { clientX: 310, // Col B Right Edge clientY: 16, // Header }); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 350, clientY: 16, buttons: 1, }); - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { clientX: 350, clientY: 16, }); @@ -204,18 +204,18 @@ describe("data-editor", () => { prep(); const canvas = screen.getByTestId("data-grid-canvas"); - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { clientX: 310, // Col B Right Edge clientY: 16, // Header }); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 350, clientY: 16, buttons: 1, }); - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { clientX: 350, clientY: 16, }); @@ -245,18 +245,18 @@ describe("data-editor", () => { prep(); const canvas = screen.getByTestId("data-grid-canvas"); - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { clientX: 314, // Col B Right Edge clientY: 16, // Header }); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 350, clientY: 16, buttons: 1, }); - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { clientX: 350, clientY: 16, }); diff --git a/packages/core/test/data-editor.test.tsx b/packages/core/test/data-editor.test.tsx index 097c8e967..c06c4ae7b 100644 --- a/packages/core/test/data-editor.test.tsx +++ b/packages/core/test/data-editor.test.tsx @@ -709,12 +709,12 @@ describe("data-editor", () => { prep(); const canvas = screen.getByTestId("data-grid-canvas"); - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { clientX: 300, // Col B, ends at x = 310 clientY: 36 + 32 + 16, // Row 1 (0 indexed) }); - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { clientX: 320, // Col C, started at x = 310 clientY: 36 + 32 + 16, // Row 1 (0 indexed) }); @@ -732,12 +732,12 @@ describe("data-editor", () => { prep(); const canvas = screen.getByTestId("data-grid-canvas"); - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { clientX: 300, // Col B, ends at x = 310 clientY: 16, // Header }); - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { clientX: 320, // Col C, started at x = 310 clientY: 16, // Header }); @@ -974,7 +974,7 @@ describe("data-editor", () => { prep(); const canvas = screen.getByTestId("data-grid-canvas"); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 300, // Col B clientY: 16, // Group Header }); @@ -1020,7 +1020,7 @@ describe("data-editor", () => { prep(); const canvas = screen.getByTestId("data-grid-canvas"); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 300, // Col B clientY: 16, // Header }); @@ -1077,7 +1077,7 @@ describe("data-editor", () => { prep(); const canvas = screen.getByTestId("data-grid-canvas"); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 300, // Col B clientY: 36 + 32 + 16, // Row 1 (0 indexed) }); @@ -1096,7 +1096,7 @@ describe("data-editor", () => { prep(); const canvas = screen.getByTestId("data-grid-canvas"); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 300, // Col B clientY: 36 + 32 + 16, // Row 1 (0 indexed) }); @@ -2484,20 +2484,20 @@ describe("data-editor", () => { vi.spyOn(document, "activeElement", "get").mockImplementation(() => canvas); // mouse down col b row 2 - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { clientX: 300, clientY: 36 + 32 * 2 + 16, }); // mouse move col b row 3 - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 300, clientY: 36 + 32 * 2 + 16, buttons: 1, }); // mouse up - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { clientX: 300, clientY: 36 + 32 * 2 + 16, }); @@ -2547,20 +2547,20 @@ describe("data-editor", () => { vi.spyOn(document, "activeElement", "get").mockImplementation(() => canvas); // mouse down col b row 2 - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { clientX: 300, clientY: 36 + 32 * 2 + 16, }); // mouse move col b row 3 - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 300, clientY: 36 + 32 * 3 + 16, buttons: 1, }); // mouse up - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { clientX: 300, clientY: 36 + 32 * 3 + 16, }); @@ -2743,7 +2743,7 @@ describe("data-editor", () => { spy.mockClear(); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 300, // Col B clientY: 16, // Header }); @@ -2759,7 +2759,7 @@ describe("data-editor", () => { const scroller = prep(); const canvas = screen.getByTestId("data-grid-canvas"); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 300, // Col B clientY: 16, // Header }); @@ -2817,7 +2817,7 @@ describe("data-editor", () => { const scroller = prep(); const canvas = screen.getByTestId("data-grid-canvas"); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 300, // Col B clientY: 16, // Header }); @@ -2945,21 +2945,21 @@ describe("data-editor", () => { prep(); const canvas = screen.getByTestId("data-grid-canvas"); - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { clientX: 10, // Row marker clientY: 36 + 32 * 2 + 16, // Row 2 (0 indexed) }); spy.mockClear(); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { shiftKey: true, clientX: 10, // Row marker clientY: 36 + 32 * 5 + 16, // Row 2 (0 indexed) buttons: 1, }); - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { shiftKey: true, clientX: 10, // Row marker clientY: 36 + 32 * 5 + 16, // Row 2 (0 indexed) @@ -3341,36 +3341,36 @@ describe("data-editor", () => { prep(); const canvas = screen.getByTestId("data-grid-canvas"); - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { clientX: 300, // Col B clientY: 16, // Header }); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 250, clientY: 16, buttons: 1, }); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 200, clientY: 16, buttons: 1, }); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 150, clientY: 16, buttons: 1, }); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 100, clientY: 16, buttons: 1, }); - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { clientX: 100, // Col A clientY: 16, // Header }); @@ -3392,18 +3392,18 @@ describe("data-editor", () => { prep(); const canvas = screen.getByTestId("data-grid-canvas"); - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { clientX: 10, // Col B Right Edge clientY: 300, // Header }); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 10, clientY: 400, buttons: 1, }); - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { clientX: 10, clientY: 400, }); @@ -3425,13 +3425,13 @@ describe("data-editor", () => { prep(); const canvas = screen.getByTestId("data-grid-canvas"); - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { clientX: 300, // Col B clientY: 36 + 32 * 2 + 16, // Row 2 }); spy.mockClear(); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 600, // Col B clientY: 36 + 32 * 12 + 16, // Row 2 buttons: 1, @@ -3443,7 +3443,7 @@ describe("data-editor", () => { }) ); - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { clientX: 600, // Col B clientY: 36 + 32 * 12 + 16, // Row 2 }); @@ -3464,14 +3464,14 @@ describe("data-editor", () => { clientY: 36 + 32 * 2 + 16, // Row 2 }); - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { button: 1, clientX: 300, // Col B clientY: 36 + 32 * 2 + 16, // Row 2 }); spy.mockClear(); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 600, // Col B clientY: 36 + 32 * 12 + 16, // Row 2 buttons: 1, @@ -3479,7 +3479,7 @@ describe("data-editor", () => { expect(spy).not.toBeCalled(); - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { clientX: 600, // Col B clientY: 36 + 32 * 12 + 16, // Row 2 }); @@ -3979,13 +3979,13 @@ describe("data-editor", () => { prep(); const canvas = screen.getByTestId("data-grid-canvas"); - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { clientX: 300, // Col B clientY: 36 + 32 * 2 + 16, // Row 2 }); spy.mockClear(); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 600, // Col B clientY: 36 + 32 * 12 + 16, // Row 2 buttons: 1, @@ -3997,7 +3997,7 @@ describe("data-editor", () => { }) ); - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { clientX: 600, // Col B clientY: 36 + 32 * 12 + 16, // Row 2 }); @@ -4187,18 +4187,18 @@ describe("data-editor", () => { prep(); const canvas = screen.getByTestId("data-grid-canvas"); - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { clientX: 300, // Col B clientY: 36 + 32 * 2 + 16, // Row 2 }); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 600, // Col B clientY: 36 + 32 * 12 + 16, // Row 2 buttons: 1, }); - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { clientX: 600, // Col B clientY: 36 + 32 * 12 + 16, // Row 2 }); @@ -4238,25 +4238,25 @@ describe("data-editor", () => { prep(); const canvas = screen.getByTestId("data-grid-canvas"); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 300, // Col B clientY: 16 + 200, // Not Header }); await act(() => new Promise(r => window.setTimeout(r, 10))); - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { clientX: 300, // Col B clientY: 16 + 200, // Not Header }); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 300, // Col B clientY: 16, // Header buttons: 1, }); - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { clientX: 300, // Col B clientY: 16, // Header }); @@ -4275,12 +4275,12 @@ describe("data-editor", () => { prep(); const canvas = screen.getByTestId("data-grid-canvas"); - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { clientX: 300, // Col B clientY: 16, // Header }); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 300, // Col B clientY: 0, buttons: 1, @@ -4288,7 +4288,7 @@ describe("data-editor", () => { await act(() => new Promise(r => window.setTimeout(r, 100))); - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { clientX: 300, // Col B clientY: 0, }); @@ -4310,18 +4310,18 @@ describe("data-editor", () => { clientY: 36 + 30, // Row 2 }); - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { clientX: 308, // Col A clientY: 36 + 30, // Row 2 }); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 308, // Col A clientY: 36 + 32 * 2 + 16, // Row 2 buttons: 1, }); - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { clientX: 308, // Col A clientY: 36 + 32 * 2 + 16, // Row 2 }); @@ -4348,18 +4348,18 @@ describe("data-editor", () => { clientY: 36 + 30, // Row 2 }); - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { clientX: 308, // Col A clientY: 36 + 30, // Row 2 }); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 360, clientY: 36 + 32 * 5 + 16, // Row 5 buttons: 1, }); - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { clientX: 360, clientY: 36 + 32 * 5 + 16, // Row 5 }); @@ -4386,18 +4386,18 @@ describe("data-editor", () => { clientY: 36 + 30, // Row 2 }); - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { clientX: 308, // Col A clientY: 36 + 30, // Row 2 }); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 360, clientY: 36 + 32 * 5 + 16, // Row 5 buttons: 1, }); - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { clientX: 360, clientY: 36 + 32 * 5 + 16, // Row 5 }); @@ -4424,18 +4424,18 @@ describe("data-editor", () => { clientY: 36 + 30, }); - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { clientX: 308, clientY: 36 + 30, }); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 308, clientY: 36 + 32 * 5 + 16, buttons: 1, }); - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { clientX: 308, clientY: 36 + 32 * 5 + 16, }); @@ -4474,24 +4474,24 @@ describe("data-editor", () => { clientY: 36 + 30, }); - fireEvent.mouseDown(canvas, { + fireEvent.pointerDown(canvas, { clientX: 308, clientY: 36 + 30, }); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 308, clientY: 800, buttons: 1, }); - fireEvent.mouseMove(canvas, { + fireEvent.pointerMove(canvas, { clientX: 308, clientY: 995, buttons: 1, }); - fireEvent.mouseUp(canvas, { + fireEvent.pointerUp(canvas, { clientX: 308, clientY: 995, }); diff --git a/packages/core/test/data-grid.test.tsx b/packages/core/test/data-grid.test.tsx index 9f09a5e40..33f3c86c2 100644 --- a/packages/core/test/data-grid.test.tsx +++ b/packages/core/test/data-grid.test.tsx @@ -7,6 +7,7 @@ import { AllCellRenderers } from "../src/cells/index.js"; import { vi, expect, describe, test, beforeEach, afterEach } from "vitest"; import ImageWindowLoaderImpl from "../src/common/image-window-loader.js"; import { mergeAndRealizeTheme } from "../src/common/styles.js"; +import { standardBeforeEach } from "./test-utils.js"; const basicProps: DataGridProps = { cellXOffset: 0, @@ -114,6 +115,8 @@ const basicProps: DataGridProps = { const dataGridCanvasId = "data-grid-canvas"; describe("data-grid", () => { beforeEach(() => { + standardBeforeEach(); + Element.prototype.getBoundingClientRect = () => ({ bottom: 1000, height: 1000, @@ -136,12 +139,12 @@ describe("data-grid", () => { const spy = vi.fn(); render(); - fireEvent.mouseDown(screen.getByTestId(dataGridCanvasId), { + fireEvent.pointerDown(screen.getByTestId(dataGridCanvasId), { clientX: 300, // Col B clientY: 36 + 32 + 16, // Row 1 (0 indexed) }); - fireEvent.mouseUp(screen.getByTestId(dataGridCanvasId), { + fireEvent.pointerUp(screen.getByTestId(dataGridCanvasId), { clientX: 300, // Col B clientY: 36 + 32 + 16, // Row 1 (0 indexed) }); @@ -164,7 +167,7 @@ describe("data-grid", () => { const spy = vi.fn(); render(); - fireEvent.mouseDown(screen.getByTestId(dataGridCanvasId), { + fireEvent.pointerDown(screen.getByTestId(dataGridCanvasId), { clientX: 990, // Col B clientY: 36 + 32 + 16, // Row 1 (0 indexed) }); @@ -181,12 +184,12 @@ describe("data-grid", () => { const spy = vi.fn(); render(); - fireEvent.mouseDown(screen.getByTestId(dataGridCanvasId), { + fireEvent.pointerDown(screen.getByTestId(dataGridCanvasId), { clientX: 300, // Col B clientY: 36 + 32 * 5 + 16, // Row 5 (0 indexed) }); - fireEvent.mouseUp(screen.getByTestId(dataGridCanvasId), { + fireEvent.pointerUp(screen.getByTestId(dataGridCanvasId), { clientX: 300, // Col B clientY: 36 + 32 * 5 + 16, // Row 5 (0 indexed) }); @@ -221,12 +224,12 @@ describe("data-grid", () => { ); const el = screen.getByTestId(dataGridCanvasId); - fireEvent.mouseDown(el, { + fireEvent.pointerDown(el, { clientX: 140, clientY: 18, }); - fireEvent.mouseUp(el, { + fireEvent.pointerUp(el, { clientX: 140, clientY: 18, }); @@ -241,7 +244,7 @@ describe("data-grid", () => { render(); const el = screen.getByTestId(dataGridCanvasId); - fireEvent.mouseMove(el, { + fireEvent.pointerMove(el, { clientX: 350, // Col C clientY: 36 + 32 * 5 + 16, // Row 5 (0 indexed) }); @@ -272,7 +275,7 @@ describe("data-grid", () => { ); const outsideElement = screen.getByTestId("outside-element"); - fireEvent.mouseMove(outsideElement, { + fireEvent.pointerMove(outsideElement, { clientX: 350, // Col C clientY: 36 + 32 * 5 + 16, // Row 5 (0 indexed) }); @@ -286,7 +289,7 @@ describe("data-grid", () => { render(); const el = screen.getByTestId(dataGridCanvasId); - fireEvent.mouseMove(el, { + fireEvent.pointerMove(el, { clientX: 350, // Col C clientY: 16, // Header }); @@ -307,7 +310,7 @@ describe("data-grid", () => { ); const el = screen.getByTestId(dataGridCanvasId); - fireEvent.mouseMove(el, { + fireEvent.pointerMove(el, { clientX: 350, // Col C clientY: 46, // Header }); @@ -326,7 +329,7 @@ describe("data-grid", () => { render(); const el = screen.getByTestId(dataGridCanvasId); - fireEvent.mouseMove(el, { + fireEvent.pointerMove(el, { clientX: 350, // Col C clientY: 14, // Header }); @@ -367,12 +370,12 @@ describe("data-grid", () => { const spy = vi.fn(); render(); - fireEvent.mouseDown(screen.getByTestId(dataGridCanvasId), { + fireEvent.pointerDown(screen.getByTestId(dataGridCanvasId), { clientX: 50, // Col A clientY: 36 + 32 * 5 + 16, // Row 5 (0 indexed) }); - fireEvent.mouseUp(screen.getByTestId(dataGridCanvasId), { + fireEvent.pointerUp(screen.getByTestId(dataGridCanvasId), { clientX: 50, // Col A clientY: 36 + 32 * 5 + 16, // Row 5 (0 indexed) }); diff --git a/packages/core/test/test-utils.tsx b/packages/core/test/test-utils.tsx index 36568671a..f9b9a530e 100644 --- a/packages/core/test/test-utils.tsx +++ b/packages/core/test/test-utils.tsx @@ -19,12 +19,12 @@ function getMockBooleanData(row: number): boolean | null | undefined { } export function sendClick(el: Element | Node | Document | Window, options?: any, runTimers?: boolean): void { - fireEvent.mouseDown(el, options); + fireEvent.pointerDown(el, options); if (runTimers === true) act(() => { vi.runAllTimers(); }); - fireEvent.mouseUp(el, options); + fireEvent.pointerUp(el, options); if (runTimers === true) act(() => { vi.runAllTimers(); @@ -33,17 +33,15 @@ export function sendClick(el: Element | Node | Document | Window, options?: any, } export function sendTouchClick(el: Element | Node | Document | Window, options?: any): void { - fireEvent.touchStart(el, options); - fireEvent.touchEnd(el, { - ...options, - changedTouches: options.touches, - }); - fireEvent.click(el, { + const mouseOptions = { clientX: options?.touches?.[0]?.clientX, clientY: options?.touches?.[0]?.clientY, pointerType: "touch", ...options, - }); + } + fireEvent.pointerDown(el, mouseOptions); + fireEvent.pointerUp(el, mouseOptions); + fireEvent.click(el, mouseOptions); } export const makeCell = (cell: Item): GridCell => { @@ -292,6 +290,10 @@ export function standardBeforeEach() { // disconnect: vi.fn(), // })); + // JSDOM does not support PointerEvent + // https://github.com/jsdom/jsdom/issues/2527 + (window as any).PointerEvent = MouseEvent; + Element.prototype.scrollTo = vi.fn() as any; Element.prototype.scrollBy = vi.fn() as any; Object.assign(navigator, {