diff --git a/assets/js/base/components/tabs/index.js b/assets/js/base/components/tabs/index.js new file mode 100644 index 00000000000..330e872aff9 --- /dev/null +++ b/assets/js/base/components/tabs/index.js @@ -0,0 +1,108 @@ +/** + * External dependencies + */ +import { useState, useEffect } from '@wordpress/element'; +import { find, partial } from 'lodash'; +import { withInstanceId } from '@wordpress/compose'; +import classnames from 'classnames'; +import { __ } from '@wordpress/i18n'; + +/** + * Internal dependencies + */ +import './style.scss'; + +const TabButton = ( { + tabId, + onClick, + children, + selected, + ariaLabel, + ...rest +} ) => { + return ( + + ); +}; + +const Tabs = ( { + className, + onSelect = () => null, + tabs, + activeClass = 'is-active', + initialTabName, + instanceId, + ariaLabel = __( 'Tabbed Content', 'woo-gutenberg-products-block' ), + children, +} ) => { + const [ selected, setSelected ] = useState( '' ); + useEffect( () => { + if ( ! selected ) { + setSelected( + initialTabName || ( tabs.length > 0 ? tabs[ 0 ].name : null ) + ); + } + }, [ initialTabName, tabs ] ); + if ( ! selected ) { + return null; + } + const handleClick = ( tabKey ) => { + setSelected( tabKey ); + onSelect( tabKey ); + }; + const selectedTab = find( tabs, { name: selected } ); + const selectedId = `${ instanceId }-${ selectedTab.name }`; + return ( +