Fix: Show preview label only when Product Collection block is selected (#48795)

* Fix: Show preview label only when Product Collection block is selected

Updated the `ProductCollectionContent` component to display the preview label only when the Product Collection block itself is selected. Previously, the preview label was shown when either the block or any of its inner blocks were selected.

Key Changes:
- Removed the `isSelectedOrInnerBlockSelected` constant and its logic.
- Updated the condition to use `props.isSelected` directly to determine if the block is selected.

* Add changefile(s) from automation for the following project(s): woocommerce-blocks

* Update E2E tests

---------

Co-authored-by: github-actions <github-actions@github.com>
This commit is contained in:
Manish Menaria 2024-06-28 16:48:11 +05:30 committed by GitHub
parent d3aa89baa7
commit c70750b85a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 7 additions and 26 deletions

View File

@ -120,25 +120,6 @@ const ProductCollectionContent = ( {
[]
);
const isSelectedOrInnerBlockSelected = useSelect(
( select ) => {
const { getSelectedBlockClientId, hasSelectedInnerBlock } =
select( 'core/block-editor' );
// Check if the current block is selected.
const isSelected = getSelectedBlockClientId() === clientId;
// Check if any inner block of the current block is selected.
const isInnerBlockSelected = hasSelectedInnerBlock(
clientId,
true
);
return isSelected || isInnerBlockSelected;
},
[ clientId ]
);
/**
* If inherit is not a boolean, then we haven't set default attributes yet.
* We don't wanna render anything until default attributes are set.
@ -162,7 +143,7 @@ const ProductCollectionContent = ( {
return (
<div { ...blockProps }>
{ attributes.__privatePreviewState?.isPreview &&
isSelectedOrInnerBlockSelected && (
props.isSelected && (
<Button
variant="primary"
size="small"

View File

@ -1163,12 +1163,8 @@ test.describe( 'Product Collection', () => {
await editor.selectBlocks( otherBlockSelector );
await expect( previewButtonLocator ).toBeHidden();
// Preview button should be visible when any of inner block is selected
await editor.canvas
.getByLabel( BLOCK_LABELS.productTemplate )
.getByLabel( BLOCK_LABELS.productImage )
.first()
.click();
// Preview button should be visible again when the block is selected.
await pageObject.focusProductCollection();
await expect( previewButtonLocator ).toBeVisible();
} );
} );

View File

@ -0,0 +1,4 @@
Significance: minor
Type: update
Fix: Show preview label only when Product Collection block is selected