Skip to content

Commit

Permalink
Add create new menu button to nav block (#36245)
Browse files Browse the repository at this point in the history
* Adds a create new button to navigation block

* Keep it simple
  • Loading branch information
talldan committed Nov 5, 2021
1 parent 12f2eb1 commit 51a0a21
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
9 changes: 9 additions & 0 deletions packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,15 @@ function Navigation( {
setNavigationMenuId( id );
onClose();
} }
onCreateNew={ () => {
if ( navigationArea ) {
setAreaMenu( 0 );
}
setAttributes( {
navigationMenuId: undefined,
} );
setIsPlaceholderShown( true );
} }
/>
) }
</ToolbarDropdownMenu>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
/**
* WordPress dependencies
*/
import { MenuGroup, MenuItemsChoice } from '@wordpress/components';
import { MenuGroup, MenuItem, MenuItemsChoice } from '@wordpress/components';
import { useEntityId } from '@wordpress/core-data';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import useNavigationMenu from '../use-navigation-menu';

export default function NavigationMenuSelector( { onSelect } ) {
export default function NavigationMenuSelector( { onSelect, onCreateNew } ) {
const { navigationMenus } = useNavigationMenu();
const navigationMenuId = useEntityId( 'postType', 'wp_navigation' );

return (
<MenuGroup>
<MenuItemsChoice
value={ navigationMenuId }
onSelect={ ( selectedId ) =>
onSelect(
navigationMenus.find(
( post ) => post.id === selectedId
<>
<MenuGroup>
<MenuItemsChoice
value={ navigationMenuId }
onSelect={ ( selectedId ) =>
onSelect(
navigationMenus.find(
( post ) => post.id === selectedId
)
)
)
}
choices={ navigationMenus.map( ( { id, title } ) => ( {
value: id,
label: title.rendered,
} ) ) }
/>
</MenuGroup>
}
choices={ navigationMenus.map( ( { id, title } ) => ( {
value: id,
label: title.rendered,
} ) ) }
/>
</MenuGroup>
<MenuGroup>
<MenuItem onClick={ onCreateNew }>
{ __( 'Create new menu' ) }
</MenuItem>
</MenuGroup>
</>
);
}

0 comments on commit 51a0a21

Please sign in to comment.