// @flow import React, { useCallback, useEffect, useState } from 'react'; /** * The type of the React {@code Component} props of {@link Tabs}. */ type Props = { /** * Accessibility label for the tabs container. * */ accessibilityLabel: string, /** * Tabs information. */ tabs: Object }; /** * A React component that implements tabs. * * @returns {ReactElement} The component. */ const Tabs = ({ accessibilityLabel, tabs }: Props) => { const [ current, setCurrent ] = useState(0); const onClick = useCallback(index => event => { event.preventDefault(); setCurrent(index); }, []); const onKeyDown = useCallback(index => event => { let newIndex = null; if (event.key === 'ArrowLeft') { event.preventDefault(); newIndex = index === 0 ? tabs.length - 1 : index - 1; } if (event.key === 'ArrowRight') { event.preventDefault(); newIndex = index === tabs.length - 1 ? 0 : index + 1; } if (newIndex !== null) { setCurrent(newIndex); } }, [ tabs ]); useEffect(() => { // this test is needed to make sure the effect is triggered because of user actually changing tab if (document.activeElement?.getAttribute('role') === 'tab') { document.querySelector(`#${`${tabs[current].id}-tab`}`)?.focus(); } }, [ current, tabs ]); return (
{ tabs.length > 1 ? ( <>
{tabs.map((tab, index) => ( ))}
{tabs.map((tab, index) => (
{tab.content}
))} ) : ( <>

{accessibilityLabel}

{tabs[0].content}
) }
); }; export default Tabs;