Skip to content

Commit

Permalink
feat: Add loading spinner to search bar
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Sep 21, 2024
1 parent d9f72b9 commit 48f15f4
Showing 1 changed file with 37 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import Box from "@mui/material/Box";
import CircularProgress from "@mui/material/CircularProgress";
import Container from "@mui/material/Container";
import Typography from "@mui/material/Typography";
import { useFormik } from "formik";
import { useSearch } from "hooks/useSearch";
import { debounce } from "lodash";
import { useStore } from "pages/FlowEditor/lib/store";
import React, { useEffect, useMemo } from "react";
import React, { useEffect, useMemo, useState } from "react";
import ChecklistItem from "ui/shared/ChecklistItem";
import Input from "ui/shared/Input";

Expand All @@ -25,6 +26,8 @@ const Search: React.FC = () => {
state.orderedFlow,
state.setOrderedFlow,
]);
const [isSearching, setIsSearching] = useState(false);
const [lastSearchedTerm, setLastSearchedTerm] = useState("");

useEffect(() => {
if (!orderedFlow) setOrderedFlow();
Expand All @@ -46,11 +49,19 @@ const Search: React.FC = () => {
() =>
debounce((pattern: string) => {
console.debug("Search term: ", pattern);
return search(pattern);
search(pattern);
setLastSearchedTerm(pattern);
setIsSearching(false);
}, DEBOUNCE_MS),
[search],
);

useEffect(() => {
if (formik.values.pattern !== lastSearchedTerm) {
setIsSearching(true);
}
}, [formik.values.pattern, lastSearchedTerm]);

return (
<Container component={Box} p={3}>
<form onSubmit={formik.handleSubmit}>
Expand All @@ -63,16 +74,30 @@ const Search: React.FC = () => {
>
Search this flow and internal portals
</Typography>
<Input
id="pattern"
name="pattern"
value={formik.values.pattern}
onChange={(e) => {
formik.setFieldValue("pattern", e.target.value);
formik.handleSubmit();
}}
inputProps={{ spellCheck: false }}
/>
<Box
sx={{ display: "flex", position: "relative", alignItems: "center" }}
>
<Input
id="pattern"
name="pattern"
value={formik.values.pattern}
onChange={(e) => {
formik.setFieldValue("pattern", e.target.value);
formik.handleSubmit();
}}
inputProps={{ spellCheck: false }}
/>
{isSearching && (
<CircularProgress
size={25}
sx={(theme) => ({
position: "absolute",
right: theme.spacing(1.5),
zIndex: 1,
})}
/>
)}
</Box>
<ChecklistItem
label="Search only data fields"
id={"search-data-field-facet"}
Expand Down

0 comments on commit 48f15f4

Please sign in to comment.