Skip to content

Commit 7a31f9c

Browse files
authored
Add search in Query block (#25222)
* add search in Query block * fix React warning * debounce search requests
1 parent 668a710 commit 7a31f9c

5 files changed

Lines changed: 33 additions & 2 deletions

File tree

packages/block-library/src/query-loop/edit.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export default function QueryLoopEdit( {
2727
order,
2828
orderBy,
2929
author,
30+
search,
3031
} = {},
3132
queryContext,
3233
},
@@ -49,6 +50,9 @@ export default function QueryLoopEdit( {
4950
if ( author ) {
5051
query.author = author;
5152
}
53+
if ( search ) {
54+
query.search = search;
55+
}
5256
return {
5357
posts: select( 'core' ).getEntityRecords(
5458
'postType',
@@ -68,6 +72,7 @@ export default function QueryLoopEdit( {
6872
orderBy,
6973
clientId,
7074
author,
75+
search,
7176
]
7277
);
7378

packages/block-library/src/query-loop/index.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ function render_block_core_query_loop( $attributes, $content, $block ) {
4747
if ( isset( $block->context['query']['author'] ) ) {
4848
$query['author'] = $block->context['query']['author'];
4949
}
50+
if ( isset( $block->context['query']['search'] ) ) {
51+
$query['s'] = $block->context['query']['search'];
52+
}
5053
}
5154

5255
$posts = get_posts( $query );

packages/block-library/src/query/block.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"tagIds": [],
1616
"order": "desc",
1717
"orderBy": "date",
18-
"author": null
18+
"author": "",
19+
"search": ""
1920
}
2021
}
2122
},

packages/block-library/src/query/edit/query-toolbar.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1+
/**
2+
* External dependencies
3+
*/
4+
import { debounce } from 'lodash';
5+
16
/**
27
* WordPress dependencies
38
*/
49
import { useSelect } from '@wordpress/data';
10+
import { useEffect, useState, useCallback } from '@wordpress/element';
511
import {
612
Toolbar,
713
Dropdown,
814
ToolbarButton,
915
RangeControl,
16+
TextControl,
1017
FormTokenField,
1118
} from '@wordpress/components';
1219
import { __ } from '@wordpress/i18n';
@@ -33,6 +40,15 @@ export default function QueryToolbar( { query, setQuery } ) {
3340
tags: getTermsInfo( _tags ),
3441
};
3542
}, [] );
43+
const [ querySearch, setQuerySearch ] = useState( query.search );
44+
const onChangeDebounced = useCallback(
45+
debounce( () => setQuery( { search: querySearch } ), 250 ),
46+
[ querySearch ]
47+
);
48+
useEffect( () => {
49+
onChangeDebounced();
50+
return onChangeDebounced.cancel;
51+
}, [ querySearch, onChangeDebounced ] );
3652

3753
// Handles categories and tags changes.
3854
const onTermsChange = ( terms, queryProperty ) => ( newTermValues ) => {
@@ -113,6 +129,11 @@ export default function QueryToolbar( { query, setQuery } ) {
113129
onChange={ onTagsChange }
114130
/>
115131
) }
132+
<TextControl
133+
label={ __( 'Search' ) }
134+
value={ querySearch }
135+
onChange={ ( value ) => setQuerySearch( value ) }
136+
/>
116137
</>
117138
) }
118139
/>

packages/e2e-tests/fixtures/blocks/core__query.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"tagIds": [],
1313
"order": "desc",
1414
"orderBy": "date",
15-
"author": null
15+
"author": "",
16+
"search": ""
1617
}
1718
},
1819
"innerBlocks": [],

0 commit comments

Comments
 (0)