-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add search to Team list (#4174)
Co-authored-by: Ian Jones <51156018+ianjon3s@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
150 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import ClearIcon from "@mui/icons-material/Clear"; | ||
import Box from "@mui/material/Box"; | ||
import CircularProgress from "@mui/material/CircularProgress"; | ||
import IconButton from "@mui/material/IconButton"; | ||
import { useFormik } from "formik"; | ||
import { FuseOptionKey } from "fuse.js"; | ||
import { useSearch } from "hooks/useSearch"; | ||
import { debounce } from "lodash"; | ||
import React, { useEffect, useMemo, useState } from "react"; | ||
|
||
import { SEARCH_DEBOUNCE_MS } from "../constants"; | ||
import Input from "../Input/Input"; | ||
import InputRow from "../InputRow"; | ||
import InputRowItem from "../InputRowItem"; | ||
import InputRowLabel from "../InputRowLabel"; | ||
|
||
interface SearchBoxProps<T> { | ||
records: T[] | null; | ||
setRecords: React.Dispatch<React.SetStateAction<T[] | null>>; | ||
searchKey: FuseOptionKey<T>[]; | ||
} | ||
|
||
export const SearchBox = <T extends object>({ | ||
records, | ||
setRecords, | ||
searchKey, | ||
}: SearchBoxProps<T>) => { | ||
const [isSearching, setIsSearching] = useState(false); | ||
const [searchedTerm, setSearchedTerm] = useState<string>(); | ||
|
||
const searchKeys = useMemo(() => searchKey, []); | ||
|
||
const formik = useFormik({ | ||
initialValues: { pattern: "" }, | ||
onSubmit: ({ pattern }) => { | ||
setIsSearching(true); | ||
debouncedSearch(pattern); | ||
}, | ||
}); | ||
|
||
const { results, search } = useSearch({ | ||
list: records || [], | ||
keys: searchKeys, | ||
}); | ||
|
||
const debouncedSearch = useMemo( | ||
() => | ||
debounce((pattern: string) => { | ||
search(pattern); | ||
setSearchedTerm(pattern); | ||
setIsSearching(false); | ||
}, SEARCH_DEBOUNCE_MS), | ||
[search], | ||
); | ||
|
||
useEffect(() => { | ||
if (results && searchedTerm) { | ||
const mappedResults = results.map((result) => result.item); | ||
setRecords(mappedResults); | ||
} | ||
if (results && !searchedTerm) { | ||
records && setRecords(records); | ||
} | ||
}, [results, setRecords, searchedTerm, records]); | ||
|
||
return ( | ||
<Box maxWidth={360}> | ||
<InputRow> | ||
<InputRowLabel> | ||
<strong>Search</strong> | ||
</InputRowLabel> | ||
<InputRowItem> | ||
<Box sx={{ position: "relative" }}> | ||
<Input | ||
sx={{ | ||
pr: 5, | ||
}} | ||
name="search" | ||
id="search" | ||
value={formik.values.pattern} | ||
onChange={(e) => { | ||
formik.setFieldValue("pattern", e.target.value); | ||
formik.submitForm(); | ||
}} | ||
/> | ||
{searchedTerm && !isSearching && ( | ||
<IconButton | ||
aria-label="clear search" | ||
onClick={() => { | ||
formik.setFieldValue("pattern", ""); | ||
formik.submitForm(); | ||
}} | ||
size="small" | ||
sx={{ | ||
position: "absolute", | ||
right: (theme) => theme.spacing(1), | ||
top: "50%", | ||
transform: "translateY(-50%)", | ||
padding: 0.5, | ||
zIndex: 1, | ||
}} | ||
> | ||
<ClearIcon fontSize="small" /> | ||
</IconButton> | ||
)} | ||
{isSearching && ( | ||
<IconButton | ||
aria-label="clear search" | ||
onClick={() => { | ||
formik.setFieldValue("pattern", ""); | ||
formik.submitForm(); | ||
}} | ||
size="small" | ||
sx={{ | ||
position: "absolute", | ||
right: (theme) => theme.spacing(1), | ||
top: "50%", | ||
transform: "translateY(-50%)", | ||
padding: 0.5, | ||
zIndex: 1, | ||
}} | ||
> | ||
<CircularProgress size={"1.5rem"} /> | ||
</IconButton> | ||
)} | ||
</Box> | ||
</InputRowItem> | ||
</InputRow> | ||
</Box> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const SEARCH_DEBOUNCE_MS = 500; |