/** * External dependencies */ import { withInstanceId } from '@wordpress/compose'; import classnames from 'classnames'; import { __ } from '@wordpress/i18n'; import { useTabState, Tab, TabList, TabPanel } from 'reakit/Tab'; /** * Internal dependencies */ import './style.scss'; export interface TabsProps { /** * Component wrapper classname */ className?: string; /** * Event handler triggered when a tab is selected */ onSelect?: ( tabName: string ) => void; /** * Array of tab objects */ tabs: Array< { name: string; title: string; content: JSX.Element; ariaLabel?: string; } >; /** * Classname to be applied to the active tab */ activeClass?: string; /** * Name of the tab to be selected by default */ initialTabName?: string | undefined; /** * Aria label for the tablist */ ariaLabel?: string; /** * Instance ID for the component */ instanceId: number; /** * ID for the component */ id?: string; } /** * Exporting the component for Storybook. Use the default export instead. */ export const __TabsWithoutInstanceId = ( { className, onSelect = () => null, tabs, activeClass = 'is-active', initialTabName, ariaLabel = __( 'Tabbed Content', 'woo-gutenberg-products-block' ), instanceId, id, }: TabsProps ): JSX.Element | null => { const initialTab = initialTabName ? { selectedId: `${ instanceId }-${ initialTabName }` } : undefined; const tabState = useTabState( initialTab ); if ( tabs.length === 0 ) { return null; } return (
{ tabs.map( ( { name, title, ariaLabel: tabAriaLabel } ) => ( onSelect( name ) } type="button" key={ name } aria-label={ tabAriaLabel } > { title } ) ) } { tabs.map( ( { name, content } ) => ( { tabState.selectedId === `${ instanceId }-${ name }` && content } ) ) }
); }; export default withInstanceId( __TabsWithoutInstanceId );