Skip to content

Commit f63b8fb

Browse files
committed
Fixed menu closing problem
1 parent ebd9fba commit f63b8fb

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

simple-menu/src/components/AppMenuItem.tsx

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { MenuItem } from '@mui/material'
2+
import { useState } from 'react'
3+
import MessageDialog from './MessageDialog'
24

35
interface AppMenuItemProps {
46
title: string
@@ -13,13 +15,30 @@ interface AppMenuItemProps {
1315
*/
1416
const AppMenuItem = ({
1517
title = 'Simple Menu Example',
18+
handleClose,
1619
}: AppMenuItemProps): JSX.Element => {
20+
const [dialogOpen, setDialogOpen] = useState(false)
21+
22+
const handleCloseDialog = () => {
23+
setDialogOpen(false)
24+
handleClose && handleClose()
25+
}
26+
1727
const handleClick = (): void => {
18-
console.log('Click detected in external app')
19-
alert('Hello from the external app! (AppMenuItem)')
28+
console.log('Click detected in Simple Menu app')
29+
setDialogOpen(true)
2030
}
2131

22-
return <MenuItem onClick={handleClick}>{title}</MenuItem>
32+
return (
33+
<>
34+
<MenuItem onClick={handleClick}>{title}</MenuItem>
35+
<MessageDialog
36+
open={dialogOpen}
37+
onClose={handleCloseDialog}
38+
message="Hello from the Simple Menu app! (AppMenuItem)"
39+
/>
40+
</>
41+
)
2342
}
2443

2544
export default AppMenuItem
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import React from 'react'
2+
import Dialog from '@mui/material/Dialog'
3+
import DialogTitle from '@mui/material/DialogTitle'
4+
import DialogContent from '@mui/material/DialogContent'
5+
import DialogContentText from '@mui/material/DialogContentText'
6+
import DialogActions from '@mui/material/DialogActions'
7+
import Button from '@mui/material/Button'
8+
9+
interface MessageDialogProps {
10+
open: boolean
11+
onClose: () => void
12+
message: string
13+
}
14+
15+
const MessageDialog: React.FC<MessageDialogProps> = ({
16+
open,
17+
onClose,
18+
message,
19+
}) => {
20+
return (
21+
<Dialog open={open} onClose={onClose}>
22+
<DialogTitle>Simple Menu App</DialogTitle>
23+
<DialogContent>
24+
<DialogContentText>{message}</DialogContentText>
25+
</DialogContent>
26+
<DialogActions>
27+
<Button onClick={onClose} color="primary">
28+
Close
29+
</Button>
30+
</DialogActions>
31+
</Dialog>
32+
)
33+
}
34+
35+
export default MessageDialog

0 commit comments

Comments
 (0)