From d5ef0f8c8daba30f9ee3210f0ee6781b2949cc75 Mon Sep 17 00:00:00 2001 From: ramonjd Date: Tue, 6 Sep 2022 11:07:25 +1000 Subject: [PATCH] Ensure that the inserted block is selected after a Group variation is selected. --- packages/block-library/src/group/edit.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/block-library/src/group/edit.js b/packages/block-library/src/group/edit.js index cacfb9de594a67..1104bc4c567dc8 100644 --- a/packages/block-library/src/group/edit.js +++ b/packages/block-library/src/group/edit.js @@ -2,7 +2,7 @@ * WordPress dependencies */ import { useSelect, useDispatch } from '@wordpress/data'; -import { useEffect } from '@wordpress/element'; +import { useEffect, useCallback } from '@wordpress/element'; import { InnerBlocks, useBlockProps, @@ -71,12 +71,13 @@ function GroupEditControls( { tagName, onSelectTagName } ) { * Display group variations if none is selected. * * @param {Object} props Component props. + * @param {string} props.clientId The block's clientId. * @param {string} props.name The block's name. * @param {Function} props.setAttributes Function to set block's attributes. * * @return {JSX.Element} The placeholder. */ -function Placeholder( { name, setAttributes } ) { +function Placeholder( { clientId, name, setAttributes } ) { const { blockType, defaultVariation, variations } = useSelect( ( select ) => { const { @@ -94,7 +95,12 @@ function Placeholder( { name, setAttributes } ) { [ name ] ); const blockProps = useBlockProps(); - + const { selectBlock } = useDispatch( blockEditorStore ); + // Ensure that the inserted block is selected after a Group variation is selected. + const updateSelection = useCallback( + ( newClientId ) => selectBlock( newClientId, -1 ), + [ selectBlock ] + ); return (
<__experimentalBlockVariationPicker @@ -103,6 +109,7 @@ function Placeholder( { name, setAttributes } ) { variations={ variations } onSelect={ ( nextVariation = defaultVariation ) => { setAttributes( nextVariation.attributes ); + updateSelection( clientId ); } } allowSkip /> @@ -175,7 +182,11 @@ function GroupEdit( { attributes, name, setAttributes, clientId } ) { } /> { showPlaceholder && ( - + ) } { layoutSupportEnabled && ! showPlaceholder && (