UI: Improved UX in Search Bar
This commit is contained in:
@@ -7,6 +7,10 @@ import {
|
|||||||
useEffect,
|
useEffect,
|
||||||
useRef,
|
useRef,
|
||||||
useMemo,
|
useMemo,
|
||||||
|
ClassAttributes,
|
||||||
|
JSX,
|
||||||
|
Key,
|
||||||
|
LiHTMLAttributes,
|
||||||
} from "react";
|
} from "react";
|
||||||
import IconButton from "@mui/material/IconButton";
|
import IconButton from "@mui/material/IconButton";
|
||||||
import Tooltip from "@mui/material/Tooltip";
|
import Tooltip from "@mui/material/Tooltip";
|
||||||
@@ -31,14 +35,19 @@ export function SearchBar(props: SearchBarProps) {
|
|||||||
let { tableData, setFilteredList } = props;
|
let { tableData, setFilteredList } = props;
|
||||||
const [search, setSearch] = useState<string>("");
|
const [search, setSearch] = useState<string>("");
|
||||||
const debouncedSearch = useDebounce(search, 250);
|
const debouncedSearch = useDebounce(search, 250);
|
||||||
|
const [open, setOpen] = useState(true);
|
||||||
|
|
||||||
// Define a function to handle the Esc key press
|
// Define a function to handle the Esc key press
|
||||||
function handleEsc(event: React.KeyboardEvent<HTMLDivElement>) {
|
function handleEsc(event: React.KeyboardEvent<HTMLDivElement>) {
|
||||||
if (event.key === "Escape") {
|
if (event.key === "Escape") {
|
||||||
console.log("Escape key pressed");
|
|
||||||
setSearch("");
|
setSearch("");
|
||||||
setFilteredList(tableData);
|
setFilteredList(tableData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check if the key is Enter
|
||||||
|
if (event.key === "Enter") {
|
||||||
|
setOpen(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -50,11 +59,12 @@ export function SearchBar(props: SearchBarProps) {
|
|||||||
}
|
}
|
||||||
}, [debouncedSearch, tableData, setFilteredList]);
|
}, [debouncedSearch, tableData, setFilteredList]);
|
||||||
|
|
||||||
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
const handleInputChange = (event: any, value: string) => {
|
||||||
if (e.target.value === "") {
|
if (value === "") {
|
||||||
setFilteredList(tableData);
|
setFilteredList(tableData);
|
||||||
}
|
}
|
||||||
setSearch(e.target.value);
|
|
||||||
|
setSearch(value);
|
||||||
};
|
};
|
||||||
|
|
||||||
const suggestions = useMemo(
|
const suggestions = useMemo(
|
||||||
@@ -65,16 +75,24 @@ export function SearchBar(props: SearchBarProps) {
|
|||||||
return (
|
return (
|
||||||
<Autocomplete
|
<Autocomplete
|
||||||
freeSolo
|
freeSolo
|
||||||
|
autoComplete
|
||||||
options={suggestions}
|
options={suggestions}
|
||||||
|
renderOption={(props: any, option: any) => {
|
||||||
|
return (
|
||||||
|
<li {...props} key={option}>
|
||||||
|
{option}
|
||||||
|
</li>
|
||||||
|
);
|
||||||
|
}}
|
||||||
onKeyDown={handleEsc}
|
onKeyDown={handleEsc}
|
||||||
onChange={(event, value) => {
|
onInputChange={handleInputChange}
|
||||||
// do something with the selected value
|
value={search}
|
||||||
if (value === null) {
|
open={open}
|
||||||
setSearch("");
|
onOpen={() => {
|
||||||
setFilteredList(tableData);
|
setOpen(true);
|
||||||
} else {
|
}}
|
||||||
setSearch(value);
|
onClose={() => {
|
||||||
}
|
setOpen(false);
|
||||||
}}
|
}}
|
||||||
renderInput={(params) => (
|
renderInput={(params) => (
|
||||||
<TextField
|
<TextField
|
||||||
@@ -82,26 +100,18 @@ export function SearchBar(props: SearchBarProps) {
|
|||||||
fullWidth
|
fullWidth
|
||||||
label="Search"
|
label="Search"
|
||||||
variant="outlined"
|
variant="outlined"
|
||||||
value={search}
|
|
||||||
onChange={handleInputChange}
|
|
||||||
autoComplete="nickname"
|
autoComplete="nickname"
|
||||||
InputProps={{
|
InputProps={{
|
||||||
...params.InputProps,
|
...params.InputProps,
|
||||||
// endAdornment: (
|
startAdornment: (
|
||||||
// <InputAdornment position="end">
|
<InputAdornment position="start">
|
||||||
// <IconButton>
|
<IconButton>
|
||||||
// <SearchIcon />
|
<SearchIcon />
|
||||||
// </IconButton>
|
</IconButton>
|
||||||
// </InputAdornment>
|
</InputAdornment>
|
||||||
// ),
|
),
|
||||||
}}
|
}}
|
||||||
>
|
></TextField>
|
||||||
{/* {suggestions.map((item, index) => (
|
|
||||||
<option key={index} onClick={() => handleSelect(item)}>
|
|
||||||
{item}
|
|
||||||
</option>
|
|
||||||
))} */}
|
|
||||||
</TextField>
|
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user