Skip to content

Commit 2eced3c

Browse files
authored
fix: input debounce (#6478)
1 parent 789e7cd commit 2eced3c

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

components/Common/Search/States/WithSearchBox.tsx

+9-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ export const WithSearchBox: FC<SearchBoxProps> = ({ onClose }) => {
7171
}, []);
7272

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

102104
const facets: Facets = {
103-
all: searchResults?.count ?? 0,
105+
all: searchResults?.facets
106+
? Object.values(searchResults?.facets.siteSection.values).reduce(
107+
(a, b) => a + b,
108+
0
109+
)
110+
: 0,
104111
...(searchResults?.facets?.siteSection?.values ?? {}),
105112
};
106113

util/debounce.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
type DebounceFunction<T = unknown> = (...args: Array<T>) => void;
22

3-
export const debounce = <T extends DebounceFunction>(
4-
func: T,
5-
delay: number
6-
): ((...args: Parameters<T>) => void) => {
7-
let timeoutId: NodeJS.Timeout;
3+
let timeoutId: NodeJS.Timeout;
84

9-
return (...args: Parameters<T>) => {
5+
export const debounce =
6+
<T extends DebounceFunction>(
7+
func: T,
8+
delay: number
9+
): ((...args: Parameters<T>) => void) =>
10+
(...args: Parameters<T>) => {
1011
clearTimeout(timeoutId);
1112

1213
timeoutId = setTimeout(() => {
1314
func(...args);
1415
}, delay);
1516
};
16-
};

0 commit comments

Comments
 (0)