diff --git a/src/core/components/tab/tabList.tsx b/src/core/components/tab/tabList.tsx index 4b7f676bd..146d32d19 100644 --- a/src/core/components/tab/tabList.tsx +++ b/src/core/components/tab/tabList.tsx @@ -1,4 +1,4 @@ -import {cloneElement, forwardRef, useCallback, useMemo, useState} from 'react' +import {cloneElement, forwardRef, useCallback, useState, Children, isValidElement} from 'react' import {styled} from 'styled-components' import {Inline, InlineProps} from '../../primitives' @@ -9,10 +9,6 @@ export interface TabListProps extends Omit { children: Array } -function _isReactElement(node: unknown): node is React.ReactElement { - return Boolean(node) -} - //Limits the width of tabs in tablist const CustomInline = styled(Inline)` & > div { @@ -33,22 +29,18 @@ export const TabList = forwardRef(function TabList( const {children: childrenProp, ...restProps} = props const [focusedIndex, setFocusedIndex] = useState(-1) - const children = useMemo(() => childrenProp.filter(_isReactElement), [childrenProp]) + const children: React.ReactElement[] = Children.toArray(childrenProp).filter(isValidElement) const tabs = children.map((child, childIndex) => cloneElement(child, { focused: focusedIndex === childIndex, key: childIndex, - onFocus: () => handleTabFocus(childIndex), + onFocus: () => setFocusedIndex(childIndex), }), ) const numTabs = tabs.length - const handleTabFocus = useCallback((tabIdx: number) => { - setFocusedIndex(tabIdx) - }, []) - const handleKeyDown = useCallback( (event: React.KeyboardEvent) => { if (event.key === 'ArrowLeft') {