Skip to content
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

63 the search dialog results don't close when a item is selected #85

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 27 additions & 30 deletions src/components/HeaderBar.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import type { FoodList, FoodObject } from "../types/food";
import Fuse from 'fuse.js'
import { useRef, useEffect } from "react"
import Fuse from "fuse.js";
import { useRef, useEffect } from "react";
import i18next from "i18next";
import { useTranslation } from "react-i18next";
import { Menu, Search } from "@mui/icons-material";
import {
AppBar,
styled,
alpha,
Toolbar,
IconButton,
InputBase
InputBase,
Box,
} from "@mui/material";

type Props = {
Expand Down Expand Up @@ -45,17 +45,23 @@ export default function HeaderBar(props: Props) {
const options = {
threshold: 0.3,
keys: [
{ name: 'name-en', getFn: (food: FoodObject) => food.description[0].name },
{ name: 'name-it', getFn: (food: FoodObject) => food.description[1].name }
]
}
{
name: "name-en",
getFn: (food: FoodObject) => food.description[0].name,
},
{
name: "name-it",
getFn: (food: FoodObject) => food.description[1].name,
},
],
};

const fuse = new Fuse(food, options)
const searchText = query.current.value.trim().toLowerCase()
const searchLanguage: { [lang: string]: string } = {}
searchLanguage["name-" + i18next.language] = searchText
const fuse = new Fuse(food, options);
const searchText = query.current.value.trim().toLowerCase();
const searchLanguage: { [lang: string]: string } = {};
searchLanguage["name-" + i18next.language] = searchText;

return fuse.search(searchLanguage).map((i) => i.item)
return fuse.search(searchLanguage).map((i) => i.item);
};

function leftButton() {
Expand All @@ -72,20 +78,6 @@ export default function HeaderBar(props: Props) {
);
}

const SearchBar = styled("div")(({ theme }) => ({
position: "relative",
borderRadius: theme.shape.borderRadius,
backgroundColor: alpha(theme.palette.common.white, 0.15),
"&:hover": {
backgroundColor: alpha(theme.palette.common.white, 0.25),
},
"&:focus": {
backgroundColor: alpha(theme.palette.common.white, 0.25),
},
marginLeft: "auto",
width: "auto",
}));

const SearchIconWrapper = styled("div")(() => ({
height: "100%",
aspectRatio: 1,
Expand Down Expand Up @@ -114,7 +106,12 @@ export default function HeaderBar(props: Props) {
<AppBar position="static" sx={{ mb: 1, backgroundColor: "primary.dark" }}>
<Toolbar>
{leftButton()}
<SearchBar>
<Box
position="relative"
marginLeft="auto"
borderRadius=".5rem"
bgcolor="rgba(255,255,255,.1)"
>
<SearchIconWrapper>
<IconButton
sx={{ m: 0 }}
Expand All @@ -128,15 +125,15 @@ export default function HeaderBar(props: Props) {
</SearchIconWrapper>
<form onSubmit={(e) => handleSubmit(e)}>
<StyledInputBase
key="searchInput"
placeholder={t("Header_searchBar")}
inputProps={{ "aria-label": "search" }}
inputRef={query}
id="search-bar"
/>
</form>
</SearchBar>
</Box>
</Toolbar>
</AppBar>
);
}

4 changes: 2 additions & 2 deletions src/components/RenderFoods.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ interface RenderFoodProps {
//render the grid of foods
const RenderFoods:FunctionComponent<RenderFoodProps> = (props:RenderFoodProps) => {
const foodList = props.foodList
const foodItems = foodList.map((item ) => {
const foodItems = foodList.map((item, key) => {
return (
<Item {...item} />
<Item key={key} {...item} />
);
});
return (
Expand Down
2 changes: 1 addition & 1 deletion src/components/SearchResult.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ function SearchResult(props: Props) {
maxWidth="md"
open={ifSearched}
onClose={closeModal}
onClick={closeModal}
aria-labelledby="search results"
aria-describedby="the results of your search are shown here"
TransitionComponent={Transition}
Expand All @@ -51,7 +52,6 @@ function SearchResult(props: Props) {
sx={{ml: "auto" }}
edge="start"
color="inherit"
onClick={closeModal}
aria-label="close"
>
<Close />
Expand Down