2020-01-06 22:28:09 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useState } from '@wordpress/element';
|
2020-03-26 12:39:54 +00:00
|
|
|
import { withInstanceId } from '@woocommerce/base-hocs/with-instance-id';
|
2020-01-06 22:28:09 +00:00
|
|
|
import classnames from 'classnames';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
2020-03-20 16:48:11 +00:00
|
|
|
import TabButton from './tab-button';
|
2020-01-06 22:28:09 +00:00
|
|
|
|
|
|
|
const Tabs = ( {
|
|
|
|
className,
|
|
|
|
onSelect = () => null,
|
|
|
|
tabs,
|
|
|
|
activeClass = 'is-active',
|
|
|
|
initialTabName,
|
2020-03-10 14:40:30 +00:00
|
|
|
instanceId,
|
2020-01-06 22:28:09 +00:00
|
|
|
ariaLabel = __( 'Tabbed Content', 'woo-gutenberg-products-block' ),
|
|
|
|
children,
|
2020-03-30 12:07:49 +00:00
|
|
|
id,
|
2020-01-06 22:28:09 +00:00
|
|
|
} ) => {
|
|
|
|
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' );
|
|
|
|
}
|
2020-03-10 14:40:30 +00:00
|
|
|
const selectedId = `${ instanceId }-${ selectedTab.name }`;
|
2020-01-06 22:28:09 +00:00
|
|
|
return (
|
2020-03-30 12:07:49 +00:00
|
|
|
<div
|
|
|
|
className={ classnames( 'wc-block-components-tabs', className ) }
|
|
|
|
id={ id }
|
|
|
|
>
|
2020-01-06 22:28:09 +00:00
|
|
|
<div
|
|
|
|
role="tablist"
|
|
|
|
aria-label={ ariaLabel }
|
2020-03-20 16:48:11 +00:00
|
|
|
className="wc-block-components-tabs__list"
|
2020-01-06 22:28:09 +00:00
|
|
|
>
|
|
|
|
{ tabs.map( ( tab ) => (
|
|
|
|
<TabButton
|
|
|
|
className={ classnames(
|
2020-03-20 16:48:11 +00:00
|
|
|
'wc-block-components-tabs__item',
|
2020-01-06 22:28:09 +00:00
|
|
|
tab.className,
|
|
|
|
{
|
|
|
|
[ activeClass ]: tab.name === selected,
|
|
|
|
}
|
|
|
|
) }
|
2020-03-10 14:40:30 +00:00
|
|
|
tabId={ `${ instanceId }-${ tab.name }` }
|
|
|
|
aria-controls={ `${ instanceId }-${ tab.name }-view` }
|
2020-01-06 22:28:09 +00:00
|
|
|
selected={ tab.name === selected }
|
|
|
|
key={ tab.name }
|
|
|
|
ariaLabel={ tab.ariaLabel || null }
|
|
|
|
onClick={ () => handleClick( tab.name ) }
|
|
|
|
>
|
2020-04-29 10:57:58 +00:00
|
|
|
{ tab.title }
|
2020-01-06 22:28:09 +00:00
|
|
|
</TabButton>
|
|
|
|
) ) }
|
|
|
|
</div>
|
|
|
|
{ selectedTab && (
|
|
|
|
<div
|
|
|
|
aria-labelledby={ selectedId }
|
|
|
|
role="tabpanel"
|
|
|
|
id={ `${ selectedId }-view` }
|
2020-03-20 16:48:11 +00:00
|
|
|
className="wc-block-components-tabs__content"
|
|
|
|
tabIndex={ 0 }
|
2020-01-06 22:28:09 +00:00
|
|
|
>
|
|
|
|
{ children( selected ) }
|
|
|
|
</div>
|
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-03-10 14:40:30 +00:00
|
|
|
export default withInstanceId( Tabs );
|