Skip to content
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

Use standard select element for small number of authors. #26426

Merged
merged 14 commits into from
Jan 3, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/editor/src/components/post-author/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ export default compose( [
false
),
postType: select( 'core/editor' ).getCurrentPostType(),
authors: select( 'core' ).getAuthors(),
authors: select( 'core' ).getUsers( {
who: 'authors',
per_page: 100,
} ),
};
} ),
withInstanceId,
Expand Down
61 changes: 51 additions & 10 deletions packages/editor/src/components/post-author/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import { useState, useMemo, useEffect } from '@wordpress/element';
import { useSelect, useDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { ComboboxControl } from '@wordpress/components';
import { withInstanceId, compose } from '@wordpress/compose';
import { decodeEntities } from '@wordpress/html-entities';

/**
* Internal dependencies
*/
import PostAuthorCheck from './check';

function PostAuthor() {
function PostAuthor( { instanceId } ) {
const [ fieldValue, setFieldValue ] = useState();

const [ isCombobox, setIsCombobox ] = useState( false );
const { authorId, isLoading, authors, postAuthor } = useSelect(
( select ) => {
const { __unstableGetAuthor, getAuthors, isResolving } = select(
Expand All @@ -28,8 +30,14 @@ function PostAuthor() {
const author = __unstableGetAuthor(
getEditedPostAttribute( 'author' )
);
const query =
! fieldValue || '' === fieldValue ? {} : { search: fieldValue };
const isSearching =
isCombobox &&
( postAuthor
? fieldValue && postAuthor.name !== fieldValue
: fieldValue );
const query = isSearching
? { search: fieldValue, per_page: 100 }
: { per_page: 100 };
return {
authorId: getEditedPostAttribute( 'author' ),
postAuthor: author,
Expand All @@ -41,6 +49,12 @@ function PostAuthor() {
);
const { editPost } = useDispatch( 'core/editor' );

useEffect( () => {
if ( authors ) {
setIsCombobox( authors?.length >= minimumUsersForCombobox );
}
}, [] );

const authorOptions = useMemo( () => {
const fetchedAuthors = ( authors ?? [] ).map( ( author ) => {
return {
Expand Down Expand Up @@ -71,17 +85,21 @@ function PostAuthor() {
setFieldValue( postAuthor.name );
}
}, [ postAuthor ] );

/**
* Handle author selection.
*
* @param {number} postAuthorId The selected Author.
* @param {number|Object} selectedAuthor The selected Author.
*/
const handleSelect = ( postAuthorId ) => {
if ( ! postAuthorId ) {
const handleSelect = ( selectedAuthor ) => {
if ( ! selectedAuthor ) {
return;
}
editPost( { author: postAuthorId } );
if ( isNaN( selectedAuthor ) ) {
const { value } = selectedAuthor.target;
editPost( { author: Number( value ) } );
} else {
editPost( { author: selectedAuthor } );
}
};

/**
Expand All @@ -97,6 +115,29 @@ function PostAuthor() {
return null;
}

if ( ! isCombobox ) {
const selectId = 'post-author-selector-' + instanceId;

return (
<PostAuthorCheck>
<label htmlFor={ selectId }>{ __( 'Author' ) }</label>
<select
id={ selectId }
defaultValue={ postAuthor.id }
onBlur={ handleSelect }
className="editor-post-author__select"
>
{ authors &&
authors.map( ( author ) => (
<option key={ author.id } value={ author.id }>
{ decodeEntities( author.name ) }
</option>
) ) }
</select>
</PostAuthorCheck>
);
}

return (
<PostAuthorCheck>
<ComboboxControl
Expand All @@ -112,4 +153,4 @@ function PostAuthor() {
);
}

export default PostAuthor;
export default compose( [ withInstanceId ] )( PostAuthor );
36 changes: 21 additions & 15 deletions packages/shortcode/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,29 @@ export function next( tag, text, index = 0 ) {
* @return {string} Text with shortcodes replaced.
*/
export function replace( tag, text, callback ) {
return text.replace(
regexp( tag ),
function ( match, left, $3, attrs, slash, content, closing, right ) {
// If both extra brackets exist, the shortcode has been properly
// escaped.
if ( left === '[' && right === ']' ) {
return match;
}
return text.replace( regexp( tag ), function (
match,
left,
$3,
attrs,
slash,
content,
closing,
right
) {
// If both extra brackets exist, the shortcode has been properly
// escaped.
if ( left === '[' && right === ']' ) {
return match;
}

// Create the match object and pass it through the callback.
const result = callback( fromMatch( arguments ) );
// Create the match object and pass it through the callback.
const result = callback( fromMatch( arguments ) );

// Make sure to return any of the extra brackets if they weren't used to
// escape the shortcode.
return result || result === '' ? left + result + right : match;
}
);
// Make sure to return any of the extra brackets if they weren't used to
// escape the shortcode.
return result || result === '' ? left + result + right : match;
} );
}

/**
Expand Down