-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add post author selector enhanced with ComboboxControl (#23237)
Co-authored-by: epiqueras <epiquerass@gmail.com> Co-authored-by: Enrique Piqueras <epiqueras@users.noreply.github.com> Co-authored-by: Noah Allen <noahtallen@gmail.com> Co-authored-by: Riad Benguella <benguella@gmail.com> Co-authored-by: jasmussen <joen@automattic.com>
- Loading branch information
1 parent
6325f76
commit 78dde89
Showing
8 changed files
with
114 additions
and
144 deletions.
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
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
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
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 |
---|---|---|
@@ -1,71 +1,112 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { debounce } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState, useMemo, useEffect } from '@wordpress/element'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { withInstanceId, compose } from '@wordpress/compose'; | ||
import { Component } from '@wordpress/element'; | ||
import { withSelect, withDispatch } from '@wordpress/data'; | ||
import { decodeEntities } from '@wordpress/html-entities'; | ||
import { ComboboxControl } from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import PostAuthorCheck from './check'; | ||
|
||
export class PostAuthor extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
function PostAuthor() { | ||
const [ fieldValue, setFieldValue ] = useState(); | ||
|
||
this.setAuthorId = this.setAuthorId.bind( this ); | ||
} | ||
const { authorId, isLoading, authors, postAuthor } = useSelect( | ||
( select ) => { | ||
const { getUser, getUsers, isResolving } = select( 'core' ); | ||
const { getEditedPostAttribute } = select( 'core/editor' ); | ||
const author = getUser( getEditedPostAttribute( 'author' ) ); | ||
const query = | ||
! fieldValue || '' === fieldValue ? {} : { search: fieldValue }; | ||
return { | ||
authorId: getEditedPostAttribute( 'author' ), | ||
postAuthor: author, | ||
authors: getUsers( { who: 'authors', ...query } ), | ||
isLoading: isResolving( 'core', 'getUsers', [ | ||
{ search: fieldValue, who: 'authors' }, | ||
] ), | ||
}; | ||
}, | ||
[ fieldValue ] | ||
); | ||
const { editPost } = useDispatch( 'core/editor' ); | ||
|
||
setAuthorId( event ) { | ||
const { onUpdateAuthor } = this.props; | ||
const { value } = event.target; | ||
onUpdateAuthor( Number( value ) ); | ||
} | ||
const authorOptions = useMemo( () => { | ||
const fetchedAuthors = ( authors ?? [] ).map( ( author ) => { | ||
return { | ||
value: author.id, | ||
label: author.name, | ||
}; | ||
} ); | ||
|
||
// Ensure the current author is included in the dropdown list. | ||
const foundAuthor = fetchedAuthors.findIndex( | ||
( { value } ) => postAuthor?.id === value | ||
); | ||
|
||
render() { | ||
const { postAuthor, instanceId, authors } = this.props; | ||
const selectId = 'post-author-selector-' + instanceId; | ||
if ( foundAuthor < 0 && postAuthor ) { | ||
return [ | ||
{ value: postAuthor.id, label: postAuthor.name }, | ||
...fetchedAuthors, | ||
]; | ||
} | ||
|
||
// Disable reason: A select with an onchange throws a warning | ||
return fetchedAuthors; | ||
}, [ authors, postAuthor ] ); | ||
|
||
/* eslint-disable jsx-a11y/no-onchange */ | ||
return ( | ||
<PostAuthorCheck> | ||
<label htmlFor={ selectId }>{ __( 'Author' ) }</label> | ||
<select | ||
id={ selectId } | ||
value={ postAuthor } | ||
onChange={ this.setAuthorId } | ||
className="editor-post-author__select" | ||
> | ||
{ authors.map( ( author ) => ( | ||
<option key={ author.id } value={ author.id }> | ||
{ decodeEntities( author.name ) } | ||
</option> | ||
) ) } | ||
</select> | ||
</PostAuthorCheck> | ||
); | ||
/* eslint-enable jsx-a11y/no-onchange */ | ||
// Initializes the post author properly | ||
// Also ensures external changes are reflected. | ||
useEffect( () => { | ||
if ( postAuthor ) { | ||
setFieldValue( postAuthor.name ); | ||
} | ||
}, [ postAuthor ] ); | ||
|
||
/** | ||
* Handle author selection. | ||
* | ||
* @param {number} postAuthorId The selected Author. | ||
*/ | ||
const handleSelect = ( postAuthorId ) => { | ||
if ( ! postAuthorId ) { | ||
return; | ||
} | ||
editPost( { author: postAuthorId } ); | ||
}; | ||
|
||
/** | ||
* Handle user input. | ||
* | ||
* @param {string} inputValue The current value of the input field. | ||
*/ | ||
const handleKeydown = ( inputValue ) => { | ||
setFieldValue( inputValue ); | ||
}; | ||
|
||
if ( ! postAuthor ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<PostAuthorCheck> | ||
<ComboboxControl | ||
label={ __( 'Author' ) } | ||
options={ authorOptions } | ||
value={ authorId } | ||
onFilterValueChange={ debounce( handleKeydown, 300 ) } | ||
onChange={ handleSelect } | ||
isLoading={ isLoading } | ||
/> | ||
</PostAuthorCheck> | ||
); | ||
} | ||
|
||
export default compose( [ | ||
withSelect( ( select ) => { | ||
return { | ||
postAuthor: select( 'core/editor' ).getEditedPostAttribute( | ||
'author' | ||
), | ||
authors: select( 'core' ).getAuthors(), | ||
}; | ||
} ), | ||
withDispatch( ( dispatch ) => ( { | ||
onUpdateAuthor( author ) { | ||
dispatch( 'core/editor' ).editPost( { author } ); | ||
}, | ||
} ) ), | ||
withInstanceId, | ||
] )( PostAuthor ); | ||
export default PostAuthor; |
This file was deleted.
Oops, something went wrong.