Skip to content

fix: prevent workspace search bar text from getting garbled #9703

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Sep 15, 2023
Prev Previous commit
Next Next commit
fix: Update focus logic to apply for any inner focus
  • Loading branch information
Parkreiner committed Sep 14, 2023
commit 2d4d2857449c2e92a11a94f31f875a799559e881
23 changes: 14 additions & 9 deletions site/src/components/Filter/filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import {
} from "api/errors";
import { useFilterMenu } from "./menu";
import { BaseOption } from "./options";
import debounce from "just-debounce-it";
import MenuList from "@mui/material/MenuList";
import { Loader } from "components/Loader/Loader";
import Divider from "@mui/material/Divider";
import OpenInNewOutlined from "@mui/icons-material/OpenInNewOutlined";
import { useDebouncedFunction } from "hooks/debounce";

export type PresetFilter = {
name: string;
Expand Down Expand Up @@ -58,7 +58,7 @@ export const useFilter = ({
}
};

const debounceUpdate = debounce(
const { debounced: debounceUpdate, cancelDebounce } = useDebouncedFunction(
(values: string | FilterValues) => update(values),
500,
);
Expand All @@ -69,6 +69,7 @@ export const useFilter = ({
query,
update,
debounceUpdate,
cancelDebounce,
values,
used,
};
Expand Down Expand Up @@ -153,19 +154,23 @@ export const Filter = ({
learnMoreLink2,
presets,
}: FilterProps) => {
const shouldDisplayError = hasError(error) && isApiValidationError(error);
const hasFilterQuery = filter.query !== "";
const [searchQuery, setSearchQuery] = useState(filter.query);
const inputRef = useRef<HTMLInputElement>(null);
const textboxInputRef = useRef<HTMLInputElement>(null);

useEffect(() => {
// We don't want to update this while the user is typing something or has the focus in the input
const isFocused = document.activeElement === inputRef.current;
if (!isFocused) {
// We don't want to update this while the user is typing something or has
// the focus in the input
const hasInnerFocus =
textboxInputRef.current?.contains(document.activeElement) ?? false;

if (!hasInnerFocus) {
setSearchQuery(filter.query);
}
}, [filter.query]);

const shouldDisplayError = hasError(error) && isApiValidationError(error);
const hasFilterQuery = filter.query !== "";

return (
<Box
sx={{
Expand Down Expand Up @@ -201,7 +206,7 @@ export const Filter = ({
name: "query",
placeholder: "Search...",
value: searchQuery,
ref: inputRef,
ref: textboxInputRef,
onChange: (e) => {
setSearchQuery(e.target.value);
filter.debounceUpdate(e.target.value);
Expand Down