woocommerce-paypal-payments/modules/ppcp-settings/resources/js/Components/ReusableComponents/TabBar.js

48 lines
1.2 KiB
JavaScript
Raw Normal View History

2025-01-28 08:59:06 +01:00
import { useCallback, useEffect } from '@wordpress/element';
// TODO: Migrate to Tabs (TabPanel v2) once its API is publicly available, as it provides programmatic tab switching support: https://github.com/WordPress/gutenberg/issues/52997
2024-10-24 13:54:50 +02:00
import { TabPanel } from '@wordpress/components';
2025-01-28 08:59:06 +01:00
import { updateQueryString } from '../../utils/navigation';
const TabBar = ( { tabs, activePanel, setActivePanel } ) => {
const isValidTab = ( tabsList, checkTab ) => {
return tabsList.some( ( tab ) => tab.name === checkTab );
};
const updateActivePanel = useCallback(
( tabName ) => {
if ( isValidTab( tabs, tabName ) ) {
setActivePanel( tabName );
} else {
console.warn( `Invalid tab name: ${ tabName }` );
}
},
2025-01-28 08:59:06 +01:00
[ tabs, setActivePanel ]
);
2024-10-28 15:58:40 +01:00
useEffect( () => {
updateQueryString( { panel: activePanel } );
2024-10-28 15:58:40 +01:00
}, [ activePanel ] );
2024-10-24 13:54:50 +02:00
return (
<TabPanel
className={ `ppcp-r-tabs ${ activePanel }` }
initialTabName={ activePanel }
onSelect={ updateActivePanel }
tabs={ tabs }
orientation="horizontal"
selectOnMove={ false }
2024-10-24 13:54:50 +02:00
>
{ ( tab ) => (
<div
className={ `ppcp-r-tabpanel-content ppcp-r-tabpanel-${ tab.name }` }
>
{ tab.render ? tab.render() : '' }
</div>
) }
2024-10-24 13:54:50 +02:00
</TabPanel>
);
};
export default TabBar;