Skip to content

Commit

Permalink
Merge branch 'main' into merx-1310-add-source-header
Browse files Browse the repository at this point in the history
  • Loading branch information
poulch authored Jan 31, 2025
2 parents 598e5ed + 1743872 commit 0b1999b
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/five-bears-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"saleor-dashboard": patch
---

Opening item in new tab using cmd key on datagrid now takes into account mounting point
19 changes: 4 additions & 15 deletions src/components/Datagrid/Datagrid.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "@glideapps/glide-data-grid/dist/index.css";

import useNavigator, { NavigatorOpts } from "@dashboard/hooks/useNavigator";
import { useRowAnchorHandler } from "@dashboard/components/Datagrid/hooks/useRowAnchorHandler";
import { NavigatorOpts } from "@dashboard/hooks/useNavigator";
import { usePreventHistoryBack } from "@dashboard/hooks/usePreventHistoryBack";
import { getCellAction } from "@dashboard/products/components/ProductListDatagrid/datagrid";
import DataEditor, {
Expand Down Expand Up @@ -139,7 +140,6 @@ export const Datagrid: React.FC<DatagridProps> = ({
const datagridTheme = useDatagridTheme(readonly, readonly);
const editor = useRef<DataEditorRef | null>(null);
const customRenderers = useCustomCellRenderers();
const navigate = useNavigator();
const { scrolledToRight, scroller } = useScrollRight();
const fullScreenClasses = useFullScreenStyles(classes);
const { isOpen, isAnimationOpenFinished, toggle } = useFullScreenMode();
Expand All @@ -152,6 +152,7 @@ export const Datagrid: React.FC<DatagridProps> = ({
rowMarkers,
availableColumns,
});
const rowAnchorHandler = useRowAnchorHandler(navigatorOpts);

const { handleRowHover, hoverRow } = useRowHover({
hasRowHover,
Expand Down Expand Up @@ -529,19 +530,7 @@ export const Datagrid: React.FC<DatagridProps> = ({
tabIndex={-1}
aria-hidden={true}
onWheelCapture={hideLinkAndShowAfterDelay}
onClick={e => {
e.preventDefault();

if (e.currentTarget.dataset.reactRouterPath) {
const url = e.currentTarget.dataset.reactRouterPath;

if (e.metaKey || e.ctrlKey) {
window.open(url, "_blank");
} else {
navigate(url, navigatorOpts);
}
}
}}
onClick={rowAnchorHandler}
/>
)}
</FullScreenContainer>
Expand Down
80 changes: 80 additions & 0 deletions src/components/Datagrid/hooks/useRowAnchorHandler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import useNavigator from "@dashboard/hooks/useNavigator";
import { renderHook } from "@testing-library/react-hooks";

import { useRowAnchorHandler } from "./useRowAnchorHandler";

jest.mock("@dashboard/hooks/useNavigator", () => jest.fn());

jest.mock("@dashboard/hooks/useNavigator", () => ({
__esModule: true,
default: jest.fn(() => jest.fn()),
}));

describe("useRowAnchorHandler", () => {
it("should navigate to the given path", () => {
// Arrange
const navigate = jest.fn();

(useNavigator as jest.Mock).mockReturnValue(navigate);

const navigatorOpts = { replace: true };
const handler = renderHook(() => useRowAnchorHandler(navigatorOpts)).result.current;
const event = {
preventDefault: jest.fn(),
currentTarget: {
dataset: {
reactRouterPath: "/some-path",
},
},
};

// Act
handler(event as any);

// Assert
expect(event.preventDefault).toHaveBeenCalled();
expect(navigate).toHaveBeenCalledWith("/some-path", navigatorOpts);
});

it("should not prevent default when CMD key is pressed", () => {
// Arrange
const navigate = jest.fn();

(useNavigator as jest.Mock).mockReturnValue(navigate);

const handler = renderHook(() => useRowAnchorHandler()).result.current;
const event = {
preventDefault: jest.fn(),
metaKey: true,
ctrlKey: false,
};

// Act
handler(event as any);

// Assert
expect(event.preventDefault).not.toHaveBeenCalled();
expect(navigate).not.toHaveBeenCalled();
});

it("should not prevent default when CTRL key is pressed", () => {
// Arrange
const navigate = jest.fn();

(useNavigator as jest.Mock).mockReturnValue(navigate);

const handler = renderHook(() => useRowAnchorHandler()).result.current;
const event = {
preventDefault: jest.fn(),
metaKey: false,
ctrlKey: true,
};

// Act
handler(event as any);

// Assert
expect(event.preventDefault).not.toHaveBeenCalled();
expect(navigate).not.toHaveBeenCalled();
});
});
22 changes: 22 additions & 0 deletions src/components/Datagrid/hooks/useRowAnchorHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import useNavigator, { NavigatorOpts } from "@dashboard/hooks/useNavigator";
import { MouseEvent } from "react";

export const useRowAnchorHandler = (navigatorOpts?: NavigatorOpts) => {
const navigate = useNavigator();

return (e: MouseEvent<HTMLAnchorElement>) => {
// When someone clicks with CMD key to open in new tab, we should not prevent default
if (e.metaKey || e.ctrlKey) {
return;
}

// Prevent default when navigate with browser router
e.preventDefault();

if (e.currentTarget.dataset.reactRouterPath) {
// Navigate gets only a path to navigate, for example, /products/1
// Navigate use browser router and cover case when url is with /dashboard or not
navigate(e.currentTarget.dataset.reactRouterPath, navigatorOpts);
}
};
};

0 comments on commit 0b1999b

Please sign in to comment.