Skip to content

Commit 2d4d285

Browse files
committed
fix: Update focus logic to apply for any inner focus
1 parent fec4384 commit 2d4d285

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

site/src/components/Filter/filter.tsx

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ import {
2020
} from "api/errors";
2121
import { useFilterMenu } from "./menu";
2222
import { BaseOption } from "./options";
23-
import debounce from "just-debounce-it";
2423
import MenuList from "@mui/material/MenuList";
2524
import { Loader } from "components/Loader/Loader";
2625
import Divider from "@mui/material/Divider";
2726
import OpenInNewOutlined from "@mui/icons-material/OpenInNewOutlined";
27+
import { useDebouncedFunction } from "hooks/debounce";
2828

2929
export type PresetFilter = {
3030
name: string;
@@ -58,7 +58,7 @@ export const useFilter = ({
5858
}
5959
};
6060

61-
const debounceUpdate = debounce(
61+
const { debounced: debounceUpdate, cancelDebounce } = useDebouncedFunction(
6262
(values: string | FilterValues) => update(values),
6363
500,
6464
);
@@ -69,6 +69,7 @@ export const useFilter = ({
6969
query,
7070
update,
7171
debounceUpdate,
72+
cancelDebounce,
7273
values,
7374
used,
7475
};
@@ -153,19 +154,23 @@ export const Filter = ({
153154
learnMoreLink2,
154155
presets,
155156
}: FilterProps) => {
156-
const shouldDisplayError = hasError(error) && isApiValidationError(error);
157-
const hasFilterQuery = filter.query !== "";
158157
const [searchQuery, setSearchQuery] = useState(filter.query);
159-
const inputRef = useRef<HTMLInputElement>(null);
158+
const textboxInputRef = useRef<HTMLInputElement>(null);
160159

161160
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) {
165167
setSearchQuery(filter.query);
166168
}
167169
}, [filter.query]);
168170

171+
const shouldDisplayError = hasError(error) && isApiValidationError(error);
172+
const hasFilterQuery = filter.query !== "";
173+
169174
return (
170175
<Box
171176
sx={{
@@ -201,7 +206,7 @@ export const Filter = ({
201206
name: "query",
202207
placeholder: "Search...",
203208
value: searchQuery,
204-
ref: inputRef,
209+
ref: textboxInputRef,
205210
onChange: (e) => {
206211
setSearchQuery(e.target.value);
207212
filter.debounceUpdate(e.target.value);

0 commit comments

Comments
 (0)