Settings view component

This commit is contained in:
paul sealock 2024-06-10 14:02:35 +12:00
parent 32cc91b29d
commit 18119dcfea
6 changed files with 106 additions and 19 deletions

View File

@ -1,2 +1,3 @@
export { SettingsCheckbox } from './checkbox'; export { SettingsCheckbox } from './checkbox';
export { SettingsInput } from './input'; export { SettingsInput } from './input';
export { SideBar, SidebarContext } from './side-bar';

View File

@ -0,0 +1,14 @@
/**
* External dependencies
*/
import { createContext } from '@wordpress/element';
export const SidebarContext = createContext( null );
export const SideBar = () => {
return (
<div className="woocommerce-settings-side-bar">
<h2>Side Bar Content</h2>
</div>
);
};

View File

@ -4,7 +4,7 @@
import { getQuery, getNewPath } from '@woocommerce/navigation'; import { getQuery, getNewPath } from '@woocommerce/navigation';
import { Button } from '@wordpress/components'; import { Button } from '@wordpress/components';
import { Icon, chevronLeft } from '@wordpress/icons'; import { Icon, chevronLeft } from '@wordpress/icons';
import { useEffect } from '@wordpress/element'; import { useEffect, createContext, useState } from '@wordpress/element';
/** /**
* Internal dependencies * Internal dependencies
@ -16,13 +16,22 @@ import { possiblyRenderSettingsSlots } from './settings-slots';
import { registerTaxSettingsConflictErrorFill } from './conflict-error-slotfill'; import { registerTaxSettingsConflictErrorFill } from './conflict-error-slotfill';
import { registerPaymentsSettingsBannerFill } from '../payments/payments-settings-banner-slotfill'; import { registerPaymentsSettingsBannerFill } from '../payments/payments-settings-banner-slotfill';
import { registerSiteVisibilitySlotFill } from '../launch-your-store'; import { registerSiteVisibilitySlotFill } from '../launch-your-store';
import { registerExampleSettingsView } from './settings-view-example';
import { useFullScreen } from '~/utils'; import { useFullScreen } from '~/utils';
import { SidebarContext } from './components/side-bar';
import './style.scss'; import './style.scss';
const Settings = ( { params } ) => { const Settings = ( { params } ) => {
useFullScreen( [ 'woocommerce-settings' ] ); useFullScreen( [ 'woocommerce-settings' ] );
const settingsData = window.wcSettings?.admin?.settingsPages; const settingsData = window.wcSettings?.admin?.settingsPages;
const sections = settingsData[ params.page ]?.sections;
const { section } = getQuery(); const { section } = getQuery();
const contentData =
Array.isArray( sections ) && sections.length === 0
? {}
: sections[ section || '' ];
const [ hasSideBar, setHasSideBar ] = useState( false );
// Be sure to render Settings slots when the params change. // Be sure to render Settings slots when the params change.
useEffect( () => { useEffect( () => {
@ -34,18 +43,13 @@ const Settings = ( { params } ) => {
registerTaxSettingsConflictErrorFill(); registerTaxSettingsConflictErrorFill();
registerPaymentsSettingsBannerFill(); registerPaymentsSettingsBannerFill();
registerSiteVisibilitySlotFill(); registerSiteVisibilitySlotFill();
registerExampleSettingsView();
}, [] ); }, [] );
if ( ! settingsData ) { if ( ! settingsData ) {
return <div>Error getting data</div>; return <div>Error getting data</div>;
} }
const sections = settingsData[ params.page ]?.sections;
const title = settingsData[ params.page ]?.label; const title = settingsData[ params.page ]?.label;
const contentData =
Array.isArray( sections ) && sections.length === 0
? {}
: sections[ section || '' ];
return ( return (
<> <>
@ -66,7 +70,9 @@ const Settings = ( { params } ) => {
section={ section } section={ section }
> >
<div className="woocommerce-settings-layout-main"> <div className="woocommerce-settings-layout-main">
<SidebarContext.Provider value={ setHasSideBar }>
<Content data={ contentData } /> <Content data={ contentData } />
</SidebarContext.Provider>
</div> </div>
</SectionNav> </SectionNav>
</div> </div>

View File

@ -1,16 +1,23 @@
/** /**
* External dependencies * External dependencies
*/ */
import { render, createRoot } from '@wordpress/element'; import { render, createRoot, useContext } from '@wordpress/element';
import { createSlotFill, SlotFillProvider } from '@wordpress/components'; import { createSlotFill, SlotFillProvider } from '@wordpress/components';
import { PluginArea } from '@wordpress/plugins'; import { PluginArea } from '@wordpress/plugins';
/**
* Internal dependencies
*/
import { SideBar, SidebarContext } from './components/side-bar';
export const SETTINGS_SLOT_FILL_CONSTANT = export const SETTINGS_SLOT_FILL_CONSTANT =
'__EXPERIMENTAL__WcAdminSettingsSlots'; '__EXPERIMENTAL__WcAdminSettingsSlots';
const { Slot } = createSlotFill( SETTINGS_SLOT_FILL_CONSTANT ); const { Slot } = createSlotFill( SETTINGS_SLOT_FILL_CONSTANT );
export const possiblyRenderSettingsSlots = () => { export const possiblyRenderSettingsSlots = () => {
//@TODO We need to automatically register these based on the settings data so
// this way extensions don't need to add to this configuration.
const slots = [ const slots = [
{ {
id: 'wc_payments_settings_slotfill', id: 'wc_payments_settings_slotfill',
@ -18,6 +25,10 @@ export const possiblyRenderSettingsSlots = () => {
}, },
{ id: 'wc_tax_settings_slotfill', scope: 'woocommerce-tax-settings' }, { id: 'wc_tax_settings_slotfill', scope: 'woocommerce-tax-settings' },
{ id: 'wc_settings_slotfill', scope: 'woocommerce-settings' }, { id: 'wc_settings_slotfill', scope: 'woocommerce-settings' },
{
id: 'wc_site_visibility_settings_view',
scope: 'woocommerce-settings',
},
{ {
id: 'wc_settings_site_visibility_slotfill', id: 'wc_settings_site_visibility_slotfill',
scope: 'woocommerce-site-visibility-settings', scope: 'woocommerce-site-visibility-settings',
@ -34,7 +45,7 @@ export const possiblyRenderSettingsSlots = () => {
const slotFill = ( const slotFill = (
<> <>
<SlotFillProvider> <SlotFillProvider>
<Slot /> <Slot fillProps={ { SideBar } } />
<PluginArea scope={ slot.scope } /> <PluginArea scope={ slot.scope } />
</SlotFillProvider> </SlotFillProvider>
</> </>
@ -44,15 +55,7 @@ export const possiblyRenderSettingsSlots = () => {
const root = createRoot( slotDomElement ); const root = createRoot( slotDomElement );
root.render( slotFill ); root.render( slotFill );
} else { } else {
render( render( slotFill, slotDomElement );
<>
<SlotFillProvider>
<Slot />
<PluginArea scope={ slot.scope } />
</SlotFillProvider>
</>,
slotDomElement
);
} }
} ); } );
}; };

View File

@ -0,0 +1,34 @@
/**
* External dependencies
*/
import { createSlotFill } from '@wordpress/components';
import { registerPlugin } from '@wordpress/plugins';
/**
* Internal dependencies
*/
import { SETTINGS_SLOT_FILL_CONSTANT } from './settings-slots';
const { Fill } = createSlotFill( SETTINGS_SLOT_FILL_CONSTANT );
const ExampleSettingsViewSlotFill = () => {
const style = { margin: '36px 0px' };
return (
<Fill>
{ ( { SideBar } ) => (
<div style={ style }>
<h1>Example Settings View</h1>
<p>This is the main content using a SlotFill</p>
{ <SideBar /> }
</div>
) }
</Fill>
);
};
export const registerExampleSettingsView = () => {
registerPlugin( 'woocommerce-exampple-settings-view-slotfill', {
scope: 'woocommerce-settings',
render: ExampleSettingsViewSlotFill,
} );
};

View File

@ -29,6 +29,18 @@ class WC_Settings_Site_Visibility extends WC_Settings_Page {
parent::__construct(); parent::__construct();
} }
/**
* Get own sections.
*
* @return array
*/
protected function get_own_sections() {
return array(
'' => __( 'General', 'woocommerce' ),
'custom_view' => __( 'Custom View', 'woocommerce' ),
);
}
/** /**
* Get settings for the default section. * Get settings for the default section.
@ -46,6 +58,23 @@ class WC_Settings_Site_Visibility extends WC_Settings_Page {
return $settings; return $settings;
} }
/**
* Get settings for the default section.
*
* @return array
*/
protected function get_settings_for_custom_view_section() {
$settings =
array(
array(
'id' => 'wc_site_visibility_settings_view',
'type' => 'slotfill_placeholder',
),
);
return $settings;
}
} }