@@ -20,11 +20,11 @@ import {
20
20
} from "api/errors" ;
21
21
import { useFilterMenu } from "./menu" ;
22
22
import { BaseOption } from "./options" ;
23
- import debounce from "just-debounce-it" ;
24
23
import MenuList from "@mui/material/MenuList" ;
25
24
import { Loader } from "components/Loader/Loader" ;
26
25
import Divider from "@mui/material/Divider" ;
27
26
import OpenInNewOutlined from "@mui/icons-material/OpenInNewOutlined" ;
27
+ import { useDebouncedFunction } from "hooks/debounce" ;
28
28
29
29
export type PresetFilter = {
30
30
name : string ;
@@ -58,7 +58,7 @@ export const useFilter = ({
58
58
}
59
59
} ;
60
60
61
- const debounceUpdate = debounce (
61
+ const { debounced : debounceUpdate , cancelDebounce } = useDebouncedFunction (
62
62
( values : string | FilterValues ) => update ( values ) ,
63
63
500 ,
64
64
) ;
@@ -69,6 +69,7 @@ export const useFilter = ({
69
69
query,
70
70
update,
71
71
debounceUpdate,
72
+ cancelDebounce,
72
73
values,
73
74
used,
74
75
} ;
@@ -153,19 +154,23 @@ export const Filter = ({
153
154
learnMoreLink2,
154
155
presets,
155
156
} : FilterProps ) => {
156
- const shouldDisplayError = hasError ( error ) && isApiValidationError ( error ) ;
157
- const hasFilterQuery = filter . query !== "" ;
158
157
const [ searchQuery , setSearchQuery ] = useState ( filter . query ) ;
159
- const inputRef = useRef < HTMLInputElement > ( null ) ;
158
+ const textboxInputRef = useRef < HTMLInputElement > ( null ) ;
160
159
161
160
useEffect ( ( ) => {
162
- // We don't want to update this while the user is typing something or has the focus in the input
163
- const isFocused = document . activeElement === inputRef . current ;
164
- if ( ! isFocused ) {
161
+ // We don't want to update this while the user is typing something or has
162
+ // the focus in the input
163
+ const hasInnerFocus =
164
+ textboxInputRef . current ?. contains ( document . activeElement ) ?? false ;
165
+
166
+ if ( ! hasInnerFocus ) {
165
167
setSearchQuery ( filter . query ) ;
166
168
}
167
169
} , [ filter . query ] ) ;
168
170
171
+ const shouldDisplayError = hasError ( error ) && isApiValidationError ( error ) ;
172
+ const hasFilterQuery = filter . query !== "" ;
173
+
169
174
return (
170
175
< Box
171
176
sx = { {
@@ -201,7 +206,7 @@ export const Filter = ({
201
206
name : "query" ,
202
207
placeholder : "Search..." ,
203
208
value : searchQuery ,
204
- ref : inputRef ,
209
+ ref : textboxInputRef ,
205
210
onChange : ( e ) => {
206
211
setSearchQuery ( e . target . value ) ;
207
212
filter . debounceUpdate ( e . target . value ) ;
0 commit comments