Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 5 additions & 3 deletions packages/editor/src/components/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ const MAX_SUGGESTED_ITEMS = 9;
export const searchItems = ( items, searchTerm ) => {
const normalizedSearchTerm = normalizeTerm( searchTerm );
const matchSearch = ( string ) => normalizeTerm( string ).indexOf( normalizedSearchTerm ) !== -1;
const categories = getCategories();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we retrieve the categories in the withSelect using select( 'core/blocks' ).getCategories() and then pass them to this function?
I think data retrieval functions of wp.blocks only exist because of historical reasons and they may be removed some day.
Maybe we can also add a test case to the searchItems tests matching using the categories.

Copy link
Member Author

@tyxla tyxla Oct 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we retrieve the categories in the withSelect using select( 'core/blocks' ).getCategories() and then pass them to this function?

Yes, we could, but I intentionally didn't do it - there are several more usages of getCategories() in this file (and not only in this file), so I think rewriting to use withSelect would be material for another PR.

I think data retrieval functions of wp.blocks only exist because of historical reasons and they may be removed some day.

I realize this might be the case, but they don't even seem to be deprecated yet, so in that case I'd prefer staying consistent and following the example of code that's already merged and has proved to work well.

Maybe we can also add a test case to the searchItems tests matching using the categories.

Good call! I've added a unit test case in 11d1b69


return items.filter( ( item ) =>
matchSearch( item.title ) || some( item.keywords, matchSearch )
);
return items.filter( ( item ) => {
const itemCategory = find( categories, { slug: item.category } );
return matchSearch( item.title ) || some( item.keywords, matchSearch ) || ( itemCategory && matchSearch( itemCategory.title ) );
} );
};

/**
Expand Down
6 changes: 6 additions & 0 deletions packages/editor/src/components/inserter/test/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,12 @@ describe( 'searchItems', () => {
);
} );

it( 'should search items using the categories', () => {
expect( searchItems( items, 'LAYOUT' ) ).toEqual(
[ moreItem ]
);
} );

it( 'should ignore a leading slash on a search term', () => {
expect( searchItems( items, '/GOOGL' ) ).toEqual(
[ youtubeItem ]
Expand Down