Projekt

Obecné

Profil

Stáhnout (2.05 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
    open: boolean,
14
    setOpen: (open: boolean) => void
15
}
16

    
17

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

    
31
    // Change maxWidth to large if its undefined
32
    maxWidth = maxWidth ?? 'lg'
33

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

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

    
70
export default ButtonOpenableDialog
(1-1/5)