@@ -7,7 +7,8 @@ import { makeStyles } from "@material-ui/core/styles"
77import TextField from "@material-ui/core/TextField"
88import SearchIcon from "@material-ui/icons/Search"
99import { FormikErrors , useFormik } from "formik"
10- import { useState } from "react"
10+ import debounce from "just-debounce-it"
11+ import { useCallback , useEffect , useState } from "react"
1112import { getValidationErrorMessage } from "../../api/errors"
1213import { getFormHelpers } from "../../util/formUtils"
1314import { CloseDropdown , OpenDropdown } from "../DropdownArrows/DropdownArrows"
@@ -53,6 +54,23 @@ export const SearchBarWithFilter: React.FC<SearchBarWithFilterProps> = ({
5354 } ,
5455 } )
5556
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+
5674 const getFieldHelpers = getFormHelpers < FilterFormValues > ( form )
5775
5876 const [ anchorEl , setAnchorEl ] = useState < null | HTMLElement > ( null )
0 commit comments