The Node.js® Website

fix: input debounce (#6478)

authored by Caner Akdas and committed by GitHub 2eced3cf 789e7cdf

Changed files
+16 -9
components
Common
Search
util
+9 -2
components/Common/Search/States/WithSearchBox.tsx
··· 71 71 }, []); 72 72 73 73 useEffect( 74 - () => debounce(() => search(searchTerm), 1000), 74 + () => { 75 + debounce(() => search(searchTerm), 1000)(); 76 + }, 75 77 // we don't need to care about memoization of search function 76 78 // eslint-disable-next-line react-hooks/exhaustive-deps 77 79 [searchTerm, selectedFacet] ··· 100 102 }; 101 103 102 104 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, 104 111 ...(searchResults?.facets?.siteSection?.values ?? {}), 105 112 }; 106 113
+7 -7
util/debounce.ts
··· 1 1 type DebounceFunction<T = unknown> = (...args: Array<T>) => void; 2 2 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; 8 4 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>) => { 10 11 clearTimeout(timeoutId); 11 12 12 13 timeoutId = setTimeout(() => { 13 14 func(...args); 14 15 }, delay); 15 16 }; 16 - };