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

Add search in Query block #25222

Merged
merged 3 commits into from
Sep 11, 2020
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
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