Projekt

Obecné

Profil

Stáhnout (2.02 KB) Statistiky
| Větev: | Tag: | Revize:
1
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
}
14

    
15

    
16
// Generic dialog that can be opened by a button and closed by clicking on the backdrop.
17
const ButtonOpenableDialog: FunctionComponent<ButtonOpenableDialogProps> = ({
18
    onOpenCallback,
19
    onCloseCallback,
20
    buttonText,
21
    buttonColor,
22
    buttonVariant,
23
    children,
24
    maxWidth,
25
}) => {
26
    const [open, setOpen] = useState(false)
27

    
28
    // Change maxWidth to large if its undefined
29
    maxWidth = maxWidth ?? 'lg'
30

    
31
    const onOpen = () => {
32
        if (onOpenCallback) {
33
            // execute the callback if exists
34
            onOpenCallback()
35
        }
36
        setOpen(true)
37
    }
38
    const onClose = () => {
39
        if (onCloseCallback) {
40
            // execute the callback if exists
41
            onCloseCallback()
42
        }
43
        setOpen(false)
44
    }
45

    
46
    return (
47
        <Fragment>
48
            <Button
49
                onClick={onOpen}
50
                color={buttonColor}
51
                variant={buttonVariant}
52
            >
53
                {buttonText}
54
            </Button>
55
            <Dialog
56
                fullWidth
57
                open={open}
58
                onClose={onClose}
59
                maxWidth={maxWidth}
60
            >
61
                {children}
62
            </Dialog>
63
        </Fragment>
64
    )
65
}
66

    
67
export default ButtonOpenableDialog
(1-1/5)