From ee411a71bef154acd8275ec484316d0bb21b2323 Mon Sep 17 00:00:00 2001 From: "amaresh.sm" Date: Tue, 24 May 2022 00:11:36 +0530 Subject: [PATCH] docs: fix search option in mobile view --- docs/src/_includes/components/nav-search.html | 3 +- docs/src/_includes/components/search.html | 4 +- docs/src/assets/js/search.js | 37 +++++++++++-------- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/docs/src/_includes/components/nav-search.html b/docs/src/_includes/components/nav-search.html index 531e8197957..ed8d3653234 100644 --- a/docs/src/_includes/components/nav-search.html +++ b/docs/src/_includes/components/nav-search.html @@ -5,7 +5,8 @@ - + + diff --git a/docs/src/_includes/components/search.html b/docs/src/_includes/components/search.html index 6f86571b740..91f83e9ba41 100644 --- a/docs/src/_includes/components/search.html +++ b/docs/src/_includes/components/search.html @@ -5,8 +5,8 @@ - + -
+ diff --git a/docs/src/assets/js/search.js b/docs/src/assets/js/search.js index e9f3a5e58f7..4bd36775945 100644 --- a/docs/src/assets/js/search.js +++ b/docs/src/assets/js/search.js @@ -18,8 +18,7 @@ const client = algoliasearch('L633P0C2IR', 'bb6bbd2940351f3afc18844a6b06a6e8'); const index = client.initIndex('eslint'); // page -const resultsElement = document.querySelector('#search-results'); -const searchInput = document.querySelector('#search'); +const searchInputs = document.querySelectorAll('input.search__input'); //----------------------------------------------------------------------------- // Helpers @@ -38,9 +37,10 @@ function fetchSearchResults(query) { /** * Removes any current search results from the display. + * @param {Element} resultsElement The HTML element used to display search results. * @returns {void} */ -function clearSearchResults() { +function clearSearchResults(resultsElement) { while (resultsElement.firstChild) { resultsElement.removeChild(resultsElement.firstChild); } @@ -49,11 +49,11 @@ function clearSearchResults() { /** * Displays the given search results in the page. * @param {Array} results The search results to display. + * @param {Element} resultsElement The HTML element used to display search results. * @returns {void} */ -function displaySearchResults(results) { - - clearSearchResults(); +function displaySearchResults(results,resultsElement) { + clearSearchResults(resultsElement); if (results.length) { @@ -82,16 +82,21 @@ function displaySearchResults(results) { // listen for input changes -searchInput.addEventListener('keyup', function (event) { - const query = searchInput.value; - - if (query.length > 2) { - fetchSearchResults(query) - .then(displaySearchResults) - .catch(clearSearchResults); - } else { - clearSearchResults(); - } +searchInputs.forEach((searchInput) => { + searchInput.addEventListener('keyup', function (event) { + const resultsElement = document.querySelector(`#${searchInput.id}-results`); + const query = searchInput.value; + + if (query.length > 2) { + fetchSearchResults(query) + .then(function (results) { displaySearchResults(results, resultsElement) }) + .catch(function () { + clearSearchResults(resultsElement); + }); + } else { + clearSearchResults(resultsElement); + } + }); }); // add an event listenrer for a click on the search link