From 8c5821c43ad5c80319402498466402aa7a2f3d9a Mon Sep 17 00:00:00 2001 From: Brian Hung Date: Mon, 10 Jun 2024 12:16:45 -0700 Subject: [PATCH] getMouseArgsForPosition --- packages/core/API.md | 19 ++++++++++--------- packages/core/src/data-editor/data-editor.tsx | 15 +++++++++++++++ .../core/src/internal/data-grid/data-grid.tsx | 7 +++++++ 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/packages/core/API.md b/packages/core/API.md index 44fb12bc4..cdcd58bea 100644 --- a/packages/core/API.md +++ b/packages/core/API.md @@ -45,15 +45,16 @@ Details of each property can be found by clicking on it. ## Ref Methods -| Name | Description | -| ------------------------------------- | ------------------------------------------------------------------------------------------------------------ | -| [appendRow](#appendrow) | Append a row to the data grid. | -| [emit](#emit) | Used to emit commands normally emitted by keyboard shortcuts. | -| [focus](#focus) | Focuses the data grid. | -| [getBounds](#getbounds) | Gets the current screen-space bounds of a desired cell. | -| [remeasureColumns](#remeasureColumns) | Causes the columns in the selection to have their natural sizes recomputed and re-emitted as a resize event. | -| [scrollTo](#scrollto) | Tells the data-grid to scroll to a particular location. | -| [updateCells](#updatecells) | Invalidates the rendering of a list of passed cells. | +| Name | Description | +| --------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ | +| [appendRow](#appendrow) | Append a row to the data grid. | +| [emit](#emit) | Used to emit commands normally emitted by keyboard shortcuts. | +| [focus](#focus) | Focuses the data grid. | +| [getBounds](#getbounds) | Gets the current screen-space bounds of a desired cell. | +| [remeasureColumns](#remeasureColumns) | Causes the columns in the selection to have their natural sizes recomputed and re-emitted as a resize event. | +| [scrollTo](#scrollto) | Tells the data-grid to scroll to a particular location. | +| [updateCells](#updatecells) | Invalidates the rendering of a list of passed cells. | +| [getMouseArgsForPosition](#getMouseArgsForPosition) | Gets the mouse args from pointer event position. | ## Required Props diff --git a/packages/core/src/data-editor/data-editor.tsx b/packages/core/src/data-editor/data-editor.tsx index 4736b1099..2ebfea4f0 100644 --- a/packages/core/src/data-editor/data-editor.tsx +++ b/packages/core/src/data-editor/data-editor.tsx @@ -722,6 +722,10 @@ export interface DataEditorRef { * Causes the columns in the selection to have their natural size recomputed and re-emitted as a resize event. */ remeasureColumns: (cols: CompactSelection) => void; + /** + * Gets the mouse args from pointer event position. + */ + getMouseArgsForPosition: (posX: number, posY: number, ev?: MouseEvent | TouchEvent) => GridMouseEventArgs } const loadingCell: GridCell = { @@ -3912,6 +3916,17 @@ const DataEditorImpl: React.ForwardRefRenderFunction { + if (gridRef?.current === null) { + return undefined; + } + + const args = gridRef.current.getMouseArgsForPosition(posX, posY, ev); + return { + ...args, // FIXME: Mutate + location: [args.location[0] - rowMarkerOffset, args.location[1]] as any, + }; + } }), [appendRow, normalSizeColumn, scrollRef, onCopy, onKeyDown, onPasteInternal, rowMarkerOffset, scrollTo] ); diff --git a/packages/core/src/internal/data-grid/data-grid.tsx b/packages/core/src/internal/data-grid/data-grid.tsx index 52338a786..7fec46509 100644 --- a/packages/core/src/internal/data-grid/data-grid.tsx +++ b/packages/core/src/internal/data-grid/data-grid.tsx @@ -1708,6 +1708,13 @@ const DataGrid: React.ForwardRefRenderFunction = (p, return getBoundsForItem(canvasRef.current, col ?? 0, row ?? -1); }, damage, + getMouseArgsForPosition: (posX: number, posY: number, ev?: MouseEvent | TouchEvent) => { + if (canvasRef === undefined || canvasRef.current === null) { + return undefined; + } + + return getMouseArgsForPosition(canvasRef.current, posX, posY, ev); + } }), [canvasRef, damage, getBoundsForItem] );