Skip to content

Commit

Permalink
feat(Collapse): set a11y attributes and props
Browse files Browse the repository at this point in the history
  • Loading branch information
sarkaaa committed Jan 9, 2025
1 parent 9c4e6b2 commit 2e0e87e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 37 deletions.
6 changes: 6 additions & 0 deletions packages/orbit-components/src/Collapse/Collapse.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ export const Uncontrolled: Story = {
args: {
expanded: undefined,
},

parameters: {
controls: {
exclude: "expanded",
},
},
};

export const Rtl: Story = {
Expand Down
22 changes: 13 additions & 9 deletions packages/orbit-components/src/Collapse/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import userEvent from "@testing-library/user-event";
import { render, screen } from "../../test-utils";
import Collapse from "..";

const toggleButtons = [
[0, "label"],
[1, "icon button"],
];
const toggleTriggers = ["div", "button"];

describe("Collapse", () => {
const user = userEvent.setup();
Expand All @@ -31,27 +28,34 @@ describe("Collapse", () => {
expect(screen.getByText("customLabel")).toBeInTheDocument();
});

it.each(toggleButtons)("should trigger click handler when clicking on %s", async buttonIndex => {
it.each(toggleTriggers)("should trigger click handler when clicking on %s", async trigger => {
const onClick = jest.fn();
render(
<Collapse label="Collapse" onClick={onClick}>
<div>children</div>
</Collapse>,
);
const toggleButton = screen.getAllByRole("button", { name: "Collapse" })[buttonIndex];

const toggleButton =
trigger === "div" ? screen.getByText("Collapse") : screen.getByRole(trigger);

await user.click(toggleButton);
expect(onClick).toHaveBeenCalled();
});

describe("uncontrolled", () => {
it.each(toggleButtons)("should expand/collapse when clicking on %s", async buttonIndex => {
it.each(toggleTriggers)("should expand/collapse when clicking on %s", async trigger => {
render(
<Collapse label="Collapse">
<article>children</article>
</Collapse>,
);
const toggleButton = screen.getAllByRole("button", { name: "Collapse" })[buttonIndex];
// with ByRole we can test whether the content is visible because of aria-hidden

const toggleButton =
// We need to distinguish between div and button on the next line
// eslint-disable-next-line jest/no-if
trigger === "div" ? screen.getByText("Collapse") : screen.getByRole("button");

expect(screen.queryByRole("article")).not.toBeInTheDocument();
await user.click(toggleButton);
expect(screen.getByRole("article")).toBeInTheDocument();
Expand Down
59 changes: 31 additions & 28 deletions packages/orbit-components/src/Collapse/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,36 +64,39 @@ const Collapse = ({
data-test={dataTest}
id={id}
>
{/* eslint-disable-next-line jsx-a11y/click-events-have-key-events */}
<div
className="block w-full cursor-pointer"
onClick={handleClick}
role="button"
tabIndex={-1}
id={labelID}
>
<Stack justify="between" align="center">
{label && !customLabel && <Heading type="title4">{label}</Heading>}
{customLabel}
<Stack inline grow={false} align="center" spacing="300">
{/* eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */}
<div
className="flex items-center"
onClick={ev => {
ev.stopPropagation();
}}
>
<div className="flex items-center justify-between">
<div
className="flex w-full cursor-pointer self-stretch"
id={labelID}
role="button"
tabIndex={0}
onClick={handleClick}
onKeyDown={e => {
if (e.key === "Enter" || e.key === " ") {
handleClick(e);
}
}}
>
<Stack justify="between" align="center">
{label && !customLabel && <Heading type="title4">{label}</Heading>}
{customLabel}
</Stack>
</div>
<Stack inline grow={false} align="center" spacing="none">
{actions && (
<div aria-labelledby={labelID} className="mx-300 flex items-center">
{actions}
</div>
<ButtonLink
iconLeft={<AnimatedIcon expanded={expanded} />}
size="small"
type="secondary"
ariaLabelledby={labelID}
ariaExpanded={expanded}
ariaControls={slideID}
/>
</Stack>
)}
<ButtonLink
iconLeft={<AnimatedIcon expanded={expanded} />}
size="small"
type="secondary"
onClick={handleClick}
ariaLabelledby={labelID}
ariaExpanded={expanded}
ariaControls={slideID}
/>
</Stack>
</div>
<Slide maxHeight={height} expanded={expanded} id={slideID} ariaLabelledBy={labelID}>
Expand Down

0 comments on commit 2e0e87e

Please sign in to comment.