-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Creation of menus on the edit navigation screen #22309
Merged
+262
−23
Merged
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
ffc21f0
Use hasFinishedResolution to correctly determine loading state
talldan c1cd1ec
First draft of a form to create a new menu
talldan a05b38d
Add conditional logic for UI
talldan 26de8a8
Use saveMenu action to create menu
talldan 69e98e1
Handle returning to initial state when deleting last menu
talldan 82351d8
Add create first menu message
talldan 13d4e6c
Markup CreateMenuForm component as a proper form
talldan c1072a1
Add button for creating new menus when an existing menu is selected
talldan 570e1a4
Improve responsive styles
talldan b0be954
Fix header text displaying at wrong time
talldan 00a8d89
Add an option for cancelling the menu creation process
talldan b0a18d4
Tidy up styling of cancel button
talldan c413d56
Tidy up rendering logic
talldan 395d664
Use notices for errors
talldan 3d11160
Add busy indicator that is not really shown
talldan 9d1e051
Use getIsResolving for more succinct code
talldan c0e37e4
Focus input field when creating a menu
talldan 00c6585
Try withFocusReturn
talldan c7e57a8
Fix returning to first menu when selecting cancel
talldan 83192e1
Try a snackbar
talldan 366ed6f
Remove dead code
talldan 7d784e2
Remove unused logic for hiding contents of panels
talldan 762e721
Show create panel at same time as other panels
talldan 037fbbb
Try setting the created menu after menu creation
talldan c8fd34b
Try only showing loading spinner on first load
talldan 357dfda
Fix menu creation notifications
talldan 7431733
Only hide panels on first load
talldan eb794a6
Fix uncontrolled select
talldan 27ea6f8
Fix button busy appearance
talldan 16270a4
Use pixel value for margin
talldan 9ef96d6
Ensure items do not take up full width
talldan 3b0316d
Ensure loading indicator when creating menus isn't shown when validat…
talldan 63cbfd5
Use a `finally` clause to mark when a save finishes
talldan d74569b
Fix menu save error message
talldan 0d7c57b
Remove selector dependency
talldan e9f8129
Center items along horizontal axis when using row layout
talldan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
packages/edit-navigation/src/components/menus-editor/create-menu-panel.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { some } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
Button, | ||
Panel, | ||
PanelBody, | ||
TextControl, | ||
withFocusReturn, | ||
} from '@wordpress/components'; | ||
import { useDispatch, useSelect } from '@wordpress/data'; | ||
import { useCallback, useEffect, useState } from '@wordpress/element'; | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
const { DOMParser } = window; | ||
|
||
const noticeId = 'edit-navigation-create-menu-error'; | ||
|
||
const menuNameMatches = ( menuName ) => ( menu ) => | ||
menu.name.toLowerCase() === menuName.toLowerCase(); | ||
|
||
export function CreateMenuForm( { onCancel, onCreateMenu, menus } ) { | ||
const [ menuName, setMenuName ] = useState( '' ); | ||
const [ isCreatingMenu, setIsCreatingMenu ] = useState( false ); | ||
const menuSaveError = useSelect( ( select ) => | ||
select( 'core' ).getLastEntitySaveError( 'root', 'menu' ) | ||
); | ||
const { saveMenu } = useDispatch( 'core' ); | ||
const { createInfoNotice, createErrorNotice, removeNotice } = useDispatch( | ||
'core/notices' | ||
); | ||
|
||
// Handle REST API Error messages. | ||
useEffect( () => { | ||
if ( menuSaveError ) { | ||
// Error messages from the REST API often contain HTML. | ||
// createErrorNotice does not support HTML in error text, so first | ||
// strip HTML out using DOMParser. | ||
const document = new DOMParser().parseFromString( | ||
menuSaveError.message, | ||
'text/html' | ||
); | ||
const errorText = document.body.textContent || ''; | ||
createErrorNotice( errorText, { id: noticeId } ); | ||
} | ||
}, [ menuSaveError ] ); | ||
|
||
const createMenu = useCallback( | ||
async ( event ) => { | ||
// Prevent form submission. | ||
event.preventDefault(); | ||
|
||
// Remove existing notices. | ||
removeNotice( noticeId ); | ||
|
||
if ( menuName.length === 0 ) { | ||
// Button is aria-disabled, do nothing. | ||
return; | ||
} | ||
|
||
// Validate the menu name doesn't match an existing menu. | ||
if ( some( menus, menuNameMatches( menuName ) ) ) { | ||
const message = sprintf( | ||
// translators: %s: the name of a menu. | ||
__( | ||
'The menu name %s conflicts with another menu name. Please try another.' | ||
), | ||
menuName | ||
); | ||
createErrorNotice( message, { id: noticeId } ); | ||
return; | ||
} | ||
|
||
setIsCreatingMenu( true ); | ||
|
||
const menu = await saveMenu( { name: menuName } ); | ||
if ( menu ) { | ||
createInfoNotice( __( 'Menu created' ), { | ||
type: 'snackbar', | ||
isDismissible: true, | ||
} ); | ||
onCreateMenu( menu.id ); | ||
} | ||
talldan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
setIsCreatingMenu( false ); | ||
}, | ||
[ menuName, menus ] | ||
); | ||
|
||
return ( | ||
<Panel className="edit-navigation-menus-editor__create-menu-panel"> | ||
<PanelBody title={ __( 'Create navigation menu' ) }> | ||
<form onSubmit={ createMenu }> | ||
<TextControl | ||
// Disable reason - autoFocus is legitimate in this usage, | ||
// The first focusable on the form should be focused, | ||
// which is this element. | ||
// eslint-disable-next-line jsx-a11y/no-autofocus | ||
autoFocus | ||
label={ __( 'Menu name' ) } | ||
value={ menuName } | ||
onChange={ setMenuName } | ||
placeholder={ __( 'Main Navigation' ) } | ||
/> | ||
<Button | ||
type="submit" | ||
isBusy={ isCreatingMenu } | ||
onClick={ createMenu } | ||
aria-disabled={ menuName.length === 0 } | ||
isPrimary | ||
> | ||
{ __( 'Create menu' ) } | ||
</Button> | ||
{ onCancel && ( | ||
<Button | ||
className="edit-navigation-menus-editor__cancel-create-menu-button" | ||
isSecondary | ||
onClick={ onCancel } | ||
> | ||
{ __( 'Cancel' ) } | ||
</Button> | ||
) } | ||
</form> | ||
</PanelBody> | ||
</Panel> | ||
); | ||
} | ||
|
||
export default withFocusReturn( CreateMenuForm ); | ||
talldan marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above - should this component just return null when
showNavigationStructure === false
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed this back to how it was before for the same reason as above, the panel will show at the same time as the create panel.