2020-01-06 22:28:09 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
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';
|
2020-05-15 18:55:17 +00:00
|
|
|
import { useTabState, Tab, TabList, TabPanel } from 'reakit/Tab';
|
2020-01-06 22:28:09 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
const Tabs = ( {
|
|
|
|
className,
|
|
|
|
onSelect = () => null,
|
|
|
|
tabs,
|
|
|
|
activeClass = 'is-active',
|
|
|
|
initialTabName,
|
|
|
|
ariaLabel = __( 'Tabbed Content', 'woo-gutenberg-products-block' ),
|
2020-05-15 18:55:17 +00:00
|
|
|
instanceId,
|
2020-03-30 12:07:49 +00:00
|
|
|
id,
|
2020-01-06 22:28:09 +00:00
|
|
|
} ) => {
|
2020-05-15 18:55:17 +00:00
|
|
|
const initialTab =
|
|
|
|
( initialTabName || !! tabs.length ) &&
|
|
|
|
`${ instanceId }-${ initialTabName ? initialTabName : tabs[ 0 ].name }`;
|
|
|
|
const tabState = useTabState( {
|
|
|
|
selectedId: initialTab,
|
|
|
|
} );
|
|
|
|
if ( tabs.length === 0 ) {
|
2020-01-06 22:28:09 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
2020-05-15 18:55:17 +00:00
|
|
|
<div className={ classnames( 'wc-block-components-tabs', className ) }>
|
|
|
|
<TabList
|
|
|
|
{ ...tabState }
|
|
|
|
id={ id }
|
|
|
|
className={ 'wc-block-components-tabs__list' }
|
2020-01-06 22:28:09 +00:00
|
|
|
aria-label={ ariaLabel }
|
|
|
|
>
|
2020-05-15 18:55:17 +00:00
|
|
|
{ tabs.map( ( { name, title, ariaLabel: tabAriaLabel } ) => (
|
|
|
|
<Tab
|
|
|
|
{ ...tabState }
|
|
|
|
id={ `${ instanceId }-${ name }` }
|
|
|
|
manual={ true }
|
2020-01-06 22:28:09 +00:00
|
|
|
className={ classnames(
|
2020-03-20 16:48:11 +00:00
|
|
|
'wc-block-components-tabs__item',
|
2020-01-06 22:28:09 +00:00
|
|
|
{
|
2020-05-15 18:55:17 +00:00
|
|
|
[ activeClass ]:
|
|
|
|
// reakit uses the ID as the selectedId
|
|
|
|
`${ instanceId }-${ name }` ===
|
|
|
|
tabState.selectedId,
|
2020-01-06 22:28:09 +00:00
|
|
|
}
|
|
|
|
) }
|
2020-05-15 18:55:17 +00:00
|
|
|
onClick={ () => onSelect( name ) }
|
|
|
|
type="button"
|
|
|
|
key={ name }
|
|
|
|
aria-label={ tabAriaLabel }
|
2020-01-06 22:28:09 +00:00
|
|
|
>
|
2020-05-15 18:55:17 +00:00
|
|
|
<span className="wc-block-components-tabs__item-content">
|
|
|
|
{ title }
|
|
|
|
</span>
|
|
|
|
</Tab>
|
2020-01-06 22:28:09 +00:00
|
|
|
) ) }
|
2020-05-15 18:55:17 +00:00
|
|
|
</TabList>
|
|
|
|
|
|
|
|
{ tabs.map( ( { name, content } ) => (
|
|
|
|
<TabPanel
|
|
|
|
{ ...tabState }
|
|
|
|
key={ name }
|
|
|
|
id={ `${ instanceId }-${ name }-view` }
|
|
|
|
tabId={ `${ instanceId }-${ name }` }
|
2020-03-20 16:48:11 +00:00
|
|
|
className="wc-block-components-tabs__content"
|
2020-01-06 22:28:09 +00:00
|
|
|
>
|
2020-05-15 18:55:17 +00:00
|
|
|
{ content }
|
|
|
|
</TabPanel>
|
|
|
|
) ) }
|
2020-01-06 22:28:09 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-03-10 14:40:30 +00:00
|
|
|
export default withInstanceId( Tabs );
|