Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Editor: Use hooks instead of HoC in 'BlockModeToggle' #64460

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import { __ } from '@wordpress/i18n';
import { MenuItem } from '@wordpress/components';
import { getBlockType, hasBlockSupport } from '@wordpress/blocks';
import { withSelect, withDispatch } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { useDispatch, useSelect } from '@wordpress/data';

/**
* Internal dependencies
Expand All @@ -14,13 +13,23 @@ import { store as blockEditorStore } from '../../store';

const noop = () => {};

export function BlockModeToggle( {
blockType,
mode,
onToggleMode,
small = false,
isCodeEditingEnabled = true,
} ) {
export default function BlockModeToggle( { clientId, onToggle = noop } ) {
const { blockType, mode, isCodeEditingEnabled } = useSelect(
( select ) => {
const { getBlock, getBlockMode, getSettings } =
select( blockEditorStore );
const block = getBlock( clientId );

return {
mode: getBlockMode( clientId ),
blockType: block ? getBlockType( block.name ) : null,
isCodeEditingEnabled: getSettings().codeEditingEnabled,
};
},
[ clientId ]
);
const { toggleBlockMode } = useDispatch( blockEditorStore );

if (
! blockType ||
! hasBlockSupport( blockType, 'html', true ) ||
Expand All @@ -32,26 +41,14 @@ export function BlockModeToggle( {
const label =
mode === 'visual' ? __( 'Edit as HTML' ) : __( 'Edit visually' );

return <MenuItem onClick={ onToggleMode }>{ ! small && label }</MenuItem>;
return (
<MenuItem
onClick={ () => {
toggleBlockMode( clientId );
onToggle();
} }
>
{ label }
</MenuItem>
);
}

export default compose( [
withSelect( ( select, { clientId } ) => {
const { getBlock, getBlockMode, getSettings } =
select( blockEditorStore );
const block = getBlock( clientId );
const isCodeEditingEnabled = getSettings().codeEditingEnabled;

return {
mode: getBlockMode( clientId ),
blockType: block ? getBlockType( block.name ) : null,
isCodeEditingEnabled,
};
} ),
withDispatch( ( dispatch, { onToggle = noop, clientId } ) => ( {
onToggleMode() {
dispatch( blockEditorStore ).toggleBlockMode( clientId );
onToggle();
},
} ) ),
] )( BlockModeToggle );
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,59 @@
*/
import { render, screen } from '@testing-library/react';

/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import { BlockModeToggle } from '../block-mode-toggle';
import BlockModeToggle from '../block-mode-toggle';

jest.mock( '@wordpress/data/src/components/use-select', () => jest.fn() );

function setupUseSelectMock( mode, blockType, codeEditingEnabled = true ) {
useSelect.mockImplementation( () => {
return {
mode,
blockType,
isCodeEditingEnabled: codeEditingEnabled,
};
} );
}

describe( 'BlockModeToggle', () => {
it( "should not render the HTML mode button if the block doesn't support it", () => {
render(
<BlockModeToggle blockType={ { supports: { html: false } } } />
);
setupUseSelectMock( undefined, { supports: { html: false } } );
render( <BlockModeToggle /> );

expect(
screen.queryByRole( 'menuitem', { name: 'Edit as HTML' } )
).not.toBeInTheDocument();
} );

it( 'should render the HTML mode button', () => {
render(
<BlockModeToggle
blockType={ { supports: { html: true } } }
mode="visual"
/>
);
setupUseSelectMock( 'visual', { supports: { html: true } } );
render( <BlockModeToggle /> );

expect(
screen.getByRole( 'menuitem', { name: 'Edit as HTML' } )
).toBeVisible();
} );

it( 'should render the Visual mode button', () => {
render(
<BlockModeToggle
blockType={ { supports: { html: true } } }
mode="html"
/>
);
setupUseSelectMock( 'html', { supports: { html: true } } );
render( <BlockModeToggle /> );

expect(
screen.getByRole( 'menuitem', { name: 'Edit visually' } )
).toBeVisible();
} );

it( 'should not render the Visual mode button if code editing is disabled', () => {
render(
<BlockModeToggle
blockType={ { supports: { html: true } } }
mode="html"
isCodeEditingEnabled={ false }
/>
);
setupUseSelectMock( 'html', { supports: { html: true } }, false );
render( <BlockModeToggle /> );

expect(
screen.queryByRole( 'menuitem', { name: 'Edit visually' } )
Expand Down
Loading