import { useState } from '@wordpress/element'; import FieldSelector from '../FieldSelector'; import MetaKeySelector from '../MetaKeySelector'; import TaxonomySelector from '../TaxonomySelector'; import FilterSelector from '../FilterSelector'; import OptionsSelector from '../OptionsSelector'; import JoinSelector from '../JoinSelector'; const TAB_COMPONENTS = { 'field-selector': function FieldTab( { block, definition, updateConfig } ) { return ( updateConfig( { fields } ) } /> ); }, 'meta-selector': function MetaTab( { block, updateConfig, tabConfig } ) { return ( updateConfig( { meta_keys } ) } /> ); }, 'taxonomy-selector': function TaxonomyTab( { block, updateConfig, tabConfig } ) { return ( updateConfig( { taxonomies } ) } /> ); }, 'filter-selector': function FilterTab( { block, definition, updateConfig } ) { return ( updateConfig( { filters } ) } /> ); }, 'options-selector': function OptionsTab( { block, updateConfig } ) { return ( updateConfig( { options } ) } /> ); }, 'join-selector': function JoinTab( { block, updateConfig, tabConfig } ) { return ( updateConfig( { joins } ) } /> ); }, }; export default function GenericConfigurator( { block, definition, dispatch, tabs } ) { const [ activeTab, setActiveTab ] = useState( tabs[ 0 ]?.key || 'fields' ); function updateConfig( partial ) { dispatch( { type: 'UPDATE_BLOCK_CONFIG', payload: { id: block.id, config: partial }, } ); } const activeTabConfig = tabs.find( ( t ) => t.key === activeTab ); const TabComponent = activeTabConfig ? TAB_COMPONENTS[ activeTabConfig.type ] : null; return (
{ tabs.map( ( tab ) => ( ) ) }
{ TabComponent && ( ) }
); }