mirror of
https://github.com/nextui-org/nextui.git
synced 2025-12-08 19:26:11 +00:00
parent
cb1b3135bc
commit
7c2c9c4875
5
.changeset/smooth-mayflies-wonder.md
Normal file
5
.changeset/smooth-mayflies-wonder.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@nextui-org/tabs": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Add tab ref
|
||||||
@ -276,6 +276,7 @@ You can customize the `Tabs` component by passing custom Tailwind CSS classes to
|
|||||||
|
|
||||||
| Attribute | Type | Description | Default |
|
| Attribute | Type | Description | Default |
|
||||||
|-------------------------|-------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------|
|
|-------------------------|-------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------|
|
||||||
|
| tabRef | `RefObject<HTMLButtonElement>`| A ref to the tab item. | - |
|
||||||
| children\* | `ReactNode` | The content of the tab. | - |
|
| children\* | `ReactNode` | The content of the tab. | - |
|
||||||
| title | `ReactNode` | The title of the tab. | - |
|
| title | `ReactNode` | The title of the tab. | - |
|
||||||
| titleValue | `string` | A string representation of the item's contents. Use this when the `title` is not readable. | - |
|
| titleValue | `string` | A string representation of the item's contents. Use this when the `title` is not readable. | - |
|
||||||
|
|||||||
@ -388,4 +388,17 @@ describe("Tabs", () => {
|
|||||||
|
|
||||||
expect(input).toHaveValue("23");
|
expect(input).toHaveValue("23");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("should forward ref to the tab item", () => {
|
||||||
|
const ref = React.createRef<HTMLButtonElement>();
|
||||||
|
|
||||||
|
render(
|
||||||
|
<Tabs aria-label="Tabs static test">
|
||||||
|
<Tab key="item1" tabRef={ref} title="Item 1">
|
||||||
|
<div>Content 1</div>
|
||||||
|
</Tab>
|
||||||
|
</Tabs>,
|
||||||
|
);
|
||||||
|
expect(ref.current).not.toBeNull();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import {BaseItem, ItemProps} from "@nextui-org/aria-utils";
|
import {BaseItem, ItemProps} from "@nextui-org/aria-utils";
|
||||||
import {ReactNode} from "react";
|
import {ReactNode, RefObject} from "react";
|
||||||
interface Props<T extends object = {}> extends Omit<ItemProps<"button", T>, "children" | "title"> {
|
interface Props<T extends object = {}> extends Omit<ItemProps<"button", T>, "children" | "title"> {
|
||||||
/**
|
/**
|
||||||
* The content of the component.
|
* The content of the component.
|
||||||
@ -18,6 +18,8 @@ interface Props<T extends object = {}> extends Omit<ItemProps<"button", T>, "chi
|
|||||||
isDisabled?: boolean;
|
isDisabled?: boolean;
|
||||||
/** Whether the tab selection should occur on press up instead of press down. */
|
/** Whether the tab selection should occur on press up instead of press down. */
|
||||||
shouldSelectOnPressUp?: boolean;
|
shouldSelectOnPressUp?: boolean;
|
||||||
|
/** A ref to the tab item. */
|
||||||
|
tabRef?: RefObject<HTMLButtonElement>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type TabItemProps<T extends object = {}> = Props<T>;
|
export type TabItemProps<T extends object = {}> = Props<T>;
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import type {TabItemProps as BaseTabItemProps} from "./base/tab-item-base";
|
import type {TabItemProps as BaseTabItemProps} from "./base/tab-item-base";
|
||||||
|
|
||||||
import {forwardRef} from "@nextui-org/system";
|
import {forwardRef} from "@nextui-org/system";
|
||||||
import {useDOMRef, filterDOMProps} from "@nextui-org/react-utils";
|
import {useDOMRef, filterDOMProps, mergeRefs} from "@nextui-org/react-utils";
|
||||||
import {clsx, dataAttr} from "@nextui-org/shared-utils";
|
import {clsx, dataAttr} from "@nextui-org/shared-utils";
|
||||||
import {chain, mergeProps} from "@react-aria/utils";
|
import {chain, mergeProps} from "@react-aria/utils";
|
||||||
import scrollIntoView from "scroll-into-view-if-needed";
|
import scrollIntoView from "scroll-into-view-if-needed";
|
||||||
@ -44,6 +44,7 @@ const Tab = forwardRef<"button", TabItemProps>((props, ref) => {
|
|||||||
disableCursorAnimation,
|
disableCursorAnimation,
|
||||||
shouldSelectOnPressUp,
|
shouldSelectOnPressUp,
|
||||||
onClick,
|
onClick,
|
||||||
|
tabRef,
|
||||||
...otherProps
|
...otherProps
|
||||||
} = props;
|
} = props;
|
||||||
|
|
||||||
@ -94,7 +95,7 @@ const Tab = forwardRef<"button", TabItemProps>((props, ref) => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<Component
|
<Component
|
||||||
ref={domRef}
|
ref={mergeRefs(domRef, tabRef)}
|
||||||
data-disabled={dataAttr(isDisabledItem)}
|
data-disabled={dataAttr(isDisabledItem)}
|
||||||
data-focus={dataAttr(isFocused)}
|
data-focus={dataAttr(isFocused)}
|
||||||
data-focus-visible={dataAttr(isFocusVisible)}
|
data-focus-visible={dataAttr(isFocusVisible)}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user