ammend fix recommendations

This commit is contained in:
Gibson Han
2022-02-02 20:56:52 +07:00
parent cf1f2a8c88
commit e4170715bf
3 changed files with 30 additions and 51 deletions

View File

@@ -8,24 +8,20 @@ export default function ContextMenu() {
const { tableState }: any = useProjectContext();
const { contextMenu, resetContextMenu } = useContextMenuAtom();
const { anchorEl, selectedCell } = contextMenu;
const selectedColIndex = selectedCell?.colIndex;
const columns = tableState?.columns;
const selectedColIndex = selectedCell?.colIndex;
const selectedCol = _find(columns, { index: selectedColIndex });
const configActions =
getFieldProp("contextMenuActions", selectedCol?.type) ||
function empty() {};
const actions = configActions(selectedCell, resetContextMenu) || [];
const hasAnchorEle = Boolean(contextMenu.anchorEl);
const hasNoActions = Boolean(actions.length === 0);
const notOpen = Boolean(!anchorEl || !hasAnchorEle || hasNoActions);
if (notOpen) return <></>;
if (!contextMenu || hasNoActions) return <></>;
return (
<MenuContents
anchorEl={anchorEl as HTMLElement}
open={hasAnchorEle}
open={Boolean(contextMenu.anchorEl)}
handleClose={resetContextMenu}
items={actions}
/>

View File

@@ -1,32 +1,12 @@
import { useProjectContext } from "@src/contexts/ProjectContext";
import useContextMenuAtom from "@src/hooks/useContextMenuAtom";
import { Fragment } from "react";
import { Row, RowRendererProps } from "react-data-grid";
import { IContextMenuActions } from "../fields/_BasicCell/BasicCellContextMenuActions";
import OutOfOrderIndicator from "./OutOfOrderIndicator";
export default function TableRow(props: RowRendererProps<any>) {
if (props.row._rowy_outOfOrder)
return (
<Fragment key={props.row.id}>
<OutOfOrderIndicator top={props.top} height={props.height} />
<ContextMenu>
<Row {...props} />
</ContextMenu>
</Fragment>
);
return (
<ContextMenu>
<Row {...props} />
</ContextMenu>
);
}
const ContextMenu = (props: any) => {
const { setContextMenu } = useContextMenuAtom();
function handleClick(e: any) {
function handleContextMenu(e: React.MouseEvent<HTMLDivElement, MouseEvent>) {
e.preventDefault();
if (setContextMenu)
setContextMenu((prev) => ({
@@ -34,5 +14,13 @@ const ContextMenu = (props: any) => {
anchorEl: e?.target as HTMLElement,
}));
}
return <span onContextMenu={(e) => handleClick(e)}>{props.children}</span>;
};
if (props.row._rowy_outOfOrder)
return (
<Fragment key={props.row.id}>
<OutOfOrderIndicator top={props.top} height={props.height} />
<Row onContextMenu={(e) => handleContextMenu(e)} {...props} />
</Fragment>
);
return <Row onContextMenu={(e) => handleContextMenu(e)} {...props} />;
}

View File

@@ -24,40 +24,35 @@ export default function BasicContextMenuActions(
const selectedColIndex = selectedCell?.colIndex;
const selectedCol = _find(columns, { index: selectedColIndex });
const selectedRow = rows?.[selectedRowIndex];
const cell = selectedRow?.[selectedCol.key];
const handleClose = () => reset?.();
const handleClose = async () => await reset?.();
const handleCopy = () => {
const cell = selectedRow?.[selectedCol.key];
const onFail = () =>
enqueueSnackbar("Failed to copy", { variant: "error" });
const onSuccess = () =>
const handleCopy = async () => {
try {
await navigator.clipboard.writeText(JSON.stringify(cell));
enqueueSnackbar("Copied to clipboard", { variant: "success" });
const copy = navigator.clipboard.writeText(JSON.stringify(cell));
copy.then(onSuccess, onFail);
} catch (error) {
enqueueSnackbar("Failed to copy", { variant: "error" });
}
handleClose();
};
const handleCut = () => {
const cell = selectedRow?.[selectedCol.key];
const notUndefined = Boolean(typeof cell !== "undefined");
if (deleteCell && notUndefined)
deleteCell(selectedRow?.ref, selectedCol?.key);
handleClose();
};
const handlePaste = () => {
const paste = navigator.clipboard.readText();
paste.then(async (clipText) => {
try {
const paste = await JSON.parse(clipText);
updateCell?.(selectedRow?.ref, selectedCol.key, paste);
} catch (error) {
enqueueSnackbar(`${error}`, { variant: "error" });
}
});
const handlePaste = async () => {
try {
const text = await navigator.clipboard.readText();
const paste = await JSON.parse(text);
updateCell?.(selectedRow?.ref, selectedCol.key, paste);
} catch (error) {
enqueueSnackbar(`${error}`, { variant: "error" });
}
handleClose();
};