Skip to content

Commit

Permalink
✨ feat: add hook to allow overriding search results
Browse files Browse the repository at this point in the history
  • Loading branch information
niketpathak committed Nov 15, 2024
1 parent 7c6968c commit a1e7fa7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
23 changes: 23 additions & 0 deletions demo/dev.umd.html
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,29 @@ <h4>Dummy input to test keyboard nav</h4>
minLength: 1,
limit: 10,
retainFocus: true,
hooks: {
updateHits: async (resultSet) => {

const response = await fetch(
"https://raw.githubusercontent.com/digitalfortress-tech/typeahead-standalone/master/docs/assets/json/superheroes.json",
{
method: 'GET',
}
);

const text = await response.text();
const data = text && JSON.parse(text);

const hits = data.results.slice(0,10)

// const hits = resultSet.hits.filter(hit => hit.popularity && hit.popularity > 50);

return {
hits,
count: hits.length,
};
}
},
templates: {
suggestion: (item) => {
const date = item.release_date
Expand Down
13 changes: 13 additions & 0 deletions src/common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,19 @@ export interface typeaheadConfig<T extends Dictionary> {
* It defaults to true.
*/
retainFocus?: boolean;
/**
* Hooks to be able to perform fine-tuning of results
*/
hooks?: {
/**
* The updateHits hook allows you to modify/filter/sort the search results before being rendered
* @param hits The found results
* @returns
*/
updateHits: (
resultSet: Pick<ResultSet<T>, 'hits' | 'query' | 'count'>
) => Promise<void> | Promise<Pick<ResultSet<T>, 'hits' | 'count'>>;
};
}

export interface ResultSet<T extends Dictionary> {
Expand Down
20 changes: 17 additions & 3 deletions src/typeahead-standalone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ const typeahead = <T extends Dictionary>(config: typeaheadConfig<T>): typeaheadR
...(config.classNames || {}),
};
const listScrollOptions: ScrollIntoViewOptions = { block: 'nearest', ...(config.listScrollOptions || {}) };
const hooks = {
updateHits: config.hooks?.updateHits || NOOP,
};

// validate presence of atleast one data-source
if (!local && !prefetch && !remote) throw new Error('e02');
Expand Down Expand Up @@ -305,7 +308,18 @@ const typeahead = <T extends Dictionary>(config: typeaheadConfig<T>): typeaheadR
/**
* Responsible for drawing/updating the view
*/
const update = (): void => {
const update = async (): Promise<void> => {
// hook to update Hits before displaying results from tree
const results_mod = await hooks.updateHits({
hits: resultSet.hits,
query: resultSet.query,
count: resultSet.count,
});
if (results_mod?.hits?.length) {
resultSet.hits = results_mod.hits;
resultSet.count = results_mod.count ?? results_mod.hits.length;
}

// No Matches
if (noSuggestionsHandler()) return;

Expand Down Expand Up @@ -521,7 +535,7 @@ const typeahead = <T extends Dictionary>(config: typeaheadConfig<T>): typeaheadR
startFetch();
};

const startFetch = (): void => {
const startFetch = async (): Promise<void> => {
clearRemoteDebounceTimer();
const val = input.value.replace(/\s{2,}/g, ' ').trim();

Expand All @@ -533,7 +547,7 @@ const typeahead = <T extends Dictionary>(config: typeaheadConfig<T>): typeaheadR
// inject default suggestions if they were updated in the empty() template
if (Array.isArray(emptyTemplateResp) && emptyTemplateResp.length) {
resultSet.hits = normalizer(emptyTemplateResp, keys[0]) as T[];
return update();
return await update();
}

// inject empty html template only if default suggestions aren't provided
Expand Down

0 comments on commit a1e7fa7

Please sign in to comment.