Skip to content

Commit

Permalink
feat: add tab ref (#3974)
Browse files Browse the repository at this point in the history
* feat: add tab ref

* feat: add changeset
  • Loading branch information
winchesHe authored Nov 4, 2024
1 parent cb1b313 commit 7c2c9c4
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/smooth-mayflies-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nextui-org/tabs": patch
---

Add tab ref
1 change: 1 addition & 0 deletions apps/docs/content/docs/components/tabs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ You can customize the `Tabs` component by passing custom Tailwind CSS classes to

| Attribute | Type | Description | Default |
|-------------------------|-------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------|
| tabRef | `RefObject<HTMLButtonElement>`| A ref to the tab item. | - |
| children\* | `ReactNode` | The content 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. | - |
Expand Down
13 changes: 13 additions & 0 deletions packages/components/tabs/__tests__/tabs.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -388,4 +388,17 @@ describe("Tabs", () => {

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();
});
});
4 changes: 3 additions & 1 deletion packages/components/tabs/src/base/tab-item-base.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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"> {
/**
* The content of the component.
Expand All @@ -18,6 +18,8 @@ interface Props<T extends object = {}> extends Omit<ItemProps<"button", T>, "chi
isDisabled?: boolean;
/** Whether the tab selection should occur on press up instead of press down. */
shouldSelectOnPressUp?: boolean;
/** A ref to the tab item. */
tabRef?: RefObject<HTMLButtonElement>;
}

export type TabItemProps<T extends object = {}> = Props<T>;
Expand Down
5 changes: 3 additions & 2 deletions packages/components/tabs/src/tab.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {TabItemProps as BaseTabItemProps} from "./base/tab-item-base";

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 {chain, mergeProps} from "@react-aria/utils";
import scrollIntoView from "scroll-into-view-if-needed";
Expand Down Expand Up @@ -44,6 +44,7 @@ const Tab = forwardRef<"button", TabItemProps>((props, ref) => {
disableCursorAnimation,
shouldSelectOnPressUp,
onClick,
tabRef,
...otherProps
} = props;

Expand Down Expand Up @@ -94,7 +95,7 @@ const Tab = forwardRef<"button", TabItemProps>((props, ref) => {

return (
<Component
ref={domRef}
ref={mergeRefs(domRef, tabRef)}
data-disabled={dataAttr(isDisabledItem)}
data-focus={dataAttr(isFocused)}
data-focus-visible={dataAttr(isFocusVisible)}
Expand Down

0 comments on commit 7c2c9c4

Please sign in to comment.