Skip to content

Commit

Permalink
fix(tabs): destroyInactiveTabPanel unmounts inactive tabs' content (#…
Browse files Browse the repository at this point in the history
…3164)

* fix(tabs): incorrect content in tab panel

* feat(tabs): revise destroyInactiveTabPanel test cases
  • Loading branch information
wingkwong committed Jun 7, 2024
1 parent a0d6a77 commit 5c83e9c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-scissors-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nextui-org/tabs": patch
---

Fixed incorrect content in tab panel (#3159)
58 changes: 40 additions & 18 deletions packages/components/tabs/__tests__/tabs.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from "react";
import {act, render} from "@testing-library/react";
import {act, render, fireEvent} from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import {focus} from "@nextui-org/test-utils";

Expand Down Expand Up @@ -322,36 +322,58 @@ describe("Tabs", () => {
test("should destory inactive tab panels", () => {
const {container} = render(
<Tabs aria-label="Tabs test (destroyInactiveTabPanel=true)">
<Tab key="item1" title="Item 1">
<div>Content 1</div>
</Tab>
<Tab key="item2" title="Item 2">
<div>Content 2</div>
<Tab key="tab1" data-testid="item1" title="Tab 1">
<input className="border-2" data-testid="input" id="firstTab" />
</Tab>
<Tab key="item3" title="Item 3">
<div>Content 3</div>
<Tab key="tab2" data-testid="item2" title="Tab 2">
<p id="secondTab">second tab content</p>
</Tab>
</Tabs>,
);

expect(container.querySelectorAll("[data-slot='panel']")).toHaveLength(1);
});

test("should destory inactive tab panels", () => {
const {container} = render(
test("should not destory inactive tab panels", async () => {
const wrapper = render(
<Tabs aria-label="Tabs test (destroyInactiveTabPanel=false)" destroyInactiveTabPanel={false}>
<Tab key="item1" title="Item 1">
<div>Content 1</div>
</Tab>
<Tab key="item2" title="Item 2">
<div>Content 2</div>
<Tab key="tab1" data-testid="item1" title="Tab 1">
<input className="border-2" data-testid="input" id="firstTab" />
</Tab>
<Tab key="item3" title="Item 3">
<div>Content 3</div>
<Tab key="tab2" data-testid="item2" title="Tab 2">
<p id="secondTab">second tab content</p>
</Tab>
</Tabs>,
);

expect(container.querySelectorAll("[data-slot='panel']")).toHaveLength(3);
const {container} = wrapper;

expect(container.querySelectorAll("[data-slot='panel']")).toHaveLength(2);

const tab1 = wrapper.getByTestId("item1");
const tab2 = wrapper.getByTestId("item2");
const input = wrapper.getByTestId("input");

fireEvent.change(input, {target: {value: "23"}});

expect(input).toHaveValue("23");

act(() => {
focus(tab1);
});

await act(async () => {
await userEvent.keyboard("[ArrowRight]");
});

expect(tab2).toHaveFocus();

await act(async () => {
await userEvent.keyboard("[ArrowLeft]");
});

expect(tab1).toHaveFocus();

expect(input).toHaveValue("23");
});
});
2 changes: 1 addition & 1 deletion packages/components/tabs/src/tab-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const TabPanel = forwardRef<"div", TabPanelProps>((props, ref) => {

const selectedItem = state.selectedItem;

const content = selectedItem?.props?.children;
const content = state.collection.getItem(tabKey)!.props.children;

const tabPanelStyles = clsx(classNames?.panel, className, selectedItem?.props?.className);

Expand Down

0 comments on commit 5c83e9c

Please sign in to comment.