diff --git a/packages/editor/src/components/autocompleters/test/user.js b/packages/editor/src/components/autocompleters/test/user.js
index f699ebfbfd2022..3ef0e41549c7e9 100644
--- a/packages/editor/src/components/autocompleters/test/user.js
+++ b/packages/editor/src/components/autocompleters/test/user.js
@@ -1,34 +1,31 @@
/**
* Internal dependencies
*/
-import userCompleter from '../user';
+import { getUserLabel } from '../user';
describe( 'user', () => {
- describe( 'getOptionLabel', () => {
+ describe( 'getUserLabel', () => {
it( 'should return user details fragment', () => {
const user = {
name: 'Smithers Jones',
slug: 'userSlug',
avatar_urls: { 24: 'http://my.avatar' },
};
- const userLabel = userCompleter.getOptionLabel( user );
- expect( userLabel[ 0 ] ).toEqual(
-
- );
- expect( userLabel[ 1 ] ).toEqual(
-
- Smithers Jones
-
- );
- expect( userLabel[ 2 ] ).toEqual(
-
- userSlug
-
+ const userLabel = getUserLabel( user );
+ expect( userLabel ).toEqual(
+ <>
+
+
+ Smithers Jones
+
+
+ userSlug
+
+ >
);
} );
it( 'should return user details fragment without default avatar dashicon if avatar_urls array not set', () => {
@@ -36,19 +33,17 @@ describe( 'user', () => {
name: 'Smithers Jones',
slug: 'userSlug',
};
- const userLabel = userCompleter.getOptionLabel( user );
- expect( userLabel[ 0 ] ).toEqual(
-
- );
- expect( userLabel[ 1 ] ).toEqual(
-
- Smithers Jones
-
- );
- expect( userLabel[ 2 ] ).toEqual(
-
- userSlug
-
+ const userLabel = getUserLabel( user );
+ expect( userLabel ).toEqual(
+ <>
+
+
+ Smithers Jones
+
+
+ userSlug
+
+ >
);
} );
} );
diff --git a/packages/editor/src/components/autocompleters/user.js b/packages/editor/src/components/autocompleters/user.js
index 4b8c1f2dbcf374..e176d3fb6b05a2 100644
--- a/packages/editor/src/components/autocompleters/user.js
+++ b/packages/editor/src/components/autocompleters/user.js
@@ -1,10 +1,37 @@
/**
* WordPress dependencies
*/
-import apiFetch from '@wordpress/api-fetch';
+import { useMemo } from '@wordpress/element';
+import { useSelect } from '@wordpress/data';
+import { store as coreStore } from '@wordpress/core-data';
/** @typedef {import('@wordpress/components').WPCompleter} WPCompleter */
+export function getUserLabel( user ) {
+ const avatar =
+ user.avatar_urls && user.avatar_urls[ 24 ] ? (
+
+ ) : (
+
+ );
+
+ return (
+ <>
+ { avatar }
+
+ { user.name }
+
+
+ { user.slug }
+
+ >
+ );
+}
+
/**
* A user mentions completer.
*
@@ -14,40 +41,34 @@ export default {
name: 'users',
className: 'editor-autocompleters__user',
triggerPrefix: '@',
- options( search ) {
- let payload = '';
- if ( search ) {
- payload = '?search=' + encodeURIComponent( search );
- }
- return apiFetch( { path: '/wp/v2/users' + payload } );
- },
- isDebounced: true,
- getOptionKeywords( user ) {
- return [ user.slug, user.name ];
- },
- getOptionLabel( user ) {
- const avatar =
- user.avatar_urls && user.avatar_urls[ 24 ] ? (
-
- ) : (
-
- );
-
- return [
- avatar,
-
- { user.name }
- ,
-
- { user.slug }
- ,
- ];
+
+ useItems( filterValue ) {
+ const users = useSelect(
+ ( select ) => {
+ const { getUsers } = select( coreStore );
+ return getUsers( {
+ context: 'view',
+ search: encodeURIComponent( filterValue ),
+ } );
+ },
+ [ filterValue ]
+ );
+
+ const options = useMemo(
+ () =>
+ users
+ ? users.map( ( user ) => ( {
+ key: `user-${ user.slug }`,
+ value: user,
+ label: getUserLabel( user ),
+ } ) )
+ : [],
+ [ users ]
+ );
+
+ return [ options ];
},
+
getOptionCompletion( user ) {
return `@${ user.slug }`;
},