Skip to content

Commit

Permalink
Fix Rename in Navigation on Browse Mode (#51791)
Browse files Browse the repository at this point in the history
* Ensure edits are passed to save

* Ensure empty strings are invalid

* Force break of long strings in menu titles
  • Loading branch information
getdave authored Jun 23, 2023
1 parent a844d20 commit 3b249d7
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default function SidebarNavigationScreenNavigationMenu() {
useNavigationMenuHandlers();

const _handleDelete = () => handleDelete( navigationMenu );
const _handleSave = () => handleSave( navigationMenu );
const _handleSave = ( edits ) => handleSave( navigationMenu, edits );
const _handleDuplicate = () => handleDuplicate( navigationMenu );

if ( isLoading ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@ import {
import { __ } from '@wordpress/i18n';
import { useState } from '@wordpress/element';

const notEmptyString = ( testString ) => testString?.trim()?.length > 0;

export default function RenameModal( { menuTitle, onClose, onSave } ) {
const [ editedMenuTitle, setEditedMenuTitle ] = useState( menuTitle );

const titleHasChanged = editedMenuTitle !== menuTitle;

const isEditedMenuTitleValid =
titleHasChanged && notEmptyString( editedMenuTitle );

return (
<Modal title={ __( 'Rename' ) } onRequestClose={ onClose }>
<form className="sidebar-navigation__rename-modal-form">
Expand All @@ -30,11 +37,15 @@ export default function RenameModal( { menuTitle, onClose, onSave } ) {
</Button>

<Button
disabled={ editedMenuTitle === menuTitle }
disabled={ ! isEditedMenuTitleValid }
variant="primary"
type="submit"
onClick={ ( e ) => {
e.preventDefault();

if ( ! isEditedMenuTitleValid ) {
return;
}
onSave( { title: editedMenuTitle } );

// Immediate close avoids ability to hit save multiple times.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ function useSaveNavigationMenu() {
const { createSuccessNotice, createErrorNotice } =
useDispatch( noticesStore );

const handleSave = async ( navigationMenu, edits = {} ) => {
const handleSave = async ( navigationMenu, edits ) => {
if ( ! edits ) {
return;
}

const postId = navigationMenu?.id;
// Prepare for revert in case of error.
const originalRecord = getEditedEntityRecord(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ export default function SidebarNavigationScreenNavigationMenus() {
navigationMenu={ firstNavigationMenu }
handleDelete={ () => handleDelete( firstNavigationMenu ) }
handleDuplicate={ () => handleDuplicate( firstNavigationMenu ) }
handleSave={ () => handleSave( firstNavigationMenu ) }
handleSave={ ( edits ) =>
handleSave( firstNavigationMenu, edits )
}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
.edit-site-sidebar-navigation-screen__title {
flex-grow: 1;
padding: $grid-unit-15 * 0.5 0 0 0;
overflow: hidden;
overflow-wrap: break-word;
}

.edit-site-sidebar-navigation-screen__actions {
Expand Down

0 comments on commit 3b249d7

Please sign in to comment.