1 |
0d90d67b
|
Vaclav Honzik
|
import { Button, Dialog } from '@mui/material'
|
2 |
|
|
import { Fragment, FunctionComponent, ReactNode, useState } from 'react'
|
3 |
|
|
|
4 |
|
|
export interface ButtonOpenableDialogProps {
|
5 |
|
|
onOpenCallback?: () => void // this callback is always executed when the dialog is opened
|
6 |
|
|
onCloseCallback?: () => void // this callback is always executed when the dialog is closed
|
7 |
|
|
buttonText: string // the text of the button that opens the dialog
|
8 |
|
|
buttonColor: // the color of the button that opens the dialog
|
9 |
|
|
'primary' | 'secondary' | 'warning' | 'error' | 'success' | 'inherit'
|
10 |
|
|
buttonVariant: 'text' | 'outlined' | 'contained' // the variant of the button that opens the dialog
|
11 |
|
|
children: ReactNode // the content of the dialog
|
12 |
|
|
maxWidth?: 'xs' | 'sm' | 'md' | 'lg' // the max width of the dialog
|
13 |
c0b66eaf
|
Vaclav Honzik
|
open: boolean
|
14 |
7aa6893d
|
Vaclav Honzik
|
setOpen: (open: boolean) => void
|
15 |
f41a4cd3
|
Vaclav Honzik
|
size?: 'small' | 'medium' | 'large'
|
16 |
c0b66eaf
|
Vaclav Honzik
|
startIcon?: ReactNode // the icon to the left of the button
|
17 |
|
|
endIcon?: ReactNode // the icon to the right of the button
|
18 |
0d90d67b
|
Vaclav Honzik
|
}
|
19 |
|
|
|
20 |
|
|
// Generic dialog that can be opened by a button and closed by clicking on the backdrop.
|
21 |
|
|
const ButtonOpenableDialog: FunctionComponent<ButtonOpenableDialogProps> = ({
|
22 |
|
|
onOpenCallback,
|
23 |
|
|
onCloseCallback,
|
24 |
|
|
buttonText,
|
25 |
|
|
buttonColor,
|
26 |
|
|
buttonVariant,
|
27 |
|
|
children,
|
28 |
|
|
maxWidth,
|
29 |
7aa6893d
|
Vaclav Honzik
|
open,
|
30 |
f41a4cd3
|
Vaclav Honzik
|
setOpen,
|
31 |
c0b66eaf
|
Vaclav Honzik
|
size,
|
32 |
|
|
startIcon,
|
33 |
|
|
endIcon,
|
34 |
0d90d67b
|
Vaclav Honzik
|
}) => {
|
35 |
|
|
// Change maxWidth to large if its undefined
|
36 |
|
|
maxWidth = maxWidth ?? 'lg'
|
37 |
|
|
|
38 |
|
|
const onOpen = () => {
|
39 |
|
|
if (onOpenCallback) {
|
40 |
|
|
// execute the callback if exists
|
41 |
|
|
onOpenCallback()
|
42 |
|
|
}
|
43 |
|
|
setOpen(true)
|
44 |
|
|
}
|
45 |
|
|
const onClose = () => {
|
46 |
|
|
if (onCloseCallback) {
|
47 |
|
|
// execute the callback if exists
|
48 |
|
|
onCloseCallback()
|
49 |
|
|
}
|
50 |
|
|
setOpen(false)
|
51 |
|
|
}
|
52 |
|
|
|
53 |
|
|
return (
|
54 |
|
|
<Fragment>
|
55 |
|
|
<Button
|
56 |
|
|
onClick={onOpen}
|
57 |
|
|
color={buttonColor}
|
58 |
|
|
variant={buttonVariant}
|
59 |
f41a4cd3
|
Vaclav Honzik
|
size={size ?? 'medium'}
|
60 |
c0b66eaf
|
Vaclav Honzik
|
startIcon={startIcon}
|
61 |
|
|
endIcon={endIcon}
|
62 |
0d90d67b
|
Vaclav Honzik
|
>
|
63 |
|
|
{buttonText}
|
64 |
|
|
</Button>
|
65 |
c0b66eaf
|
Vaclav Honzik
|
<Dialog fullWidth open={open} onClose={onClose} maxWidth={maxWidth}>
|
66 |
0d90d67b
|
Vaclav Honzik
|
{children}
|
67 |
|
|
</Dialog>
|
68 |
|
|
</Fragment>
|
69 |
|
|
)
|
70 |
|
|
}
|
71 |
|
|
|
72 |
|
|
export default ButtonOpenableDialog
|