From 1d0eecb53fc3b91b1ad7bd51a368e088243fa8eb Mon Sep 17 00:00:00 2001 From: Hiroshi Urabe Date: Fri, 5 Jun 2020 21:48:47 +0900 Subject: [PATCH] useSelect for block-title . (#22910) --- docs/contributors/coding-guidelines.md | 6 ++-- packages/block-editor/README.md | 18 +++++++++++- .../src/components/block-title/index.js | 28 ++++++++++--------- .../src/components/block-title/test/index.js | 23 +++++++++++++-- 4 files changed, 55 insertions(+), 20 deletions(-) diff --git a/docs/contributors/coding-guidelines.md b/docs/contributors/coding-guidelines.md index c69b7d6e419098..5fea9211b10e6a 100644 --- a/docs/contributors/coding-guidelines.md +++ b/docs/contributors/coding-guidelines.md @@ -452,11 +452,11 @@ Documenting a function component should be treated the same as any other functio * @example * * ```jsx - * + * * ``` * - * @param {Object} props Component props. - * @param {?string} props.name Block name. + * @param {Object} props + * @param {string} props.clientId Client ID of block. * * @return {?string} Block title. */ diff --git a/packages/block-editor/README.md b/packages/block-editor/README.md index fe0657a95bf74a..c7df34922083cc 100644 --- a/packages/block-editor/README.md +++ b/packages/block-editor/README.md @@ -186,7 +186,23 @@ _Related_ # **BlockTitle** -Undocumented declaration. +Renders the block's configured title as a string, or empty if the title +cannot be determined. + +_Usage_ + +```jsx + +``` + +_Parameters_ + +- _props_ `Object`: +- _props.clientId_ `string`: Client ID of block. + +_Returns_ + +- `?string`: Block title. # **BlockToolbar** diff --git a/packages/block-editor/src/components/block-title/index.js b/packages/block-editor/src/components/block-title/index.js index 6834df11498922..0ccb00c5311a98 100644 --- a/packages/block-editor/src/components/block-title/index.js +++ b/packages/block-editor/src/components/block-title/index.js @@ -1,7 +1,7 @@ /** * WordPress dependencies */ -import { withSelect } from '@wordpress/data'; +import { useSelect } from '@wordpress/data'; import { getBlockType } from '@wordpress/blocks'; /** @@ -14,12 +14,23 @@ import { getBlockType } from '@wordpress/blocks'; * * ``` * - * @param {Object} props - * @param {?string} props.name Block name. + * @param {Object} props + * @param {string} props.clientId Client ID of block. * * @return {?string} Block title. */ -export function BlockTitle( { name } ) { +export default function BlockTitle( { clientId } ) { + const name = useSelect( + ( select ) => { + if ( ! clientId ) { + return null; + } + const { getBlockName } = select( 'core/block-editor' ); + return getBlockName( clientId ); + }, + [ clientId ] + ); + if ( ! name ) { return null; } @@ -31,12 +42,3 @@ export function BlockTitle( { name } ) { return blockType.title; } - -export default withSelect( ( select, ownProps ) => { - const { getBlockName } = select( 'core/block-editor' ); - const { clientId } = ownProps; - - return { - name: getBlockName( clientId ), - }; -} )( BlockTitle ); diff --git a/packages/block-editor/src/components/block-title/test/index.js b/packages/block-editor/src/components/block-title/test/index.js index 4306ae6247b93b..d0479223b4c417 100644 --- a/packages/block-editor/src/components/block-title/test/index.js +++ b/packages/block-editor/src/components/block-title/test/index.js @@ -3,10 +3,15 @@ */ import { shallow } from 'enzyme'; +/** + * WordPress dependencies + */ +import { useSelect } from '@wordpress/data'; + /** * Internal dependencies */ -import { BlockTitle } from '../'; +import BlockTitle from '../'; jest.mock( '@wordpress/blocks', () => { return { @@ -22,6 +27,12 @@ jest.mock( '@wordpress/blocks', () => { }; } ); +jest.mock( '@wordpress/data/src/components/use-select', () => { + // This allows us to tweak the returned value on each test + const mock = jest.fn(); + return mock; +} ); + describe( 'BlockTitle', () => { it( 'renders nothing if name is falsey', () => { const wrapper = shallow( ); @@ -30,13 +41,19 @@ describe( 'BlockTitle', () => { } ); it( 'renders nothing if block type does not exist', () => { - const wrapper = shallow( ); + useSelect.mockImplementation( () => 'name-not-exists' ); + const wrapper = shallow( + + ); expect( wrapper.type() ).toBe( null ); } ); it( 'renders title if block type exists', () => { - const wrapper = shallow( ); + useSelect.mockImplementation( () => 'name-exists' ); + const wrapper = shallow( + + ); expect( wrapper.text() ).toBe( 'Block Title' ); } );