Skip to content

Commit

Permalink
Navigation Editor: Introduce useMenuEntityProp hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Apr 23, 2021
1 parent 3011744 commit b790a14
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
18 changes: 6 additions & 12 deletions packages/edit-navigation/src/components/name-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { TextControl } from '@wordpress/components';
import { useEffect, useRef, useContext } from '@wordpress/element';
/**
* Internal dependencies
*/
import { TextControl } from '@wordpress/components';
import {
IsMenuNameControlFocusedContext,
untitledMenu,
useMenuEntity,
useMenuEntityProp,
useSelectedMenuData,
} from '../../hooks';

Expand All @@ -20,18 +19,13 @@ export function NameEditor() {
);

const { menuId } = useSelectedMenuData();
const { editedMenu, editMenuEntityRecord, menuEntityData } = useMenuEntity(
menuId
);
const editedMenuName = menuId && editedMenu.name;

const editMenuName = ( name = untitledMenu ) =>
editMenuEntityRecord( ...menuEntityData, { name } );
const [ name, setName ] = useMenuEntityProp( 'name', menuId );

const inputRef = useRef();
useEffect( () => {
if ( isMenuNameEditFocused ) inputRef.current.focus();
}, [ isMenuNameEditFocused ] );

return (
<>
<TextControl
Expand All @@ -42,8 +36,8 @@ export function NameEditor() {
label={ __( 'Name' ) }
onBlur={ () => setIsMenuNameEditFocused( false ) }
className="edit-navigation-name-editor__text-control"
value={ editedMenuName }
onChange={ editMenuName }
value={ name }
onChange={ setName }
/>
</>
);
Expand Down
1 change: 1 addition & 0 deletions packages/edit-navigation/src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const MenuIdContext = createContext();
export const IsMenuNameControlFocusedContext = createContext();

export { default as useMenuEntity } from './use-menu-entity';
export { default as useMenuEntityProp } from './use-menu-entity-prop';
export { default as useNavigationEditor } from './use-navigation-editor';
export { default as useNavigationBlockEditor } from './use-navigation-block-editor';
export { default as useMenuNotifications } from './use-menu-notifications';
Expand Down
24 changes: 24 additions & 0 deletions packages/edit-navigation/src/hooks/use-menu-entity-prop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* WordPress dependencies
*/
import { useEntityProp } from '@wordpress/core-data';

/**
* Internal dependencies
*/
import { MENU_KIND, MENU_POST_TYPE } from '../constants';

/**
* Returns the value and setter for the specified
* property of the menu.
*
* @param {string} prop A Property name.
* @param {string} menuId A menu ID.
*
* @return {[*, Function]} A tuple where the first item is the
* property value and the second is the
* setter.
*/
export default function useMenuEntityProp( prop, menuId ) {
return useEntityProp( MENU_KIND, MENU_POST_TYPE, prop, menuId );
}

0 comments on commit b790a14

Please sign in to comment.