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

fix(tabs): set tab panel id correctly #3246

Merged
merged 1 commit into from
Jun 14, 2024
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/rich-shirts-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nextui-org/tabs": patch
---

Fixed set tab panel id correctly (#2809)
1 change: 0 additions & 1 deletion apps/docs/content/components/tabs/placement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export default function App() {
<RadioGroup
className="mb-4"
label="Placement"
orientation="top"
value={placement}
onValueChange={(value) => setPlacement(value)}
>
Expand Down
40 changes: 37 additions & 3 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, fireEvent} from "@testing-library/react";
import {act, render, fireEvent, within} from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import {focus} from "@nextui-org/test-utils";

Expand All @@ -11,7 +11,7 @@ type Item = {
content?: React.ReactNode;
};

let tabs: Item[] = [
let defaultItems: Item[] = [
{
id: "item1",
label: "Item1 ",
Expand Down Expand Up @@ -76,7 +76,7 @@ describe("Tabs", () => {

it("should render correctly (dynamic)", () => {
const wrapper = render(
<Tabs aria-label="Tabs static test" items={tabs}>
<Tabs aria-label="Tabs static test" items={defaultItems}>
{(item) => (
<Tab key={item.id} title={item.label}>
<div>{item.content}</div>
Expand All @@ -88,6 +88,40 @@ describe("Tabs", () => {
expect(() => wrapper.unmount()).not.toThrow();
});

it("renders property", () => {
const wrapper = render(
<Tabs aria-label="Tabs property test">
{defaultItems.map((item) => (
<Tab key={item.id} title={item.label}>
<div>{item.content}</div>
</Tab>
))}
</Tabs>,
);
const tablist = wrapper.getByRole("tablist");

expect(tablist).toBeTruthy();
const tabs = within(tablist).getAllByRole("tab");

expect(tabs.length).toBe(3);

for (let tab of tabs) {
expect(tab).toHaveAttribute("tabindex");
expect(tab).toHaveAttribute("aria-selected");
const isSelected = tab.getAttribute("aria-selected") === "true";

if (isSelected) {
expect(tab).toHaveAttribute("aria-controls");
const tabpanel = document.getElementById(tab.getAttribute("aria-controls")!);

expect(tabpanel).toBeTruthy();
expect(tabpanel).toHaveAttribute("aria-labelledby", tab.id);
expect(tabpanel).toHaveAttribute("role", "tabpanel");
expect(tabpanel).toHaveTextContent(defaultItems[0]?.content as string);
}
}
});

it("ref should be forwarded", () => {
const ref = React.createRef<HTMLDivElement>();

Expand Down
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 @@ -47,7 +47,7 @@ const TabPanel = forwardRef<"div", TabPanelProps>((props, ref) => {

const domRef = useDOMRef(ref);

const {tabPanelProps} = useTabPanel(props, state, domRef);
const {tabPanelProps} = useTabPanel({...props, id: String(tabKey)}, state, domRef);

const {focusProps, isFocused, isFocusVisible} = useFocusRing();

Expand Down
4 changes: 4 additions & 0 deletions packages/components/tabs/src/tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ const Tab = forwardRef<"button", TabItemProps>((props, ref) => {
isPressed,
} = useTab({key, isDisabled: isDisabledProp, shouldSelectOnPressUp}, state, domRef);

if (props.children == null) {
delete tabProps["aria-controls"];
}
Comment on lines +64 to +66
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When displaying tabs only, like in a catalog, aria-controls is not set.

Comment on lines +64 to +66
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor to avoid using the delete operator, which can impact performance.

-  if (props.children == null) {
-    delete tabProps["aria-controls"];
-  }
+  if (props.children == null) {
+    tabProps["aria-controls"] = undefined;
+  }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (props.children == null) {
delete tabProps["aria-controls"];
}
if (props.children == null) {
tabProps["aria-controls"] = undefined;
}
Tools
Biome

[error] 65-65: Avoid the delete operator which can impact performance. (lint/performance/noDelete)

Unsafe fix: Use an undefined assignment instead.


const isDisabled = isDisabledProp || isDisabledItem;

const {focusProps, isFocused, isFocusVisible} = useFocusRing();
Expand Down
Loading