From 970bca59164d93df16a193188a22e3cc1bef3f8f Mon Sep 17 00:00:00 2001 From: Andrei Draganescu Date: Mon, 28 Sep 2020 16:34:01 +0300 Subject: [PATCH] testing hook to avoid add/remove filter on every render --- .../src/edit-in-place-control/index.js | 6 +- .../src/components/layout/index.js | 4 +- .../layout/use-navigation-block-with-name.js | 71 ++++++++++--------- 3 files changed, 42 insertions(+), 39 deletions(-) diff --git a/packages/components/src/edit-in-place-control/index.js b/packages/components/src/edit-in-place-control/index.js index c286a051ee36a..0a86a92ada3f2 100644 --- a/packages/components/src/edit-in-place-control/index.js +++ b/packages/components/src/edit-in-place-control/index.js @@ -14,7 +14,7 @@ import { ENTER, ESCAPE } from '@wordpress/keycodes'; */ import Button from '../button'; -function EditInPlaceControl( { label = '', onClick = noop, onChange = noop } ) { +function EditInPlaceControl( { label = '', onClick = noop, onUpdate = noop } ) { const [ edit, setEdit ] = useState( false ); const [ value, setValue ] = useState( label ); const prevValue = useRef(); @@ -56,7 +56,7 @@ function EditInPlaceControl( { label = '', onClick = noop, onChange = noop } ) { } } onBlur={ () => { setEdit( false ); - onChange( value ); + onUpdate( value ); } } onKeyDown={ ( event ) => { if ( @@ -69,7 +69,7 @@ function EditInPlaceControl( { label = '', onClick = noop, onChange = noop } ) { setValue( prevValue.current ); } else { setEdit( ! edit ); - onChange( value ); + onUpdate( value ); } } } } diff --git a/packages/edit-navigation/src/components/layout/index.js b/packages/edit-navigation/src/components/layout/index.js index d517f0b26223a..9f9fa185a43fd 100644 --- a/packages/edit-navigation/src/components/layout/index.js +++ b/packages/edit-navigation/src/components/layout/index.js @@ -42,9 +42,7 @@ export default function Layout( { blockEditorSettings } ) { useMenuNotifications( selectedMenuId ); - useNavigationBlockWithName( { - menuId: selectedMenuId, - } ); + useNavigationBlockWithName( selectedMenuId ); return ( diff --git a/packages/edit-navigation/src/components/layout/use-navigation-block-with-name.js b/packages/edit-navigation/src/components/layout/use-navigation-block-with-name.js index 838c88084c823..c74c8addd3e4d 100644 --- a/packages/edit-navigation/src/components/layout/use-navigation-block-with-name.js +++ b/packages/edit-navigation/src/components/layout/use-navigation-block-with-name.js @@ -1,6 +1,7 @@ /** * WordPress dependencies */ +import { useEffect } from '@wordpress/element'; import { useSelect, useDispatch } from '@wordpress/data'; import { createHigherOrderComponent } from '@wordpress/compose'; import { BlockControls } from '@wordpress/block-editor'; @@ -10,45 +11,49 @@ import { ToolbarGroup, } from '@wordpress/components'; -export default function useNavigationBlockWithName( { menuId } ) { +export default function useNavigationBlockWithName( menuId ) { const menu = useSelect( ( select ) => select( 'core' ).getMenu( menuId ), [ menuId, ] ); const { saveMenu } = useDispatch( 'core' ); - removeFilter( 'editor.BlockEdit', 'core/edit-navigation/with-menu-name' ); - - const withMenuName = createHigherOrderComponent( - ( BlockEdit ) => ( props ) => { - if ( props.name !== 'core/navigation' ) { - return ; - } - return ( - <> - - - - { - saveMenu( { - ...menu, - name: value || '(untitled menu)', - } ); - } } - /> - - - + useEffect( () => { + if ( menu ) { + const withMenuName = createHigherOrderComponent( + ( BlockEdit ) => ( props ) => { + if ( props.name !== 'core/navigation' ) { + return ; + } + return ( + <> + + + + { + saveMenu( { + ...menu, + name: value || '(untitled menu)', + } ); + } } + /> + + + + ); + }, + 'withMenuName' + ); + addFilter( + 'editor.BlockEdit', + 'core/edit-navigation/with-menu-name', + withMenuName ); - }, - 'withMenuName' - ); + return () => removeFilter( 'editor.BlockEdit', 'core/edit-navigation/with-menu-name', withMenuName ); + } + }, [ menu ] ); + - addFilter( - 'editor.BlockEdit', - 'core/edit-navigation/with-menu-name', - withMenuName - ); }