Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | import { useCallback, useEffect, useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import clsx from 'clsx';
import Controls from './controls';
import Tab from './tab';
const Tabs = props => {
const { attributes, setAttributes } = props;
const { monthlyDonation, annualDonation } = attributes;
const [ activeTab, setActiveTab ] = useState( 'one-time' );
const isTabActive = useCallback( tab => activeTab === tab, [ activeTab ] );
const tabs = {
'one-time': { title: __( 'One-Time', 'jetpack' ) },
...( monthlyDonation.show && { '1 month': { title: __( 'Monthly', 'jetpack' ) } } ),
...( annualDonation.show && { '1 year': { title: __( 'Yearly', 'jetpack' ) } } ),
};
// Activates the one-time tab if the interval of the current active tab is disabled.
useEffect( () => {
if ( ! monthlyDonation.show && isTabActive( '1 month' ) ) {
setActiveTab( 'one-time' );
}
if ( ! annualDonation.show && isTabActive( '1 year' ) ) {
setActiveTab( 'one-time' );
}
}, [ monthlyDonation, annualDonation, setActiveTab, isTabActive ] );
return (
<>
<div className="donations__container">
{ Object.keys( tabs ).length > 1 && (
<div className="donations__nav">
{ Object.entries( tabs ).map( ( [ interval, { title } ] ) => (
<div
role="button"
tabIndex={ 0 }
className={ clsx( 'donations__nav-item', {
'is-active': isTabActive( interval ),
} ) }
onClick={ () => setActiveTab( interval ) }
onKeyDown={ () => setActiveTab( interval ) }
key={ `jetpack-donations-nav-item-${ interval } ` }
>
{ title }
</div>
) ) }
</div>
) }
<div className="donations__content">
<Tab activeTab={ activeTab } attributes={ attributes } setAttributes={ setAttributes } />
</div>
</div>
<Controls { ...props } />
</>
);
};
export default Tabs;
|