From 5f7a1913d2fa1f0003f6c3c40b13f4863cd32ede Mon Sep 17 00:00:00 2001 From: Niket Pathak Date: Mon, 18 Nov 2024 14:13:48 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20add=20possibility=20to=20up?= =?UTF-8?q?date=20search=20index=20from=20within=20the=20updateHits=20hook?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/assets/js/testable.js | 13 +++++++++---- src/common.d.ts | 4 ++-- src/typeahead-standalone.ts | 5 +++-- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/demo/assets/js/testable.js b/demo/assets/js/testable.js index 48df6b4..041d0fa 100644 --- a/demo/assets/js/testable.js +++ b/demo/assets/js/testable.js @@ -1071,10 +1071,15 @@ const test20A = typeahead({ hooks: { updateHits: async (resultSet, loader) => { loader(); - const response = await fetch(`https://restcountries.com/v2/name/${resultSet.query}`); - const text = await response.text(); - resultSet.hits = text && JSON.parse(text); - loader(false); + try { + const response = await fetch(`https://restcountries.com/v2/name/${resultSet.query}`); + const text = await response.text(); + resultSet.hits = text && JSON.parse(text); + } catch (e) { + console.error('typeahead-20A failed request', e); + } finally { + loader(false); + } return resultSet; }, diff --git a/src/common.d.ts b/src/common.d.ts index 4ea5a8d..a4d038d 100644 --- a/src/common.d.ts +++ b/src/common.d.ts @@ -281,12 +281,12 @@ export interface typeaheadConfig { /** * The updateHits hook allows you to modify/filter/sort the search results before being rendered * @param hits The found results - * @returns A promise containing nothing/void or with the ResultSet + * @returns A promise containing nothing/void or with the ResultSet. You can set the "updateSearchIndex" flag to true if you wish for the returned items to be added to the search index. By default, the search index is not updated */ updateHits: ( resultSet: Pick, 'hits' | 'query' | 'count'>, loader: (visible: boolean) => void - ) => Promise | Promise, 'hits' | 'count'>>; + ) => Promise | Promise, 'hits'> & { count?: number; updateSearchIndex?: boolean }>; }; } diff --git a/src/typeahead-standalone.ts b/src/typeahead-standalone.ts index 006f603..56350c0 100644 --- a/src/typeahead-standalone.ts +++ b/src/typeahead-standalone.ts @@ -318,7 +318,7 @@ const typeahead = (config: typeaheadConfig): typeaheadR * Responsible for drawing/updating the view */ const update = async (): Promise => { - // hook to update Hits before displaying results from tree + // hook to update Hits before displaying results from search index/trie const results_mod = await hooks.updateHits( { hits: resultSet.hits, @@ -329,7 +329,8 @@ const typeahead = (config: typeaheadConfig): typeaheadR ); if (results_mod?.hits?.length) { resultSet.hits = results_mod.hits; - resultSet.count = results_mod.count ?? results_mod.hits.length; + results_mod.count && (resultSet.count = results_mod.count); + results_mod.updateSearchIndex && addToIndex(results_mod.hits); } // No Matches