Skip to content

Commit

Permalink
fix: input debounce (#6478)
Browse files Browse the repository at this point in the history
  • Loading branch information
canerakdas authored Mar 19, 2024
1 parent 789e7cd commit 2eced3c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
11 changes: 9 additions & 2 deletions components/Common/Search/States/WithSearchBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ export const WithSearchBox: FC<SearchBoxProps> = ({ onClose }) => {
}, []);

useEffect(
() => debounce(() => search(searchTerm), 1000),
() => {
debounce(() => search(searchTerm), 1000)();
},
// we don't need to care about memoization of search function
// eslint-disable-next-line react-hooks/exhaustive-deps
[searchTerm, selectedFacet]
Expand Down Expand Up @@ -100,7 +102,12 @@ export const WithSearchBox: FC<SearchBoxProps> = ({ onClose }) => {
};

const facets: Facets = {
all: searchResults?.count ?? 0,
all: searchResults?.facets
? Object.values(searchResults?.facets.siteSection.values).reduce(
(a, b) => a + b,
0
)
: 0,
...(searchResults?.facets?.siteSection?.values ?? {}),
};

Expand Down
14 changes: 7 additions & 7 deletions util/debounce.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
type DebounceFunction<T = unknown> = (...args: Array<T>) => void;

export const debounce = <T extends DebounceFunction>(
func: T,
delay: number
): ((...args: Parameters<T>) => void) => {
let timeoutId: NodeJS.Timeout;
let timeoutId: NodeJS.Timeout;

return (...args: Parameters<T>) => {
export const debounce =
<T extends DebounceFunction>(
func: T,
delay: number
): ((...args: Parameters<T>) => void) =>
(...args: Parameters<T>) => {
clearTimeout(timeoutId);

timeoutId = setTimeout(() => {
func(...args);
}, delay);
};
};

0 comments on commit 2eced3c

Please sign in to comment.