Projekt

Obecné

Profil

Stáhnout (2.14 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
    size?: 'small' | 'medium' | 'large'
16
}
17

    
18

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

    
33
    // Change maxWidth to large if its undefined
34
    maxWidth = maxWidth ?? 'lg'
35

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

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

    
73
export default ButtonOpenableDialog
(1-1/5)