@@ -7,7 +7,8 @@ import { makeStyles } from "@material-ui/core/styles"
7
7
import TextField from "@material-ui/core/TextField"
8
8
import SearchIcon from "@material-ui/icons/Search"
9
9
import { FormikErrors , useFormik } from "formik"
10
- import { useState } from "react"
10
+ import debounce from "just-debounce-it"
11
+ import { useCallback , useEffect , useState } from "react"
11
12
import { getValidationErrorMessage } from "../../api/errors"
12
13
import { getFormHelpers } from "../../util/formUtils"
13
14
import { CloseDropdown , OpenDropdown } from "../DropdownArrows/DropdownArrows"
@@ -53,6 +54,23 @@ export const SearchBarWithFilter: React.FC<SearchBarWithFilterProps> = ({
53
54
} ,
54
55
} )
55
56
57
+ // debounce query string entry by user
58
+ // we want the dependency array empty here
59
+ // as we don't need to redefine the function
60
+ // eslint-disable-next-line react-hooks/exhaustive-deps
61
+ const debouncedOnFilter = useCallback (
62
+ debounce ( ( debouncedQueryString : string ) => {
63
+ onFilter ( debouncedQueryString )
64
+ } , 300 ) ,
65
+ [ ] ,
66
+ )
67
+
68
+ // update the query params while typing
69
+ useEffect ( ( ) => {
70
+ debouncedOnFilter ( form . values . query )
71
+ return ( ) => debouncedOnFilter . cancel ( )
72
+ } , [ debouncedOnFilter , form . values . query ] )
73
+
56
74
const getFieldHelpers = getFormHelpers < FilterFormValues > ( form )
57
75
58
76
const [ anchorEl , setAnchorEl ] = useState < null | HTMLElement > ( null )
0 commit comments