Skip to content

Commit

Permalink
Emit the id of an activated Tab Panel
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentSmedinga committed Feb 4, 2025
1 parent 146a873 commit b4b993e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/react/src/Tabs/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ import { useKeyboardFocus } from '../common/useKeyboardFocus'
export type TabsProps = {
/** The number of the active tab. Corresponds to its `tab` value. */
activeTab?: number
/* Provides the id of the activated Tab Panel. */
onChange?: (tabId: number) => void
} & PropsWithChildren<HTMLAttributes<HTMLDivElement>>

const TabsRoot = forwardRef(
({ activeTab, children, className, ...restProps }: TabsProps, ref: ForwardedRef<HTMLDivElement>) => {
({ activeTab, children, className, onChange, ...restProps }: TabsProps, ref: ForwardedRef<HTMLDivElement>) => {
const tabsId = useId()
const innerRef = useRef<HTMLDivElement>(null)
const [activeTabId, setActiveTabId] = useState(0)
Expand All @@ -43,6 +45,7 @@ const TabsRoot = forwardRef(

const updateTab = (tab: number) => {
setActiveTabId(tab)
onChange?.(tab)
}

// Use a passed ref if it's there, otherwise use innerRef
Expand Down
1 change: 1 addition & 0 deletions storybook/src/components/Tabs/Tabs.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const meta = {
component: Tabs,
args: {
children: defaultTabs,
onChange: (tabId) => console.log(`Tab ${tabId} was selected.`),
},
argTypes: {
activeTab: {
Expand Down

0 comments on commit b4b993e

Please sign in to comment.