Skip to content
Merged
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
27 changes: 18 additions & 9 deletions packages/components/src/components/tabs/tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
useCallback,
useEffect,
useId,
useMemo,
useRef,
useState,
} from "react";
Expand Down Expand Up @@ -69,18 +70,26 @@ const TabsRoot = ({
const tabRefs = useRef<(HTMLLIElement | null)[]>([]);
const uniqueId = useId();

const arrayChildren = Children.toArray(children).filter(
(child): child is ReactElement<TabsItemProps> => isValidElement(child)
const arrayChildren = useMemo(
() =>
Children.toArray(children).filter(
(child): child is ReactElement<TabsItemProps> => isValidElement(child)
),
[children]
);

const tabIds = arrayChildren.map((child, index) => {
if (child.props.id) {
return child.props.id;
}
const tabIds = useMemo(
() =>
arrayChildren.map((child, index) => {
if (child.props.id) {
return child.props.id;
}

const tabId = child.props.title ?? DEFAULT_TAB_TITLE;
return `${uniqueId}-${slugify(tabId)}-${index}`;
});
const tabId = child.props.title ?? DEFAULT_TAB_TITLE;
return `${uniqueId}-${slugify(tabId)}-${index}`;
}),
[arrayChildren, uniqueId]
);

const validDefaultTabIndex =
arrayChildren.length > 0
Expand Down