From e040716d54977e0c15f11b7ad4cd32f705e1c36c Mon Sep 17 00:00:00 2001 From: Pedro Duarte Date: Thu, 7 Jul 2022 23:24:55 +0100 Subject: [PATCH 01/12] Add `fetchOnFocus` prop to ContentSearch This new prop allows control over the focus state on ContentSearch. It will trigger a fetch just by focusing the search field, making the user aware of what they might be selecting from. To keep previous expectations the default value is `false`. --- components/content-search/index.js | 24 +++++++++++++++++++----- components/content-search/readme.md | 4 ++-- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/components/content-search/index.js b/components/content-search/index.js index 10ab8e64..38db3b7b 100644 --- a/components/content-search/index.js +++ b/components/content-search/index.js @@ -22,11 +22,13 @@ const ContentSearch = ({ perPage, queryFilter, excludeItems, + fetchOnFocus, }) => { const [searchString, setSearchString] = useState(''); const [searchQueries, setSearchQueries] = useState({}); const [selectedItem, setSelectedItem] = useState(null); const [currentPage, setCurrentPage] = useState(1); + const [isFocused, setIsFocused] = useState(false); const mounted = useRef(true); @@ -131,17 +133,17 @@ const ContentSearch = ({ /** * handleSearchStringChange * - * Using the keyword and the list of tags that are linked to the parent block - * search for posts/terms that match and return them to the autocomplete component. + * Using the keyword and the list of tags that are linked to the parent + * block search for posts/terms/users that match and return them to the + * autocomplete component. * * @param {string} keyword search query string * @param {string} page page query string */ const handleSearchStringChange = (keyword, page) => { + // Reset page and query on empty keyword. if (keyword.trim() === '') { - setSearchString(keyword); setCurrentPage(1); - return; } const preparedQuery = prepareSearchQuery(keyword, page); @@ -327,9 +329,19 @@ const ContentSearch = ({ }} placeholder={placeholder} autoComplete="off" + onFocus={() => { + setIsFocused(true); + + if (fetchOnFocus) { + handleSearchStringChange('', 1); + } + }} + onBlur={() => { + setIsFocused(false); + }} /> - {hasSearchString ? ( + {hasSearchString || (fetchOnFocus && isFocused) ? ( <>