-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Post author selector: try downshift #7478
Changes from all commits
7213b6b
dec6f46
bcb97de
b898830
df22448
9dcc5ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,45 +5,107 @@ import { __ } from '@wordpress/i18n'; | |
import { withInstanceId } from '@wordpress/components'; | ||
import { Component, compose } from '@wordpress/element'; | ||
import { withSelect, withDispatch } from '@wordpress/data'; | ||
import apiRequest from '@wordpress/api-request'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import PostAuthorCheck from './check'; | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
import Downshift from 'downshift'; | ||
import { debounce } from 'underscore'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops, right :) |
||
|
||
export class PostAuthor extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
|
||
this.setAuthorId = this.setAuthorId.bind( this ); | ||
this.suggestAuthors = this.suggestAuthors.bind( this ); | ||
} | ||
|
||
setAuthorId( event ) { | ||
setAuthorId( selected ) { | ||
const { onUpdateAuthor } = this.props; | ||
const { value } = event.target; | ||
onUpdateAuthor( Number( value ) ); | ||
const { id } = selected; | ||
onUpdateAuthor( Number( id ) ); | ||
} | ||
|
||
suggestAuthors( query, args ) { | ||
if ( ! args.isOpen ) { | ||
return; | ||
} | ||
const payload = '?search=' + encodeURIComponent( query ); | ||
this.setState( { searching: true } ); | ||
apiRequest( { path: '/wp/v2/users' + payload } ).done( ( results ) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we leverage the data module instead? We already have a |
||
this.setState( { searching: false } ); | ||
this.setState( { authors: results.map( ( author ) => ( { id: author.id, name: author.name } ) ) } ); | ||
} ); | ||
} | ||
|
||
render() { | ||
const { postAuthor, instanceId, authors } = this.props; | ||
const { instanceId, authors, postAuthor } = this.props; | ||
const selectId = 'post-author-selector-' + instanceId; | ||
|
||
// Disable reason: A select with an onchange throws a warning | ||
const currentPostAuthor = postAuthor.length > 0 ? postAuthor[ 0 ].name : ''; | ||
const allAuthors = this.state && this.state.authors ? this.state.authors : authors; | ||
const isSearching = this.state && this.state.searching; | ||
|
||
/* eslint-disable jsx-a11y/no-onchange */ | ||
return ( | ||
currentPostAuthor && allAuthors && | ||
<PostAuthorCheck> | ||
<label htmlFor={ selectId }>{ __( 'Author' ) }</label> | ||
<select | ||
id={ selectId } | ||
value={ postAuthor } | ||
<Downshift | ||
onChange={ this.setAuthorId } | ||
className="editor-post-author__select" | ||
itemToString={ ( author ) => ( author ? author.value : '' ) } | ||
defaultInputValue={ currentPostAuthor } | ||
onInputValueChange={ debounce( this.suggestAuthors, 300 ) } | ||
> | ||
{ authors.map( ( author ) => ( | ||
<option key={ author.id } value={ author.id }>{ author.name }</option> | ||
) ) } | ||
</select> | ||
{ ( { | ||
getInputProps, | ||
getItemProps, | ||
getLabelProps, | ||
getMenuProps, | ||
isOpen, | ||
inputValue, | ||
highlightedIndex, | ||
selectedItem, | ||
} ) => ( | ||
<div> | ||
<label { ...getLabelProps() }>{ __( 'Author' ) }</label> | ||
<input { ...getInputProps() } /> | ||
<span | ||
className={ 'spinner' + ( isSearching ? ' is-active' : '' ) } | ||
style={ { position: 'absolute', right: '10px' } } | ||
/> | ||
<ul { ...getMenuProps() } > | ||
{ isOpen ? | ||
allAuthors | ||
.map( ( author ) => ( { id: author.id, value: author.name } ) ) | ||
.filter( ( author ) => | ||
! inputValue || | ||
author.value.toLowerCase().includes( inputValue.toLowerCase() ) ) | ||
.map( ( author, index ) => ( | ||
<li | ||
{ ...getItemProps( { | ||
key: author.id, | ||
index, | ||
item: author, | ||
style: { | ||
backgroundColor: | ||
highlightedIndex === index ? 'lightgray' : 'white', | ||
fontWeight: selectedItem === author ? 'bold' : 'normal', | ||
}, | ||
} ) } | ||
> | ||
{ author.value } | ||
</li> | ||
) ) : | ||
null } | ||
</ul> | ||
</div> | ||
) } | ||
</Downshift> | ||
</PostAuthorCheck> | ||
); | ||
/* eslint-enable jsx-a11y/no-onchange */ | ||
|
@@ -53,7 +115,7 @@ export class PostAuthor extends Component { | |
export default compose( [ | ||
withSelect( ( select ) => { | ||
return { | ||
postAuthor: select( 'core/editor' ).getEditedPostAttribute( 'author' ), | ||
postAuthor: select( 'core' ).getAuthor( select( 'core/editor' ).getEditedPostAttribute( 'author' ) ), | ||
authors: select( 'core' ).getAuthors(), | ||
}; | ||
} ), | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
In general the external dependencies are defined above all other deps, but this is not an official guideline :)