Skip to content

Commit

Permalink
adding ability to override copy function from client (#400)
Browse files Browse the repository at this point in the history
* adding ability to override copy function from client

* removing yarn file

* fixing empty space

* adding callback

* adding focus prop
  • Loading branch information
fneves authored Apr 24, 2024
1 parent 006d5be commit 15f0a0c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
30 changes: 24 additions & 6 deletions src/components/Grid/Grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
forwardRef,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from "react";
Expand Down Expand Up @@ -136,6 +137,7 @@ export const Grid = forwardRef<HTMLDivElement, GridProps>(
onMouseMove: onMouseMoveProp,
showBorder = false,
onCopy: onCopyProp,
onCopyCallback,
onContextMenu: onContextMenuProp,
forwardedGridRef,
...props
Expand All @@ -153,8 +155,8 @@ export const Grid = forwardRef<HTMLDivElement, GridProps>(
type: "empty",
}
);
const onCopy = useCallback(async () => {
let isCopied = false;

const defaultOnCopy: () => Promise<void> = useCallback(async () => {
try {
await copyGridElements({
cell,
Expand All @@ -164,7 +166,10 @@ export const Grid = forwardRef<HTMLDivElement, GridProps>(
columnCount,
outerRef: outerRef,
});
isCopied = true;

if (onCopyCallback) {
onCopyCallback(true);
}
if (showToast) {
createToast({
title: "Copied successfully",
Expand All @@ -174,18 +179,31 @@ export const Grid = forwardRef<HTMLDivElement, GridProps>(
}
} catch (e) {
console.error(e);

if (onCopyCallback) {
onCopyCallback(false);
}

if (showToast) {
createToast({
title: "Failed to copy",
description: "Encountered an error while copying. Try again after sometime",
type: "danger",
});
}
if (typeof onCopyProp === "function") {
onCopyProp(isCopied);
}
}, [cell, columnCount, focus, focusProp, rowCount, selection, showToast, onCopyCallback]);

const customOnCopy: () => Promise<void> = 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<void> = typeof onCopyProp === "function" ? customOnCopy: defaultOnCopy;

const defaultMenuOptions = [
{
Expand Down
3 changes: 2 additions & 1 deletion src/components/Grid/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ export interface GridProps
onMouseDown?: MouseEventHandler<HTMLDivElement>;
onMouseMove?: MouseEventHandler<HTMLDivElement>;
showBorder?: boolean;
onCopy?: (isCopied: boolean) => void | Promise<void>;
onCopy?: (selection: SelectedRegion, focus: SelectionFocus) => void | Promise<void>;
onCopyCallback?: (copied: boolean) => void;
onContextMenu?: MouseEventHandler<HTMLDivElement>;
forwardedGridRef?: MutableRefObject<VariableSizeGrid>;
}
Expand Down
1 change: 1 addition & 0 deletions src/components/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export type {
SelectionFocus,
SelectionAction,
GridContextMenuItemProps,
Rectangle
} from "./Grid/types";

export type States = "default" | "active" | "disabled" | "error" | "hover";
Expand Down

0 comments on commit 15f0a0c

Please sign in to comment.