diff --git a/src/components/Grid/Grid.tsx b/src/components/Grid/Grid.tsx index 6baad190..04024f4f 100644 --- a/src/components/Grid/Grid.tsx +++ b/src/components/Grid/Grid.tsx @@ -7,6 +7,7 @@ import { forwardRef, useCallback, useEffect, + useMemo, useRef, useState, } from "react"; @@ -136,6 +137,7 @@ export const Grid = forwardRef( onMouseMove: onMouseMoveProp, showBorder = false, onCopy: onCopyProp, + onCopyCallback, onContextMenu: onContextMenuProp, forwardedGridRef, ...props @@ -153,8 +155,8 @@ export const Grid = forwardRef( type: "empty", } ); - const onCopy = useCallback(async () => { - let isCopied = false; + + const defaultOnCopy: () => Promise = useCallback(async () => { try { await copyGridElements({ cell, @@ -164,7 +166,10 @@ export const Grid = forwardRef( columnCount, outerRef: outerRef, }); - isCopied = true; + + if (onCopyCallback) { + onCopyCallback(true); + } if (showToast) { createToast({ title: "Copied successfully", @@ -174,6 +179,11 @@ export const Grid = forwardRef( } } catch (e) { console.error(e); + + if (onCopyCallback) { + onCopyCallback(false); + } + if (showToast) { createToast({ title: "Failed to copy", @@ -181,11 +191,19 @@ export const Grid = forwardRef( type: "danger", }); } - if (typeof onCopyProp === "function") { - onCopyProp(isCopied); + } + }, [cell, columnCount, focus, focusProp, rowCount, selection, showToast, onCopyCallback]); + + const customOnCopy: () => Promise = useMemo(() => { + const result = async () => { + if(onCopyProp) { + await onCopyProp(selection, focus) } } - }, [cell, columnCount, focus, focusProp, onCopyProp, rowCount, selection, showToast]); + return result; + }, [onCopyProp, selection, focus]); + + const onCopy: () => Promise = typeof onCopyProp === "function" ? customOnCopy: defaultOnCopy; const defaultMenuOptions = [ { diff --git a/src/components/Grid/types.ts b/src/components/Grid/types.ts index 670f68f3..d6133f2f 100644 --- a/src/components/Grid/types.ts +++ b/src/components/Grid/types.ts @@ -198,7 +198,8 @@ export interface GridProps onMouseDown?: MouseEventHandler; onMouseMove?: MouseEventHandler; showBorder?: boolean; - onCopy?: (isCopied: boolean) => void | Promise; + onCopy?: (selection: SelectedRegion, focus: SelectionFocus) => void | Promise; + onCopyCallback?: (copied: boolean) => void; onContextMenu?: MouseEventHandler; forwardedGridRef?: MutableRefObject; } diff --git a/src/components/types.ts b/src/components/types.ts index 1f453021..087a204b 100644 --- a/src/components/types.ts +++ b/src/components/types.ts @@ -56,6 +56,7 @@ export type { SelectionFocus, SelectionAction, GridContextMenuItemProps, + Rectangle } from "./Grid/types"; export type States = "default" | "active" | "disabled" | "error" | "hover";