Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accordion #131

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
},
"dependencies": {
"@radix-ui/react-dropdown-menu": "^2.0.6",
"@radix-ui/react-accordion": "^1.1.2",
"@radix-ui/react-form": "^0.0.3",
"@radix-ui/react-separator": "^1.0.3",
"@radix-ui/react-tooltip": "^1.0.6",
Expand Down
41 changes: 41 additions & 0 deletions src/components/Accordion/Accordion.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright 2023 New Vector Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

.trigger {
all: unset;
display: flex;
flex-direction: row;
align-items: start;
gap: var(--cpd-space-2x);
cursor: pointer;
}

.trigger:focus {
outline: 2px solid var(--cpd-color-border-focused);
outline-offset: 1px;
}

.icon {
transform: rotate(0);
transform-origin: center center;
transition: 300ms ease;
color: var(--cpd-color-icon-secondary);
flex-shrink: 0;
}

.trigger[aria-expanded="true"] .icon {
transform: rotate(90deg);
}
84 changes: 84 additions & 0 deletions src/components/Accordion/Accordion.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
Copyright 2023 New Vector Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import React from "react";
import { Meta } from "@storybook/react";
import { Item } from "./Accordion";

import { Root as AccordionComponent } from "./Accordion";

const LOREM_IPSUM =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.";

export default {
title: "Accordion",
component: AccordionComponent,
tags: ["autodocs"],
argTypes: {
onValueChange: { action: "onValueChange" },
},
args: {
collapsible: true,
children: (
<>
<Item trigger="Item 1" value="item1">
{LOREM_IPSUM}
</Item>
<Item trigger="Item 2" value="item2">
{LOREM_IPSUM}
</Item>
<Item trigger="Item 3" value="item3">
{LOREM_IPSUM}
</Item>
</>
),
},
} as Meta<typeof AccordionComponent>;

export const Default = {
args: {},
parameters: {},
};

export const Single = {
args: {
type: "single",
},
parameters: {},
};

export const Multiple = {
args: {
type: "multiple",
},
parameters: {},
};

export const SingleControlled = {
args: {
value: "item1",
type: "single",
},
parameters: {},
};

export const MultipleControlled = {
args: {
value: ["item1", "item2"],
type: "multiple",
},
parameters: {},
};
156 changes: 156 additions & 0 deletions src/components/Accordion/Accordion.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
Copyright 2023 New Vector Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { describe, it, expect, vi, afterEach } from "vitest";
import { fireEvent, render, screen } from "@testing-library/react";
import React from "react";

import * as stories from "./Accordion.stories";
import { composeStories, composeStory } from "@storybook/react";

const { Default, Single, Multiple } = composeStories(stories);

describe("<Accordion />", () => {
it("renders an uncontrolled single accordion by default", () => {
const { container } = render(<Default />);
expect(container).toMatchSnapshot();
});

it("renders an uncontrolled single accordion", () => {
const { container } = render(<Single />);
expect(container).toMatchSnapshot();

fireEvent.click(screen.getByText("Item 1"));

expect(screen.getByText("Item 1").getAttribute("data-state")).toEqual(
"open",
);
fireEvent.click(screen.getByText("Item 2"));

expect(screen.getByText("Item 1").getAttribute("data-state")).toEqual(
"closed",
);
expect(screen.getByText("Item 2").getAttribute("data-state")).toEqual(
"open",
);
});

it("renders an uncontrolled multiple accordion", () => {
const { container } = render(<Multiple />);
expect(container).toMatchSnapshot();

fireEvent.click(screen.getByText("Item 1"));
expect(screen.getByText("Item 1").getAttribute("data-state")).toEqual(
"open",
);

fireEvent.click(screen.getByText("Item 2"));
// both open
expect(screen.getByText("Item 1").getAttribute("data-state")).toEqual(
"open",
);
expect(screen.getByText("Item 2").getAttribute("data-state")).toEqual(
"open",
);
});

describe("single controlled", () => {
const onValueChange = vi.fn();
// compose the story with our mocked onValueChange
const SingleControlled = composeStory(
{
...stories.SingleControlled,
args: {
...stories.SingleControlled.args,
onValueChange,
},
},
stories.default,
);

afterEach(() => {
onValueChange.mockClear();
});

it("renders a controlled single accordion with an item open", () => {
render(<SingleControlled />);

expect(screen.getByText("Item 1").getAttribute("data-state")).toEqual(
"open",
);
});

it("handles click on an expanded item", () => {
render(<SingleControlled />);

fireEvent.click(screen.getByText("Item 1"));
expect(onValueChange).toHaveBeenCalledWith("");
});

it("handles click on a collapsed item", () => {
render(<SingleControlled />);

fireEvent.click(screen.getByText("Item 2"));
expect(onValueChange).toHaveBeenCalledWith("item2");
});
});

describe("multiple controlled", () => {
const onValueChange = vi.fn();
// compose the story with our mocked onValueChange
const MultipleControlled = composeStory(
{
...stories.MultipleControlled,
args: {
...stories.MultipleControlled.args,
onValueChange,
},
},
stories.default,
);

afterEach(() => {
onValueChange.mockClear();
});

it("renders a controlled single accordion with an item open", () => {
render(<MultipleControlled />);

expect(screen.getByText("Item 1").getAttribute("data-state")).toEqual(
"open",
);
expect(screen.getByText("Item 2").getAttribute("data-state")).toEqual(
"open",
);
});

it("handles click on an expanded item", () => {
render(<MultipleControlled />);

fireEvent.click(screen.getByText("Item 1"));
// item1 removed from array of open items
expect(onValueChange).toHaveBeenCalledWith(["item2"]);
});

it("handles click on a collapsed item", () => {
render(<MultipleControlled />);

fireEvent.click(screen.getByText("Item 3"));
// item3 added to array of open items
expect(onValueChange).toHaveBeenCalledWith(["item1", "item2", "item3"]);
});
});
});
Loading
Loading