From 2bcdce0929062a287134f93888c7773f85a9ba9e Mon Sep 17 00:00:00 2001 From: Sarah Wang Date: Wed, 14 Aug 2024 21:12:56 -0400 Subject: [PATCH] refactor menu component --- demo/src/MemoryModelsMenu.tsx | 42 +++++++++ demo/src/MemoryModelsSample.tsx | 50 +++------- demo/src/MemoryModelsUserInput.tsx | 103 +++++++++------------ demo/src/{index.d.ts => declarations.d.ts} | 0 4 files changed, 98 insertions(+), 97 deletions(-) create mode 100644 demo/src/MemoryModelsMenu.tsx rename demo/src/{index.d.ts => declarations.d.ts} (100%) diff --git a/demo/src/MemoryModelsMenu.tsx b/demo/src/MemoryModelsMenu.tsx new file mode 100644 index 00000000..2638a2c5 --- /dev/null +++ b/demo/src/MemoryModelsMenu.tsx @@ -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); + const open = Boolean(anchorEl); + const handleClick = (event: React.MouseEvent) => { + setAnchorEl(event.currentTarget); + }; + const handleClose = () => { + setAnchorEl(null); + }; + + return ( + <> + + + {props.menuItems} + + + ); +} diff --git a/demo/src/MemoryModelsSample.tsx b/demo/src/MemoryModelsSample.tsx index 39e1c75d..04a89ff8 100644 --- a/demo/src/MemoryModelsSample.tsx +++ b/demo/src/MemoryModelsSample.tsx @@ -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"; @@ -12,14 +12,6 @@ type MemoryModelsSamplePropTypes = { export default function MemoryModelsSample(props: MemoryModelsSamplePropTypes) { const [clickedBtnIndex, setClickedBtnIndex] = useState(null); - const [anchorEl, setAnchorEl] = useState(null); - const open = Boolean(anchorEl); - const handleClick = (event: React.MouseEvent) => { - setAnchorEl(event.currentTarget); - }; - const handleClose = () => { - setAnchorEl(null); - }; useEffect(() => { if (clickedBtnIndex !== null) { @@ -40,31 +32,17 @@ export default function MemoryModelsSample(props: MemoryModelsSamplePropTypes) { }; return ( - <> - - - {SAMPLES.map((sample, index) => ( - handleButtonClick(index, sample)} - > - {sample["name"]} - - ))} - - + ( + handleButtonClick(index, sample)} + > + {sample["name"]} + + ))} + /> ); } diff --git a/demo/src/MemoryModelsUserInput.tsx b/demo/src/MemoryModelsUserInput.tsx index 6855f504..4c66b45d 100644 --- a/demo/src/MemoryModelsUserInput.tsx +++ b/demo/src/MemoryModelsUserInput.tsx @@ -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; @@ -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); - const open = Boolean(anchorEl); - const handleClick = (event: React.MouseEvent) => { - setAnchorEl(event.currentTarget); - }; - const handleClose = () => { - setAnchorEl(null); - }; const handleSeedChange = (event) => { event.preventDefault(); props.setConfigData({ @@ -149,57 +140,47 @@ function MemoryModelsConfigInput(props: MemoryModelsConfigInputPropTypes) { }; return ( - <> - - - - - - - - } - label="Use automatic layout" - sx={{ width: "50%" }} - /> - - - + + + + + + + } + label="Use automatic layout" + sx={{ width: "50%" }} + /> + + + } + /> ); } diff --git a/demo/src/index.d.ts b/demo/src/declarations.d.ts similarity index 100% rename from demo/src/index.d.ts rename to demo/src/declarations.d.ts