Skip to content

Commit

Permalink
debounce search requests
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Sep 10, 2020
1 parent 4a9d9cb commit bbf6435
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions packages/block-library/src/query/edit/query-toolbar.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/**
* External dependencies
*/
import { debounce } from 'lodash';

/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { useEffect, useState, useCallback } from '@wordpress/element';
import {
Toolbar,
Dropdown,
Expand Down Expand Up @@ -34,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 @@ -116,10 +131,8 @@ export default function QueryToolbar( { query, setQuery } ) {
) }
<TextControl
label={ __( 'Search' ) }
value={ query.search }
onChange={ ( value ) =>
setQuery( { search: value } )
}
value={ querySearch }
onChange={ ( value ) => setQuerySearch( value ) }
/>
</>
) }
Expand Down

0 comments on commit bbf6435

Please sign in to comment.