Skip to content

Commit

Permalink
refactor menu component
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahsonder committed Aug 15, 2024
1 parent 5ab0898 commit 2bcdce0
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 97 deletions.
42 changes: 42 additions & 0 deletions demo/src/MemoryModelsMenu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React, { ReactNode, useState } from "react";
import { Box, Button, Menu } from "@mui/material";
import ExpandMoreRoundedIcon from "@mui/icons-material/ExpandMoreRounded";

type MemoryModelsMenuPropTypes = {
menuName: string;
testId: string;
menuItems: ReactNode;
};

export default function MemoryModelsMenu(props: MemoryModelsMenuPropTypes) {
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
const open = Boolean(anchorEl);
const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
setAnchorEl(event.currentTarget);
};
const handleClose = () => {
setAnchorEl(null);
};

return (
<>
<Button
onClick={handleClick}
data-testid={props.testId}
sx={{
textTransform: "none",
"& .MuiSvgIcon-root": {
transition: "transform 0.2s ease-in-out",
transform: open ? "rotate(180deg)" : "rotate(0deg)",
},
}}
>
{props.menuName}
<ExpandMoreRoundedIcon />
</Button>
<Menu anchorEl={anchorEl} open={open} onClose={handleClose}>
<Box>{props.menuItems}</Box>
</Menu>
</>
);
}
50 changes: 14 additions & 36 deletions demo/src/MemoryModelsSample.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect, useState } from "react";
import { Button, Menu, MenuItem } from "@mui/material";
import ExpandMoreRoundedIcon from "@mui/icons-material/ExpandMoreRounded";
import { Box, MenuItem } from "@mui/material";
import MemoryModelsMenu from "./MemoryModelsMenu";

import { SAMPLES } from "./sample";

Expand All @@ -12,14 +12,6 @@ type MemoryModelsSamplePropTypes = {

export default function MemoryModelsSample(props: MemoryModelsSamplePropTypes) {
const [clickedBtnIndex, setClickedBtnIndex] = useState<Number>(null);
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
const open = Boolean(anchorEl);
const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
setAnchorEl(event.currentTarget);
};
const handleClose = () => {
setAnchorEl(null);
};

useEffect(() => {
if (clickedBtnIndex !== null) {
Expand All @@ -40,31 +32,17 @@ export default function MemoryModelsSample(props: MemoryModelsSamplePropTypes) {
};

return (
<>
<Button
onClick={handleClick}
data-testid="sample-inputs-menu"
sx={{
textTransform: "none",
"& .MuiSvgIcon-root": {
transition: "transform 0.2s ease-in-out",
transform: open ? "rotate(180deg)" : "rotate(0deg)",
},
}}
>
Sample Inputs
<ExpandMoreRoundedIcon />
</Button>
<Menu anchorEl={anchorEl} open={open} onClose={handleClose}>
{SAMPLES.map((sample, index) => (
<MenuItem
key={index}
onClick={() => handleButtonClick(index, sample)}
>
{sample["name"]}
</MenuItem>
))}
</Menu>
</>
<MemoryModelsMenu
menuName="Sample Inputs"
testId="sample-inputs-menu"
menuItems={SAMPLES.map((sample, index) => (
<MenuItem
key={index}
onClick={() => handleButtonClick(index, sample)}
>
{sample["name"]}
</MenuItem>
))}
/>
);
}
103 changes: 42 additions & 61 deletions demo/src/MemoryModelsUserInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import {
Input,
TextField,
Tooltip,
Menu,
MenuItem,
} from "@mui/material";
import ExpandMoreRoundedIcon from "@mui/icons-material/ExpandMoreRounded";
import DownloadJSONButton from "./DownloadJSONButton";
import MemoryModelsMenu from "./MemoryModelsMenu";

interface configDataPropTypes {
useAutomation: boolean;
Expand Down Expand Up @@ -119,14 +118,6 @@ function MemoryModelsTextInput(props: MemoryModelsTextInputPropTypes) {

//TODO: Retrieve min and max seeds from memory-viz
function MemoryModelsConfigInput(props: MemoryModelsConfigInputPropTypes) {
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
const open = Boolean(anchorEl);
const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
setAnchorEl(event.currentTarget);
};
const handleClose = () => {
setAnchorEl(null);
};
const handleSeedChange = (event) => {
event.preventDefault();
props.setConfigData({
Expand All @@ -149,57 +140,47 @@ function MemoryModelsConfigInput(props: MemoryModelsConfigInputPropTypes) {
};

return (
<>
<Button
onClick={handleClick}
data-testid="rendering-options-menu"
sx={{
textTransform: "none",
"& .MuiSvgIcon-root": {
transition: "transform 0.2s ease-in-out",
transform: open ? "rotate(180deg)" : "rotate(0deg)",
},
}}
>
Rendering Options
<ExpandMoreRoundedIcon />
</Button>
<Menu anchorEl={anchorEl} open={open} onClose={handleClose}>
<MenuItem>
<TextField
label="Seed"
id="config-seed"
variant="outlined"
value={props.configData.overallDrawConfig.seed}
type="number"
onChange={handleSeedChange}
InputProps={{
inputProps: {
min: 0,
max: 2 ** 31,
"data-testid": "config-seed",
},
}}
sx={{
width: "50%",
"& .MuiInputBase-input": { height: "10%" },
}}
/>
</MenuItem>
<MenuItem>
<FormControlLabel
control={
<Checkbox
checked={props.configData.useAutomation}
onChange={handleAutomationChange}
/>
}
label="Use automatic layout"
sx={{ width: "50%" }}
/>
</MenuItem>
</Menu>
</>
<MemoryModelsMenu
menuName="Rendering Options"
testId="rendering-options-menu"
menuItems={
<>
<MenuItem>
<TextField
label="Seed"
id="config-seed"
variant="outlined"
value={props.configData.overallDrawConfig.seed}
type="number"
onChange={handleSeedChange}
InputProps={{
inputProps: {
min: 0,
max: 2 ** 31,
"data-testid": "config-seed",
},
}}
sx={{
width: "50%",
"& .MuiInputBase-input": { height: "10%" },
}}
/>
</MenuItem>
<MenuItem>
<FormControlLabel
control={
<Checkbox
checked={props.configData.useAutomation}
onChange={handleAutomationChange}
/>
}
label="Use automatic layout"
sx={{ width: "50%" }}
/>
</MenuItem>
</>
}
/>
);
}

Expand Down
File renamed without changes.

0 comments on commit 2bcdce0

Please sign in to comment.