2020-01-06 22:28:09 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-08-05 09:26:00 +00:00
|
|
|
import { withInstanceId } from '@wordpress/compose';
|
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';
|
|
|
|
|
2023-01-12 12:16:32 +00:00
|
|
|
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 = ( {
|
2020-01-06 22:28:09 +00:00
|
|
|
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,
|
2023-01-12 12:16:32 +00:00
|
|
|
}: TabsProps ): JSX.Element | null => {
|
2020-05-22 19:32:41 +00:00
|
|
|
const initialTab = initialTabName
|
|
|
|
? { selectedId: `${ instanceId }-${ initialTabName }` }
|
|
|
|
: undefined;
|
|
|
|
const tabState = useTabState( initialTab );
|
2020-05-15 18:55:17 +00:00
|
|
|
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-10-05 13:25:40 +00:00
|
|
|
{ tabState.selectedId === `${ instanceId }-${ name }` &&
|
|
|
|
content }
|
2020-05-15 18:55:17 +00:00
|
|
|
</TabPanel>
|
|
|
|
) ) }
|
2020-01-06 22:28:09 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-01-12 12:16:32 +00:00
|
|
|
export default withInstanceId( __TabsWithoutInstanceId );
|