Skip to content

Commit 028f427

Browse files
committed
📝 docs: add docs for hooks config option
1 parent 61654fc commit 028f427

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ You can pass the following config options to `typeahead-standalone`:
128128
|`tokenizer?: (words: string) => string[]`|The tokenizer function is used to split the search query and the search data by a given character(s). This function is useful when you wish to search hypenated-words or words with a certain prefix/suffix |words are split by space characters (new line, tab, spaces)|
129129
|`listScrollOptions?: ScrollIntoViewOptions`| Allows fine control over the scroll behaviour for a large list of suggestions that needs scrolling. These options are passed to the `scrollIntoView()` function. [MDN Ref](https://developer.mozilla.org/docs/Web/API/Element/scrollIntoView) | `{ block: "nearest", inline: "nearest", behaviour: "auto"}`|
130130
|`retainFocus`| This parameter is useful to control the focus on pressing the "Tab" key when the list of suggestions is open. If enabled, it selects the highlighted option & then returns the focus to the search input. If disabled, pressing "Tab" will select the highlighted option & take the focus away to the next focussable item in your form | `true`|
131+
|`hooks`| The hooks config option has currently has 1 hook available `updateHits: (resultSet) => resultSet` which is executed just before the search results are displayed to the user & can be used to override the search results returned by the search index. (useful for custom sorting, filtering, adding or removing results. Note that that this hook is an async function allowing you to make AJAX requests to fetch new results if needed) | undefined |
131132

132133
---
133134

docs/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ <h5 class="center">
121121
<li><a href="#config?id=tokenizer" title="tokenizer config param - typeahead-standalone.js" class="section-link">tokenizer</a></li>
122122
<li><a href="#config?id=scrolloptions" title="listScrollOptions config param - typeahead-standalone.js" class="section-link">listScrollOptions</a></li>
123123
<li><a href="#config?id=retainFocus" title="retainFocus config param - typeahead-standalone.js" class="section-link">retainFocus</a></li>
124+
<li><a href="#config?id=hooks" title="Hooks config param - typeahead-standalone.js" class="section-link">hooks</a></li>
124125
<li><a href="#config?id=templates" title="templates config param - typeahead-standalone.js" class="section-link">templates</a></li>
125126
</ol>
126127
</li>
@@ -191,6 +192,7 @@ <h5 class="center">
191192
<li><a href="#config?id=tokenizer" title="tokenizer config param - typeahead-standalone.js" class="section-link">tokenizer</a></li>
192193
<li><a href="#config?id=scrolloptions" title="tokenizer config param - typeahead-standalone.js" class="section-link">listScrollOptions</a></li>
193194
<li><a href="#config?id=retainFocus" title="retainFocus config param - typeahead-standalone.js" class="section-link">retainFocus</a></li>
195+
<li><a href="#config?id=hooks" title="Hooks config param - typeahead-standalone.js" class="section-link">hooks</a></li>
194196
<li><a href="#config?id=templates" title="templates config param - typeahead-standalone.js" class="section-link">templates</a></li>
195197
</ol>
196198
</li>

docs/pages/config.html

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,49 @@ <h4><a href="#config?id=retainFocus" id="retainFocus" title="retainFocus config
934934
});</pre>
935935
</div>
936936

937+
<hr>
938+
939+
<h4><a href="#config?id=hooks" id="hooks" title="Hooks config param - typeahead-standalone.js" class="submenu-link section-link">15. 🪝 hooks</h4></a>
940+
941+
<p>
942+
The <code>hooks</code> config option is useful to execute arbitrary code at specific moments in typeahead's lifecycle.
943+
Currently, there is 1 hook available - <code>updateHits</code> which is am async function that receives 1 parameter <b>resultSet</b>
944+
containing 3 properties:
945+
<ul>
946+
<li>- items (the suggestions returned by the search index)</li>
947+
<li>- query (the users input query),</li>
948+
<li>- count (the total number of suggestions found in the search index).</li>
949+
</ul>
950+
This hook is executed before the suggestions are displayed to the user and allows you to override the results returned by
951+
the search index in any way that you'd like. You can add, remove, modify, filter, sort suggestions and being an async function,
952+
you can also make AJAX requests to fetch new results if needed.
953+
</p>
954+
<div class="codeContainer">
955+
<span class="lang">JS</span>
956+
<span class="copy" title="Copy to Clipboard"><i class="far fa-copy"></i></span>
957+
<pre class="hljs language-js">
958+
typeahead({
959+
input: document.querySelector(".myInput"),
960+
source: {
961+
// ...
962+
},
963+
hooks: {
964+
updateHits: (resultSet) => {
965+
resultSet.items.push({name: "new suggestion"}); // add new suggestion
966+
// resultSet.items.filter( ... ); // filter the suggestions
967+
// resultSet.items.sort( ... ); // custom sort the suggestions
968+
969+
// you can also make an AJAX request to fetch results
970+
// const response = await fetch('https://example.com', { method: 'GET'});
971+
// const text = await response.text();
972+
// resultSet.items = text && JSON.parse(text);
973+
974+
return resultSet; // you must return the resultSet
975+
},
976+
}
977+
});</pre>
978+
</div>
979+
937980
<hr>
938981
<h4><a href="#config?id=templates" id="templates" title="templates config param - typeahead-standalone.js" class="submenu-link section-link">16. 🎨 templates</h4></a>
939982

0 commit comments

Comments
 (0)