Skip to content

Commit

Permalink
Fix: InnerBlocks allowed blocks change with different sizes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Aug 25, 2023
1 parent f415a5d commit 57f56be
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ export default function useNestedSettingsUpdate(
[ clientId ]
);

// Memoize allowedBlocks and prioritisedInnerBlocks based on the contents
// of the arrays. Implementors often pass a new array on every render,
// Implementors often pass a new array on every render,
// and the contents of the arrays are just strings, so the entire array
// can be passed as dependencies.

// can be passed as dependencies but We need to include the length of the array,
// otherwise if the arrays change length but the first elements are equal the comparison,
// does not works as expected.
const _allowedBlocks = useMemo(
() => allowedBlocks,
// eslint-disable-next-line react-hooks/exhaustive-deps
allowedBlocks
[ allowedBlocks && allowedBlocks.length, ...allowedBlocks ]
);

const _prioritizedInserterBlocks = useMemo(
Expand Down
15 changes: 9 additions & 6 deletions packages/e2e-tests/plugins/inner-blocks-allowed-blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
};

const allowedBlocksWhenSingleEmptyChild = [ 'core/image', 'core/list' ];
const allowedBlocksWhenMultipleChildren = [ 'core/gallery', 'core/video' ];
const allowedBlocksWhenTwoChildren = [ 'core/gallery', 'core/video' ];
const allowedBlocksWhenTreeOrMoreChildren = [ 'core/gallery', 'core/video', 'core/list' ];

registerBlockType( 'test/allowed-blocks-dynamic', {
apiVersion: 3,
Expand All @@ -25,18 +26,20 @@
},
[ props.clientId ]
);

let allowedBlocks = allowedBlocksWhenSingleEmptyChild;
if ( props.numberOfChildren === 2 ) {
allowedBlocks = allowedBlocksWhenTwoChildren;
} else if( props.numberOfChildren > 2 ){
allowedBlocks = allowedBlocksWhenTreeOrMoreChildren;
}
return el(
'div',
{
...divProps,
'data-number-of-children': numberOfChildren,
},
el( InnerBlocks, {
allowedBlocks:
numberOfChildren < 2
? allowedBlocksWhenSingleEmptyChild
: allowedBlocksWhenMultipleChildren,
allowedBlocks,
} )
);
},
Expand Down
12 changes: 12 additions & 0 deletions test/e2e/specs/editor/plugins/inner-blocks-allowed-blocks.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,17 @@ test.describe( 'Allowed Blocks Setting on InnerBlocks', () => {
'Gallery',
'Video',
] );

await blockListBox.getByRole( 'option', { name: 'Gallery' } ).click();

await editor.clickBlockToolbarButton( 'Select Allowed Blocks Dynamic' );
await blockAppender.click();

// It should display a different allowed block list.
await expect( blockListBox.getByRole( 'option' ) ).toHaveText( [
'Gallery',
'List',
'Video',
] );
} );
} );

0 comments on commit 57f56be

Please sign in to comment.