Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/smooth-bears-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@heroui/accordion": patch
---

fixed keepContentMounted with disableAnimation (#5157)
96 changes: 93 additions & 3 deletions packages/components/accordion/__tests__/accordion.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "@testing-library/jest-dom";
import * as React from "react";
import {act, render} from "@testing-library/react";
import {act, render, waitFor} from "@testing-library/react";
import {focus, shouldIgnoreReactWarning, spy} from "@heroui/test-utils";
import userEvent, {UserEvent} from "@testing-library/user-event";
import {Input} from "@heroui/input";
Expand Down Expand Up @@ -267,7 +267,7 @@ describe("Accordion", () => {
expect(button).toHaveAttribute("aria-expanded", "true");
});

it("should support keepContentMounted", async () => {
it("should support keepContentMounted={true}", async () => {
const wrapper = render(
<Accordion keepContentMounted>
<AccordionItem key="1" data-testid="item-1" title="Accordion Item 1">
Expand All @@ -289,8 +289,98 @@ describe("Accordion", () => {
const button2 = item2.querySelector("button") as HTMLElement;

await user.click(button2);

await waitFor(() => {
expect(item1.querySelector("[role='region']")).toBeInTheDocument();
expect(item2.querySelector("[role='region']")).toBeInTheDocument();
});
});

it("should support keepContentMounted={false}", async () => {
const wrapper = render(
<Accordion keepContentMounted={false}>
<AccordionItem key="1" data-testid="item-1" title="Accordion Item 1">
Accordion Item 1 description
</AccordionItem>
<AccordionItem key="2" data-testid="item-2" title="Accordion Item 2">
Accordion Item 2 description
</AccordionItem>
</Accordion>,
);

const item1 = wrapper.getByTestId("item-1");
const button = item1.querySelector("button") as HTMLElement;

expect(item1.querySelector("[role='region']")).not.toBeInTheDocument();

await user.click(button);
const item2 = wrapper.getByTestId("item-2");
const button2 = item2.querySelector("button") as HTMLElement;

await user.click(button2);

await waitFor(() => {
expect(item1.querySelector("[role='region']")).not.toBeInTheDocument();
expect(item2.querySelector("[role='region']")).toBeInTheDocument();
});
});

it("should support keepContentMounted={true} & disableAnimation={true}", async () => {
const wrapper = render(
<Accordion disableAnimation keepContentMounted>
<AccordionItem key="1" data-testid="item-1" title="Accordion Item 1">
Accordion Item 1 description
</AccordionItem>
<AccordionItem key="2" data-testid="item-2" title="Accordion Item 2">
Accordion Item 2 description
</AccordionItem>
</Accordion>,
);

const item1 = wrapper.getByTestId("item-1");
const button = item1.querySelector("button") as HTMLElement;

expect(item1.querySelector("[role='region']")).toBeInTheDocument();
expect(item2.querySelector("[role='region']")).toBeInTheDocument();

await user.click(button);
const item2 = wrapper.getByTestId("item-2");
const button2 = item2.querySelector("button") as HTMLElement;

await user.click(button2);

await waitFor(() => {
expect(item1.querySelector("[role='region']")).toBeInTheDocument();
expect(item2.querySelector("[role='region']")).toBeInTheDocument();
});
});

it("should support keepContentMounted={false} & disableAnimation={true}", async () => {
const wrapper = render(
<Accordion disableAnimation keepContentMounted={false}>
<AccordionItem key="1" data-testid="item-1" title="Accordion Item 1">
Accordion Item 1 description
</AccordionItem>
<AccordionItem key="2" data-testid="item-2" title="Accordion Item 2">
Accordion Item 2 description
</AccordionItem>
</Accordion>,
);

const item1 = wrapper.getByTestId("item-1");
const button = item1.querySelector("button") as HTMLElement;

expect(item1.querySelector("[role='region']")).not.toBeInTheDocument();

await user.click(button);
const item2 = wrapper.getByTestId("item-2");
const button2 = item2.querySelector("button") as HTMLElement;

await user.click(button2);

await waitFor(() => {
expect(item1.querySelector("[role='region']")).not.toBeInTheDocument();
expect(item2.querySelector("[role='region']")).toBeInTheDocument();
});
});

it("should handle arrow key navigation within Input inside AccordionItem", async () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/components/accordion/src/accordion-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ const AccordionItem = forwardRef<"button", AccordionItemProps>((props, ref) => {

const content = useMemo(() => {
if (disableAnimation) {
return <div {...getContentProps()}>{children}</div>;
if (keepContentMounted) {
return <div {...getContentProps()}>{children}</div>;
}

return isOpen && <div {...getContentProps()}>{children}</div>;
}

const transitionVariants: Variants = {
Expand Down