Repository navigation
Expand file tree
/
Copy pathSimpleDialog.tsx
More file actions
160 lines (153 loc) · 6.95 KB
/
Copy pathSimpleDialog.tsx
File metadata and controls
160 lines (153 loc) · 6.95 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import React, { BaseSyntheticEvent } from "react";
import { IntentTypes } from "../../common/Intent";
import { CLASSPREFIX as eccgui } from "../../configuration/constants";
import IconButton from "../Icon/IconButton";
import { TestableComponent } from "../interfaces";
import { Card, CardActions, CardActionsProps, CardContent, CardHeader, CardOptions, CardTitle } from "./../Card";
import Divider from "./../Separation/Divider";
import Modal, { ModalProps } from "./Modal";
export interface SimpleDialogProps extends ModalProps, TestableComponent {
/**
* The title of the dialog.
*/
title?: string;
/**
* Parts of the dialog are separated by a horizontal ruler.
*/
hasBorder?: boolean;
/**
* Include elements to the action footer, e.g. buttons.
*/
actions?: React.ReactNode | React.ReactNode[];
/**
* If populated with elements, then a second content area is included before the action footer.
* Mainly provided to include `Notification` elements.
*/
notifications?: React.ReactNode | React.ReactNode[];
/**
* Can contain elements actionable/non-actionable elements display right-aligned to the dialog title.
*/
headerOptions?: null | React.JSX.Element | React.JSX.Element[];
/**
* If enabled neither closing via `esc` key or clicking outside the component will work, except explicitly specified.
*/
preventSimpleClosing?: boolean;
/**
* Define purpose of the dialog, e.g. if it is a warning.
*/
intent?: IntentTypes;
/** Optional props for the wrapper div element inside the modal. */
wrapperDivProps?: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>;
/** If a full screen toggler is shown that will allow to switch to full screen mode. */
showFullScreenToggler?: boolean;
/** Starts the modal in full screen mode. The show full screen toggler will be automatically enabled. */
startInFullScreenMode?: boolean;
/** Forward properties to the actions footer component. */
actionsProps?: Omit<CardActionsProps, "inverseDirection">;
}
/**
* Simplifies the dialog display by providing a direct `Card` template for the `Modal` element.
* If not given then aria attributes like `role` and `aria-labelledby` are set automatically.
* `aria-describedby` is only automatically when an `intent` state is set.
* Inherits all properties from `Modal`.
*/
export const SimpleDialog = ({
children,
canOutsideClickClose = false,
canEscapeKeyClose = false,
title = "",
actions = null,
notifications = null,
hasBorder = false,
preventSimpleClosing = false,
enforceFocus = false,
intent,
headerOptions,
showFullScreenToggler = false,
startInFullScreenMode = false,
size,
role,
"aria-labelledby": ariaLabelledby,
"aria-describedby": ariaDescribedby,
"aria-label": ariaLabel,
actionsProps,
...otherProps
}: SimpleDialogProps) => {
const dialogUniqueId = React.useId().replace(/[^a-zA-Z0-9_-]/g, "");
const [displayFullscreen, setDisplayFullscreen] = React.useState<boolean>(startInFullScreenMode);
const showToggler = startInFullScreenMode || showFullScreenToggler;
const intentClassName = intent ? `${eccgui}-intent--${intent}` : "";
const wrapperDivProps = {
...modalPreventEvents,
...otherProps.wrapperDivProps,
};
const hasSemanticIntent = intent && ["success", "warning", "danger", "info"].includes(intent);
return (
<Modal
enforceFocus={enforceFocus}
{...otherProps}
wrapperDivProps={wrapperDivProps}
// set default test id if not given
data-test-id={otherProps["data-test-id"] ?? "simpleDialogWidget"}
canOutsideClickClose={canOutsideClickClose || !preventSimpleClosing}
canEscapeKeyClose={canEscapeKeyClose || !preventSimpleClosing}
size={displayFullscreen ? "fullscreen" : size}
role={role ?? (hasSemanticIntent ? "alertdialog" : "dialog")}
aria-label={ariaLabel}
aria-labelledby={ariaLabelledby ?? (title ? `title_${dialogUniqueId}` : undefined)}
aria-describedby={ariaDescribedby ?? (hasSemanticIntent ? `description_${dialogUniqueId}` : undefined)}
>
<Card className={intentClassName}>
{title || headerOptions || showToggler ? (
<CardHeader>
<CardTitle className={intentClassName} id={title ? `title_${dialogUniqueId}` : undefined}>
{title}
</CardTitle>
{headerOptions || showToggler ? (
<CardOptions>
{headerOptions}
{showToggler && (
<IconButton
name={displayFullscreen ? "toggler-minimize" : "toggler-maximize"}
onClick={() => setDisplayFullscreen(!displayFullscreen)}
/>
)}
</CardOptions>
) : (
<></>
)}
</CardHeader>
) : null}
{hasBorder && <Divider />}
<CardContent id={hasSemanticIntent ? `description_${dialogUniqueId}` : undefined}>{children}</CardContent>
{hasBorder && <Divider />}
{!!notifications && (
<CardContent className={`${eccgui}-dialog__notifications`}>{notifications}</CardContent>
)}
{actions && (
<CardActions
{...actionsProps}
inverseDirection
className={`${actionsProps?.className ?? ""} ${intentClassName}`}
>
{actions}
</CardActions>
)}
</Card>
</Modal>
);
};
/** Events that should be prevented to bubble up from a modal that goes beyond the most simple version of a modal, e.g.
* allows to drag or supports hot keys etc. */
export const modalPreventEvents = {
// Prevent certain events from leaving the modal, so that e.g. react-flow does not receive these events doing unexpected stuff
onContextMenu: (event: BaseSyntheticEvent) => event.stopPropagation(),
onDrag: (event: BaseSyntheticEvent) => event.stopPropagation(),
onDragStart: (event: BaseSyntheticEvent) => event.stopPropagation(),
onDragEnd: (event: BaseSyntheticEvent) => event.stopPropagation(),
// The following prevents some drop-downs to not close anymore when clicking outside of them
// onMouseDown: (event: BaseSyntheticEvent) => event.stopPropagation(),
onMouseUp: (event: BaseSyntheticEvent) => event.stopPropagation(),
onClick: (event: BaseSyntheticEvent) => event.stopPropagation(),
};
export default SimpleDialog;