Projekt

Obecné

Profil

Stáhnout (2.28 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
    startIcon?: ReactNode // the icon to the left of the button
17
    endIcon?: ReactNode // the icon to the right of the button
18
}
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
    open,
30
    setOpen,
31
    size,
32
    startIcon,
33
    endIcon,
34
}) => {
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
                size={size ?? 'medium'}
60
                startIcon={startIcon}
61
                endIcon={endIcon}
62
            >
63
                {buttonText}
64
            </Button>
65
            <Dialog fullWidth open={open} onClose={onClose} maxWidth={maxWidth}>
66
                {children}
67
            </Dialog>
68
        </Fragment>
69
    )
70
}
71

    
72
export default ButtonOpenableDialog
(1-1/5)