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 14, 2025
1 parent a3bf812 commit cb02521
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 46 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
32 changes: 15 additions & 17 deletions packages/orbit-components/src/Collapse/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ import userEvent from "@testing-library/user-event";
import { render, screen } from "../../test-utils";
import Collapse from "..";

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

describe("Collapse", () => {
const user = userEvent.setup();

Expand All @@ -31,32 +26,35 @@ describe("Collapse", () => {
expect(screen.getByText("customLabel")).toBeInTheDocument();
});

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

screen.getAllByRole("button").forEach(async toggleButton => {
await user.click(toggleButton);
expect(onClick).toHaveBeenCalled();
});
});

describe("uncontrolled", () => {
it.each(toggleButtons)("should expand/collapse when clicking on %s", async buttonIndex => {
it("should expand/collapse when clicking on button", async () => {
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
expect(screen.queryByRole("article")).not.toBeInTheDocument();
await user.click(toggleButton);
expect(screen.getByRole("article")).toBeInTheDocument();
await user.click(toggleButton);
expect(screen.queryByRole("article")).not.toBeInTheDocument();

await screen.getAllByRole("button").forEach(async toggleButton => {
expect(screen.queryByRole("article")).not.toBeInTheDocument();
await user.click(toggleButton);
expect(screen.getByRole("article")).toBeInTheDocument();
await user.click(toggleButton);
expect(screen.queryByRole("article")).not.toBeInTheDocument();
});
});
});

Expand Down
57 changes: 28 additions & 29 deletions packages/orbit-components/src/Collapse/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,36 +64,35 @@ 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();
}}
>
{actions}
</div>
<ButtonLink
iconLeft={<AnimatedIcon expanded={expanded} />}
size="small"
type="secondary"
ariaLabelledby={labelID}
ariaExpanded={expanded}
ariaControls={slideID}
/>
<div className="flex items-center justify-between">
<div
className="flex w-full 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 className="mx-300 flex items-center">{actions}</div>}
<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 cb02521

Please sign in to comment.