mirror of
https://github.com/nextui-org/nextui.git
synced 2025-12-08 19:26:11 +00:00
fix(tabs): set tab panel id correctly (#3246)
This commit is contained in:
parent
a06422f373
commit
d8ceab3579
5
.changeset/rich-shirts-turn.md
Normal file
5
.changeset/rich-shirts-turn.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
"@nextui-org/tabs": patch
|
||||
---
|
||||
|
||||
Fixed set tab panel id correctly (#2809)
|
||||
@ -7,7 +7,6 @@ export default function App() {
|
||||
<RadioGroup
|
||||
className="mb-4"
|
||||
label="Placement"
|
||||
orientation="top"
|
||||
value={placement}
|
||||
onValueChange={(value) => setPlacement(value)}
|
||||
>
|
||||
|
||||
@ -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";
|
||||
|
||||
@ -11,7 +11,7 @@ type Item = {
|
||||
content?: React.ReactNode;
|
||||
};
|
||||
|
||||
let tabs: Item[] = [
|
||||
let defaultItems: Item[] = [
|
||||
{
|
||||
id: "item1",
|
||||
label: "Item1 ",
|
||||
@ -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>
|
||||
@ -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>();
|
||||
|
||||
|
||||
@ -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();
|
||||
|
||||
|
||||
@ -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"];
|
||||
}
|
||||
|
||||
const isDisabled = isDisabledProp || isDisabledItem;
|
||||
|
||||
const {focusProps, isFocused, isFocusVisible} = useFocusRing();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user