/** * External dependencies */ import { useState } from '@wordpress/element'; import { withInstanceId } from 'wordpress-compose'; import classnames from 'classnames'; import { __ } from '@wordpress/i18n'; /** * Internal dependencies */ import './style.scss'; import TabButton from './tab-button'; const Tabs = ( { className, onSelect = () => null, tabs, activeClass = 'is-active', initialTabName, instanceId, ariaLabel = __( 'Tabbed Content', 'woo-gutenberg-products-block' ), children, } ) => { const [ selected, setSelected ] = useState( initialTabName || ( tabs.length > 0 ? tabs[ 0 ].name : '' ) ); if ( ! selected ) { return null; } const handleClick = ( tabKey ) => { setSelected( tabKey ); onSelect( tabKey ); }; const selectedTab = tabs.find( ( tab ) => tab.name === selected ); if ( ! selectedTab ) { throw new Error( 'There is no available tab for the selected item' ); } const selectedId = `${ instanceId }-${ selectedTab.name }`; return (
{ tabs.map( ( tab ) => ( handleClick( tab.name ) } > { tab.title() } ) ) }
{ selectedTab && (
{ children( selected ) }
) }
); }; export default withInstanceId( Tabs );