Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Switch to parent block when changing views
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley committed Sep 23, 2021
1 parent 41eb305 commit a9ee915
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
31 changes: 18 additions & 13 deletions assets/js/blocks/cart-checkout/cart-i2/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,24 +141,29 @@ export const Edit = ( {
className,
attributes,
setAttributes,
clientId,
}: {
className: string;
attributes: Attributes;
setAttributes: ( attributes: Record< string, unknown > ) => undefined;
clientId: string;
} ): JSX.Element => {
const { currentView, component: ViewSwitcherComponent } = useViewSwitcher( [
{
view: 'emptyCart',
label: __( 'Empty Cart', 'woo-gutenberg-products-block' ),
icon: <Icon srcElement={ removeCart } />,
},
{
view: 'filledCart',
label: __( 'Filled Cart', 'woo-gutenberg-products-block' ),
icon: <Icon srcElement={ filledCart } />,
default: true,
},
] );
const { currentView, component: ViewSwitcherComponent } = useViewSwitcher(
clientId,
[
{
view: 'emptyCart',
label: __( 'Empty Cart', 'woo-gutenberg-products-block' ),
icon: <Icon srcElement={ removeCart } />,
},
{
view: 'filledCart',
label: __( 'Filled Cart', 'woo-gutenberg-products-block' ),
icon: <Icon srcElement={ filledCart } />,
default: true,
},
]
);
const cartClassName = classnames( {
'has-dark-controls': attributes.hasDarkControls,
} );
Expand Down
8 changes: 7 additions & 1 deletion assets/js/blocks/cart-checkout/cart-i2/use-view-switcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { __ } from '@wordpress/i18n';
import { useState } from '@wordpress/element';
import { useDispatch } from '@wordpress/data';
import { Toolbar, ToolbarDropdownMenu } from '@wordpress/components';
import { Icon, eye } from '@woocommerce/icons';

Expand All @@ -14,6 +15,7 @@ interface View {
}

export const useViewSwitcher = (
clientId: string,
views: View[]
): {
currentView: string;
Expand All @@ -22,6 +24,7 @@ export const useViewSwitcher = (
const initialView =
views?.find( ( view ) => view.default === true ) || views[ 0 ];
const [ currentView, setCurrentView ] = useState( initialView );
const { selectBlock } = useDispatch( 'core/block-editor' );

const ViewSwitcherComponent = () => (
<Toolbar>
Expand All @@ -34,7 +37,10 @@ export const useViewSwitcher = (
controls={ views.map( ( view ) => ( {
...view,
title: view.label,
onClick: () => setCurrentView( view ),
onClick: () => {
setCurrentView( view );
selectBlock( clientId );
},
} ) ) }
/>
</Toolbar>
Expand Down

0 comments on commit a9ee915

Please sign in to comment.