Skip to content

Commit

Permalink
Add search in Query block (#25222)
Browse files Browse the repository at this point in the history
* add search in Query block

* fix React warning

* debounce search requests
  • Loading branch information
ntsekouras authored Sep 11, 2020
1 parent 668a710 commit 7a31f9c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/block-library/src/query-loop/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default function QueryLoopEdit( {
order,
orderBy,
author,
search,
} = {},
queryContext,
},
Expand All @@ -49,6 +50,9 @@ export default function QueryLoopEdit( {
if ( author ) {
query.author = author;
}
if ( search ) {
query.search = search;
}
return {
posts: select( 'core' ).getEntityRecords(
'postType',
Expand All @@ -68,6 +72,7 @@ export default function QueryLoopEdit( {
orderBy,
clientId,
author,
search,
]
);

Expand Down
3 changes: 3 additions & 0 deletions packages/block-library/src/query-loop/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ function render_block_core_query_loop( $attributes, $content, $block ) {
if ( isset( $block->context['query']['author'] ) ) {
$query['author'] = $block->context['query']['author'];
}
if ( isset( $block->context['query']['search'] ) ) {
$query['s'] = $block->context['query']['search'];
}
}

$posts = get_posts( $query );
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/query/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"tagIds": [],
"order": "desc",
"orderBy": "date",
"author": null
"author": "",
"search": ""
}
}
},
Expand Down
21 changes: 21 additions & 0 deletions packages/block-library/src/query/edit/query-toolbar.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
/**
* External dependencies
*/
import { debounce } from 'lodash';

/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { useEffect, useState, useCallback } from '@wordpress/element';
import {
Toolbar,
Dropdown,
ToolbarButton,
RangeControl,
TextControl,
FormTokenField,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
Expand All @@ -33,6 +40,15 @@ export default function QueryToolbar( { query, setQuery } ) {
tags: getTermsInfo( _tags ),
};
}, [] );
const [ querySearch, setQuerySearch ] = useState( query.search );
const onChangeDebounced = useCallback(
debounce( () => setQuery( { search: querySearch } ), 250 ),
[ querySearch ]
);
useEffect( () => {
onChangeDebounced();
return onChangeDebounced.cancel;
}, [ querySearch, onChangeDebounced ] );

// Handles categories and tags changes.
const onTermsChange = ( terms, queryProperty ) => ( newTermValues ) => {
Expand Down Expand Up @@ -113,6 +129,11 @@ export default function QueryToolbar( { query, setQuery } ) {
onChange={ onTagsChange }
/>
) }
<TextControl
label={ __( 'Search' ) }
value={ querySearch }
onChange={ ( value ) => setQuerySearch( value ) }
/>
</>
) }
/>
Expand Down
3 changes: 2 additions & 1 deletion packages/e2e-tests/fixtures/blocks/core__query.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"tagIds": [],
"order": "desc",
"orderBy": "date",
"author": null
"author": "",
"search": ""
}
},
"innerBlocks": [],
Expand Down

0 comments on commit 7a31f9c

Please sign in to comment.