-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WD-3251 - Unify panel implementations (#1461)
* Add top level panel tests. Unify panel implementations.
- Loading branch information
Showing
39 changed files
with
1,122 additions
and
1,264 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import { render, screen } from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
import { BrowserRouter } from "react-router-dom"; | ||
|
||
import Panel from "./Panel"; | ||
|
||
describe("Panel", () => { | ||
it("displays the title and content", async () => { | ||
window.history.pushState({}, "", "/foo?panel=share-model"); | ||
render( | ||
<BrowserRouter> | ||
<Panel panelClassName="test-panel" title="Test panel"> | ||
<>Test content</> | ||
</Panel> | ||
</BrowserRouter> | ||
); | ||
expect(screen.getByText("Test panel")).toBeInTheDocument(); | ||
expect(screen.getByText("Test content")).toBeInTheDocument(); | ||
}); | ||
|
||
it("should give the panel a class name", async () => { | ||
window.history.pushState({}, "", "/foo?panel=share-model"); | ||
render( | ||
<BrowserRouter> | ||
<Panel panelClassName="test-panel" title="Test panel"> | ||
<>Test content</> | ||
</Panel> | ||
</BrowserRouter> | ||
); | ||
expect(document.querySelector(".p-panel")).toHaveClass("test-panel"); | ||
}); | ||
|
||
it("should clear the search params when escape key is pressed", async () => { | ||
window.history.pushState({}, "", "/foo?panel=share-model"); | ||
render( | ||
<BrowserRouter> | ||
<Panel panelClassName="test-panel" title="Test panel"> | ||
<>Test content</> | ||
</Panel> | ||
</BrowserRouter> | ||
); | ||
expect(window.location.pathname).toBe("/foo"); | ||
expect(window.location.search).toBe("?panel=share-model"); | ||
await userEvent.type(window.document.documentElement, "{Escape}", { | ||
skipClick: true, | ||
}); | ||
expect(window.location.pathname).toBe("/foo"); | ||
expect(window.location.search).toBe(""); | ||
}); | ||
|
||
it("should not close if another key is pressed", async () => { | ||
window.history.pushState({}, "", "/foo?panel=share-model"); | ||
render( | ||
<BrowserRouter> | ||
<Panel panelClassName="test-panel" title="Test panel"> | ||
<>Test content</> | ||
</Panel> | ||
</BrowserRouter> | ||
); | ||
expect(window.location.pathname).toBe("/foo"); | ||
expect(window.location.search).toBe("?panel=share-model"); | ||
await userEvent.type(window.document.documentElement, "{Enter}", { | ||
skipClick: true, | ||
}); | ||
expect(window.location.pathname).toBe("/foo"); | ||
expect(window.location.search).toBe("?panel=share-model"); | ||
}); | ||
|
||
it("should check if it can close when escape key is pressed", async () => { | ||
window.history.pushState({}, "", "/foo?panel=share-model"); | ||
render( | ||
<BrowserRouter> | ||
<Panel | ||
checkCanClose={() => false} | ||
panelClassName="test-panel" | ||
title="Test panel" | ||
> | ||
<>Test content</> | ||
</Panel> | ||
</BrowserRouter> | ||
); | ||
expect(window.location.pathname).toBe("/foo"); | ||
expect(window.location.search).toBe("?panel=share-model"); | ||
await userEvent.type(window.document.documentElement, "{Escape}", { | ||
skipClick: true, | ||
}); | ||
expect(window.location.pathname).toBe("/foo"); | ||
expect(window.location.search).toBe("?panel=share-model"); | ||
}); | ||
|
||
it("should clear the search params when clicking outside the panel", async () => { | ||
window.history.pushState({}, "", "/foo?panel=share-model"); | ||
render( | ||
<BrowserRouter> | ||
<Panel panelClassName="test-panel" title="Test panel"> | ||
<>Test content</> | ||
</Panel> | ||
</BrowserRouter> | ||
); | ||
expect(window.location.pathname).toBe("/foo"); | ||
expect(window.location.search).toBe("?panel=share-model"); | ||
await userEvent.click(window.document.documentElement); | ||
expect(window.location.pathname).toBe("/foo"); | ||
expect(window.location.search).toBe(""); | ||
}); | ||
|
||
it("should not clear the search params when clicking inside the panel", async () => { | ||
window.history.pushState({}, "", "/foo?panel=share-model"); | ||
render( | ||
<BrowserRouter> | ||
<Panel panelClassName="test-panel" title="Test panel"> | ||
<>Test content</> | ||
</Panel> | ||
</BrowserRouter> | ||
); | ||
expect(window.location.pathname).toBe("/foo"); | ||
expect(window.location.search).toBe("?panel=share-model"); | ||
await userEvent.click(screen.getByText("Test content")); | ||
expect(window.location.pathname).toBe("/foo"); | ||
expect(window.location.search).toBe("?panel=share-model"); | ||
}); | ||
|
||
it("should check if it can close when clicking inside the panel", async () => { | ||
window.history.pushState({}, "", "/foo?panel=share-model"); | ||
render( | ||
<BrowserRouter> | ||
<Panel | ||
checkCanClose={() => false} | ||
panelClassName="test-panel" | ||
title="Test panel" | ||
> | ||
<>Test content</> | ||
</Panel> | ||
</BrowserRouter> | ||
); | ||
expect(window.location.pathname).toBe("/foo"); | ||
expect(window.location.search).toBe("?panel=share-model"); | ||
await userEvent.click(window.document.documentElement); | ||
expect(window.location.pathname).toBe("/foo"); | ||
expect(window.location.search).toBe("?panel=share-model"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import type { PropsWithSpread } from "@canonical/react-components"; | ||
import { useListener } from "@canonical/react-components"; | ||
import classNames from "classnames"; | ||
import type { PropsWithChildren, ReactNode, MouseEvent } from "react"; | ||
import { forwardRef, useId } from "react"; | ||
|
||
import Aside from "components/Aside/Aside"; | ||
import type { Props as AsideProps } from "components/Aside/Aside"; | ||
import PanelHeader from "components/PanelHeader/PanelHeader"; | ||
import { useQueryParams } from "hooks/useQueryParams"; | ||
|
||
type Props = PropsWithSpread< | ||
{ | ||
checkCanClose?: (e: KeyboardEvent | MouseEvent) => boolean; | ||
panelClassName: string; | ||
title: ReactNode; | ||
}, | ||
PropsWithChildren & AsideProps | ||
>; | ||
|
||
export const close = { | ||
// Close panel if Escape key is pressed when panel active | ||
onEscape: ( | ||
e: KeyboardEvent, | ||
queryStringSetter: (qs?: string | null) => void, | ||
checkCanClose?: Props["checkCanClose"] | ||
) => { | ||
if (e.code === "Escape") { | ||
if (checkCanClose && !checkCanClose?.(e)) { | ||
return; | ||
} | ||
queryStringSetter(undefined); | ||
} | ||
}, | ||
onClickOutside: ( | ||
e: MouseEvent, | ||
queryStringSetter: (qs?: string | null) => void, | ||
checkCanClose?: Props["checkCanClose"] | ||
) => { | ||
if (checkCanClose && !checkCanClose?.(e)) { | ||
return; | ||
} | ||
const target = e.target as HTMLElement; | ||
if (!target.closest(".p-panel")) { | ||
queryStringSetter(undefined); | ||
} | ||
}, | ||
}; | ||
|
||
export const Panel = forwardRef<HTMLDivElement, Props>( | ||
( | ||
{ checkCanClose, children, panelClassName, title, ...props }: Props, | ||
ref | ||
) => { | ||
const [, setPanelQs] = useQueryParams<{ panel: string | null }>({ | ||
panel: null, | ||
}); | ||
|
||
useListener( | ||
window, | ||
(e: KeyboardEvent) => | ||
close.onEscape( | ||
e, | ||
() => setPanelQs(null, { replace: true }), | ||
checkCanClose | ||
), | ||
"keydown" | ||
); | ||
useListener( | ||
window, | ||
(e: MouseEvent) => | ||
close.onClickOutside( | ||
e, | ||
() => setPanelQs(null, { replace: true }), | ||
checkCanClose | ||
), | ||
"click" | ||
); | ||
|
||
const titleId = useId(); | ||
return ( | ||
<Aside {...props} aria-labelledby={titleId} role="dialog"> | ||
<div className={classNames("p-panel", panelClassName)} ref={ref}> | ||
<PanelHeader id={titleId} title={title} /> | ||
{children} | ||
</div> | ||
</Aside> | ||
); | ||
} | ||
); | ||
|
||
export default Panel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from "./Panel"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.