-
Notifications
You must be signed in to change notification settings - Fork 2
/
MessageBox.tsx
231 lines (219 loc) · 7.22 KB
/
MessageBox.tsx
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Dialog
*/
import "./MessageBox.scss";
import classnames from "classnames";
import * as React from "react";
import type { DialogButtonDef } from "@itwin/appui-abstract";
import { MessageSeverity } from "@itwin/appui-abstract";
import type { CommonProps } from "../utils/Props";
import {
SvgHelpCircular,
SvgHelpCircularHollow,
SvgInfoCircular,
SvgInfoCircularHollow,
SvgStatusError,
SvgStatusErrorHollow,
SvgStatusRejected,
SvgStatusRejectedHollow,
SvgStatusSuccess,
SvgStatusSuccessHollow,
SvgStatusWarning,
} from "@itwin/itwinui-icons-react";
import type { IconSpec } from "../icons/IconComponent";
import { Icon } from "../icons/IconComponent";
import { Dialog } from "../dialog/Dialog";
/** Properties for the [[MessageBox]] component
* @public
*/
export interface MessageBoxProps extends CommonProps {
/** Severity of MessageBox */
severity: MessageSeverity;
/** whether to show dialog or not */
opened: boolean;
/** List of [[DialogButtonDef]] objects specifying buttons and associated onClick events */
buttonCluster: DialogButtonDef[];
/** Title to show in title bar of dialog */
title?: string | React.ReactElement;
/** onClick event for X button for dialog */
onClose?: () => void;
/** 'keyup' event for <Esc> key */
onEscape?: () => void;
/** minimum width that the dialog may be resized to. Default: 400 */
minWidth?: number;
/** minimum height that the dialog may be resized to. Default: 400 */
minHeight?: number;
/** initial width of dialog.
* Displayed in px if value is a number, otherwise displayed in specified CSS unit.
* Default: "50%"
*/
width?: string | number;
/** initial height of dialog.
* Displayed in px if value is a number, otherwise displayed in specified CSS unit.
* Default: ""
*/
height?: string | number;
/** Whether to show background overlay. Default: true */
modal?: boolean;
/** Custom CSS class name for the content */
contentClassName?: string;
/** Custom CSS Style for the content */
contentStyle?: React.CSSProperties;
/** Message content */
children?: React.ReactNode;
}
/** Message Box React component.
* @public
*/
export class MessageBox extends React.PureComponent<MessageBoxProps> {
public static defaultProps: Partial<MessageBoxProps> = {
minWidth: 400,
minHeight: 400,
width: "512px",
modal: true,
};
public override render(): React.ReactElement {
return (
// eslint-disable-next-line deprecation/deprecation
<Dialog
title={this.props.title}
buttonCluster={this.props.buttonCluster}
opened={this.props.opened}
onClose={this.props.onClose}
onEscape={this.props.onEscape}
modal={this.props.modal}
className={this.props.className}
style={this.props.style}
width={this.props.width}
>
<MessageContainer
severity={this.props.severity}
className={this.props.contentClassName}
style={this.props.contentStyle}
>
{this.props.children}
</MessageContainer>
</Dialog>
);
}
}
/** Properties for the [[MessageContainer]] component
* @public
*/
export interface MessageContainerProps extends CommonProps {
severity: MessageSeverity;
/** Message Content */
children?: React.ReactNode;
}
/** Message Container React component.
* @public
*/
export class MessageContainer extends React.PureComponent<MessageContainerProps> {
/** Returns the class name corresponding to the MessageSeverity.
* @param severity MessageSeverity
*/
public static getIconClassName(severity: MessageSeverity): string;
/** Returns the class name associated with the WebFont icon corresponding to the MessageSeverity.
* @param severity MessageSeverity
* @param hollow Should the icon be hollow
* @deprecated in 4.0. Hollow parameter ignored, WebFont is not returned, only the color.
*/
public static getIconClassName(
severity: MessageSeverity,
// eslint-disable-next-line @typescript-eslint/unified-signatures
hollow?: boolean
): string;
/* istanbul ignore next */
public static getIconClassName(
severity: MessageSeverity,
_hollow?: boolean
): string {
let iconClassName = "";
switch (severity) {
case MessageSeverity.None:
iconClassName = "";
break;
case MessageSeverity.Success:
iconClassName = "core-message-box-success";
break;
case MessageSeverity.Information:
iconClassName = "core-message-box-information";
break;
case MessageSeverity.Question:
iconClassName = "core-message-box-question";
break;
case MessageSeverity.Warning:
iconClassName = "core-message-box-warning";
break;
case MessageSeverity.Error:
iconClassName = "core-message-box-error";
break;
case MessageSeverity.Fatal:
iconClassName = "core-message-box-fatal";
break;
}
return iconClassName;
}
/** Returns the React icon corresponding to the MessageSeverity.
* @param severity MessageSeverity
* @param hollow set to true to return the hollow form of the icon.
* @returns IconSpec
*/
/* ignore for unit tests and replace with visual testing */
/* istanbul ignore next */
public static getIcon(severity: MessageSeverity, hollow?: boolean): IconSpec {
let iconSpec: IconSpec = "";
switch (severity) {
case MessageSeverity.None:
iconSpec = "";
break;
case MessageSeverity.Success:
iconSpec = hollow ? <SvgStatusSuccessHollow /> : <SvgStatusSuccess />;
break;
case MessageSeverity.Information:
iconSpec = hollow ? <SvgInfoCircularHollow /> : <SvgInfoCircular />;
break;
case MessageSeverity.Question:
iconSpec = hollow ? <SvgHelpCircularHollow /> : <SvgHelpCircular />;
break;
case MessageSeverity.Warning:
iconSpec = <SvgStatusWarning />; // TODO - need icon-status-warning-hollow icon
break;
case MessageSeverity.Error:
iconSpec = hollow ? <SvgStatusErrorHollow /> : <SvgStatusError />;
break;
case MessageSeverity.Fatal:
iconSpec = hollow ? <SvgStatusRejectedHollow /> : <SvgStatusRejected />;
break;
}
return iconSpec;
}
public override render(): React.ReactElement {
const iconClassName = classnames(
"icon",
"core-message-box-icon",
MessageContainer.getIconClassName(this.props.severity)
);
const iconSpec = MessageContainer.getIcon(this.props.severity);
return (
<div className="core-message-box-container">
<div className={iconClassName}>
<Icon iconSpec={iconSpec} />
</div>
<div
className={classnames(
"core-message-box-content",
this.props.className
)}
style={this.props.style}
>
{this.props.children}
</div>
</div>
);
}
}