-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathDialog.jsx
More file actions
130 lines (121 loc) · 3.86 KB
/
Dialog.jsx
File metadata and controls
130 lines (121 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import React, { useState } from "react";
import ReactMarkdown from "react-markdown";
import rehypeRaw from "rehype-raw";
import gfm from "remark-gfm";
import {
Button,
Dialog,
DialogActions,
DialogContent,
DialogTitle,
TextField,
} from "@mui/material";
import { useMediaQuery, useTheme } from "@mui/material";
import LegacyNonMarkdownRenderer from "./LegacyNonMarkdownRenderer";
import { customComponentsForReactMarkdown } from "../../utils/customComponentsForReactMarkdown";
export default function ResponsiveDialog(props) {
const theme = useTheme();
const fullScreen = useMediaQuery(theme.breakpoints.down("xs"));
const {
children,
onAbort,
onClose,
onVisibilityChanged,
open,
options: {
abortText,
allowDangerousHtml, // ReactMarkdown disables HTML by default but we let the Admin decide
buttonText,
headerText,
primaryButtonVariant,
prompt,
text,
useLegacyNonMarkdownRenderer, // Admin can choose to pass-by the ReactMarkdown and just use dangerouslySetInnerHtml
},
} = props;
// Will hold a return value for those Dialogs that are ment to be
// used as prompt input fields.
const [promptText, setPromptText] = useState("");
const rehypePlugins = allowDangerousHtml === true ? [rehypeRaw] : [];
const handleAbort = () => {
onAbort(promptText);
};
const handleClose = () => {
onClose(promptText);
};
// This mechanism allows us to propagate the current
// visibility state of the dialog and send back, using a callback
// function, to parent component.
typeof onVisibilityChanged === "function" && onVisibilityChanged(open);
return (
open && (
<Dialog
sx={{
zIndex: props.type === "Information" ? 1301 : undefined,
}}
aria-labelledby="responsive-dialog-title"
aria-describedby="responsive-dialog-content"
fullScreen={fullScreen}
onClose={handleClose}
open={open}
// Must stop event-bubbling. Otherwise the parent element in react can be dragged etc.
onMouseDown={(e) => {
e.stopPropagation();
}}
>
{headerText && (
<DialogTitle id="responsive-dialog-title">{headerText}</DialogTitle>
)}
<DialogContent id="responsive-dialog-content">
{children}
{useLegacyNonMarkdownRenderer === true ? (
<LegacyNonMarkdownRenderer text={text} />
) : (
<ReactMarkdown
remarkPlugins={[gfm]} // GitHub Formatted Markdown adds support for Tables in MD
rehypePlugins={rehypePlugins} // Needed to parse HTML, activated in admin
components={customComponentsForReactMarkdown} // Custom renderers for components, see definition in components
children={text} // Our MD, as a text string
/>
)}
{prompt && (
<form
noValidate
autoComplete="off"
onSubmit={(e) => {
e.preventDefault();
props.onClose(promptText);
return false;
}}
>
<TextField
id="prompt-text"
label=""
value={promptText}
onChange={(e) => {
setPromptText(e.target.value);
}}
margin="normal"
autoFocus={true}
/>
</form>
)}
</DialogContent>
<DialogActions>
<Button
onClick={handleClose}
variant={primaryButtonVariant || "text"}
color="primary"
>
{buttonText}
</Button>
{abortText && (
<Button onClick={handleAbort} sx={{ color: "text.primary" }}>
{abortText}
</Button>
)}
</DialogActions>
</Dialog>
)
);
}