feat: add debounceDuration option (#165)

closes #153
This commit is contained in:
Amr Bashir
2024-10-14 14:07:58 +03:00
committed by GitHub
parent e434414550
commit 549a4d852c
3 changed files with 23 additions and 1 deletions

View File

@@ -0,0 +1,5 @@
---
"meilisearch-docsearch": "minor"
---
Add `debounceDuration` option to set debouce duration or disable it.

View File

@@ -26,6 +26,19 @@ export interface DocSearchProps {
translations?: DocSearchTranslations;
searchParams?: SearchParams;
environment?: typeof window;
/**
* Duration to wait between keystores to determine whether a search should happen or not.
* Defaults to `200`.
*
* Set to `false` to disable debouncing.
*
* This is an optimization that discards unnecessary search operations, for example,
* if a user is typing `hello`, we skip search operations for `h`, `he`, `hel` and `hell`
* as this usually not what the user wants to search for, and instead wait a few milliseconds until
* the user stops typing for a brief moment, and then we do the search operation.
* In the previous example, that would be `hello`.
*/
debounceDuration?: number | false;
}
export type DocSearchTranslations = Partial<{

View File

@@ -62,6 +62,7 @@ export const DocSearchModal: Component<DocSearchModalProps> = ({
clientAgents,
searchParams,
environment = window,
debounceDuration = 200,
translations = {},
onClose,
initialQuery,
@@ -200,7 +201,10 @@ export const DocSearchModal: Component<DocSearchModalProps> = ({
setHitsCategories(catgeories);
});
}
const debouncedSearch = utils.debounce(search, 100);
const debouncedSearch = !debounceDuration
? search
: utils.debounce(search, debounceDuration);
if (initialQuery) {
onMount(() => {