-
Notifications
You must be signed in to change notification settings - Fork 2
/
Dialog.tsx
426 lines (393 loc) · 13.5 KB
/
Dialog.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*---------------------------------------------------------------------------------------------
* 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 classnames from "classnames";
import * as React from "react";
import { Key } from "ts-key-enum";
import type { DialogButtonDef } from "@itwin/appui-abstract";
import { DialogButtonType } from "@itwin/appui-abstract";
import { Dialog as BaseDialog, Button } from "@itwin/itwinui-react";
import { DivWithOutsideClick } from "../base/DivWithOutsideClick";
import type { CommonProps } from "../utils/Props";
import type { Omit } from "../utils/typeUtils";
import "./Dialog.scss";
import { useTranslation } from "../l10n/useTranslation";
type ButtonProps = React.ComponentPropsWithoutRef<typeof Button>;
/** Enum for dialog alignment
* @public
* @deprecated in 4.12.x. Enum used in a deprecated component {@link Dialog}.
*/
export enum DialogAlignment {
TopLeft = "top-left",
Top = "top",
TopRight = "top-right",
Left = "left",
Center = "center",
Right = "right",
BottomLeft = "bottom-left",
Bottom = "bottom",
BottomRight = "bottom-right",
}
/** Properties for the [[Dialog]] component
* @public
* @deprecated in 4.12.x. Props of deprecated component {@link Dialog}.
*/
export interface DialogProps
extends Omit<React.AllHTMLAttributes<HTMLDivElement>, "title">,
CommonProps {
/** Indicates whether to show dialog or not */
opened: boolean;
/** Indicates whether the user can resize dialog with cursor. Default: false */
resizable?: boolean;
/** Indicates whether the user can move dialog with cursor. Default: false */
movable?: boolean;
/** Indicates whether the content should be inset. Default: true */
inset?: boolean;
/** Indicates whether the focus should be trapped within the dialog. Default: false */
trapFocus?: boolean;
/** Whether the hide the header. Default: false */
hideHeader?: boolean;
/** Override for the header */
header?: React.ReactNode;
/** Title to show in title bar of dialog */
title?: string | React.ReactElement;
/** Footer to show at bottom of dialog. Note: will override buttonCluster */
footer?: string | React.ReactElement;
/** List of DialogButtonDef objects specifying buttons and associated onClick events */
buttonCluster?: DialogButtonDef[];
/** Default alignment of dialog. */
// eslint-disable-next-line deprecation/deprecation
alignment?: DialogAlignment;
/** Initial x/left position of dialog in px. */
x?: number;
/** Initial y/top position of dialog in px. */
y?: number;
/** onClick event for X button for dialog */
onClose?: () => void;
/** 'keyup' event for Esc key */
onEscape?: () => void;
/** Triggered when a click is triggered outside of this dialog. */
onOutsideClick?: (event: MouseEvent) => any;
/** 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. */
height?: string | number;
/** Minimum width that the dialog may be resized to. Displayed in px if value is a number; otherwise, displayed in specified CSS unit. Default: 300px */
minWidth?: string | number;
/** Minimum height that the dialog may be resized to. Displayed in px if value is a number; otherwise, displayed in specified CSS unit. Default: 135px */
minHeight?: string | number;
/** Maximum width that the dialog may be resized to. Displayed in px if value is a number; otherwise, displayed in specified CSS unit. Default: 100% */
maxWidth?: string | number;
/** Maximum height that the dialog may be resized to. Displayed in px if value is a number; otherwise, displayed in specified CSS unit. */
maxHeight?: string | number;
/** Whether to show background overlay. Default: true.
* @note Modeless dialogs require an id and an implementation of onModelessPointerDown.
*/
modal?: boolean;
/** An id for a modeless dialog */
modelessId?: string;
/** Pointer Down event handler when modeless (modal = false) */
onModelessPointerDown?: (event: React.PointerEvent, id: string) => void;
/** Custom CSS Style for overlay */
backgroundStyle?: React.CSSProperties;
/** Custom CSS Style for title */
titleStyle?: React.CSSProperties;
/** Custom CSS Style for footer */
footerStyle?: React.CSSProperties;
/** Custom CSS class name for the content */
contentClassName?: string;
/** Custom CSS Style for the content */
contentStyle?: React.CSSProperties;
}
/** Dialog React component with optional resizing and dragging functionality
* @public
* @deprecated in 4.12.x. Use {@link https://itwinui.bentley.com/docs/dialog iTwinUI dialog} instead.
*/
// eslint-disable-next-line deprecation/deprecation
export class Dialog extends React.Component<DialogProps> {
private _parentDocument = document;
// eslint-disable-next-line deprecation/deprecation
public static defaultProps: Partial<DialogProps> = {
maxWidth: "100%",
width: "50%",
hideHeader: false,
resizable: false,
movable: false,
modal: true,
inset: true,
trapFocus: false,
};
// eslint-disable-next-line deprecation/deprecation
constructor(props: DialogProps) {
super(props);
}
public override componentWillUnmount(): void {
this._parentDocument.removeEventListener("keyup", this._handleKeyUp, true);
}
public override componentDidMount(): void {
this._parentDocument.addEventListener("keyup", this._handleKeyUp, true);
}
// istanbul ignore next
public handleRefSet = (containerDiv: HTMLDivElement | null) => {
if (containerDiv) this._parentDocument = containerDiv.ownerDocument;
};
public override render(): React.ReactElement {
const {
opened,
title,
footer,
buttonCluster,
onClose,
onEscape,
onOutsideClick,
minWidth,
minHeight,
x,
y,
width,
height,
maxHeight,
maxWidth,
backgroundStyle,
titleStyle,
footerStyle,
style,
contentStyle,
contentClassName,
modal,
resizable,
movable,
className,
alignment,
inset,
trapFocus,
modelessId,
onModelessPointerDown,
hideHeader,
header,
as,
...props
} = this.props;
let initialOffset: React.CSSProperties = {};
if (x || y) {
initialOffset = {
left: x ?? 0,
top: y ?? 0,
transform: "none",
};
}
const containerStyle: React.CSSProperties = {
...initialOffset,
margin: "",
width,
height,
};
const minMaxStyle: React.CSSProperties = {
minWidth,
minHeight,
maxWidth,
maxHeight,
};
const dialogBaseContainerStyle: React.CSSProperties = {
...containerStyle,
...minMaxStyle,
};
// eslint-disable-next-line deprecation/deprecation
const buttons = this.getFooterButtons(
buttonCluster,
"high-visibility",
true
);
return (
<BaseDialog
isOpen={opened}
onClose={onClose}
closeOnExternalClick={false}
closeOnEsc={false}
style={style}
className={classnames("core-dialog", className)}
isDraggable={movable}
isResizable={resizable}
trapFocus={trapFocus && /* istanbul ignore next */ modal}
preventDocumentScroll={true}
data-testid="core-dialog-root"
{...props}
>
{modal && <BaseDialog.Backdrop style={backgroundStyle} />}
{/* eslint-disable-next-line deprecation/deprecation */}
<DivWithOutsideClick onOutsideClick={onOutsideClick}>
<BaseDialog.Main
className={classnames(
"core-dialog-main",
alignment && this.getCSSClassNameFromAlignment(alignment)
)}
data-testid="core-dialog-container"
style={dialogBaseContainerStyle}
onPointerDown={this._handleContainerPointerDown}
>
{!hideHeader &&
(header || (
<BaseDialog.TitleBar
titleText={title}
style={titleStyle}
data-testid="core-dialog-head"
/>
))}
<BaseDialog.Content
className={contentClassName}
style={{ padding: inset ? undefined : 0, ...contentStyle }}
>
{this.props.children}
</BaseDialog.Content>
{footer ||
(buttons && (
<BaseDialog.ButtonBar style={footerStyle}>
{buttons}
</BaseDialog.ButtonBar>
))}
</BaseDialog.Main>
</DivWithOutsideClick>
</BaseDialog>
);
}
// eslint-disable-next-line deprecation/deprecation
private getCSSClassNameFromAlignment(alignment: DialogAlignment): string {
switch (alignment) {
// eslint-disable-next-line deprecation/deprecation
case DialogAlignment.TopLeft:
return "core-align-top-left";
// eslint-disable-next-line deprecation/deprecation
case DialogAlignment.Top:
return "core-align-top";
// eslint-disable-next-line deprecation/deprecation
case DialogAlignment.TopRight:
return "core-align-top-right";
// eslint-disable-next-line deprecation/deprecation
case DialogAlignment.Left:
return "core-align-left";
// eslint-disable-next-line deprecation/deprecation
case DialogAlignment.Center:
return "core-align-center";
// eslint-disable-next-line deprecation/deprecation
case DialogAlignment.Right:
return "core-align-right";
// eslint-disable-next-line deprecation/deprecation
case DialogAlignment.BottomLeft:
return "core-align-bottom-left";
// eslint-disable-next-line deprecation/deprecation
case DialogAlignment.Bottom:
return "core-align-bottom";
// eslint-disable-next-line deprecation/deprecation
case DialogAlignment.BottomRight:
return "core-align-bottom-right";
}
}
/** @deprecated in 4.12.x. Use iTwinUI Button instead. */
protected getFooterButtons(
buttonCluster: DialogButtonDef[] | undefined,
primaryStyleType: ButtonProps["styleType"] = "cta",
noCoreButtonClasses: boolean = false
): React.ReactNode[] | undefined {
if (buttonCluster === undefined) return undefined;
const buttons: React.ReactNode[] = [];
buttonCluster.forEach((button: DialogButtonDef, index: number) => {
buttons.push(
<DialogButton
key={index.toString()}
button={button}
primaryStyleType={primaryStyleType}
noCoreButtonClasses={noCoreButtonClasses}
/>
);
});
return buttons;
}
private _handleKeyUp = (event: KeyboardEvent) => {
if (
event.key === Key.Escape.valueOf() &&
this.props.opened &&
this.props.onEscape
) {
this.props.onEscape();
}
};
protected _handleContainerPointerDown = (event: React.PointerEvent): void => {
// istanbul ignore next
if (!this.props.modal) {
if (this.props.onModelessPointerDown && this.props.modelessId)
this.props.onModelessPointerDown(event, this.props.modelessId);
}
};
}
function DialogButton({
button,
primaryStyleType,
noCoreButtonClasses,
}: {
button: DialogButtonDef;
primaryStyleType: ButtonProps["styleType"];
noCoreButtonClasses: boolean;
}) {
const { translate } = useTranslation();
let buttonText = "";
let buttonClass = classnames(
!noCoreButtonClasses && "core-dialog-button",
!noCoreButtonClasses && `dialog-button-${button.type}`,
button.className
);
let usePrimaryStyleType = false;
switch (button.type) {
case DialogButtonType.OK:
buttonText = translate("dialog.ok");
buttonClass = classnames(buttonClass, button.buttonStyle);
usePrimaryStyleType = true;
break;
case DialogButtonType.Retry:
buttonText = translate("dialog.retry");
buttonClass = classnames(buttonClass, button.buttonStyle);
usePrimaryStyleType = true;
break;
case DialogButtonType.Yes:
buttonText = translate("dialog.yes");
buttonClass = classnames(buttonClass, button.buttonStyle);
usePrimaryStyleType = true;
break;
case DialogButtonType.No:
buttonText = translate("dialog.no");
buttonClass = classnames(buttonClass, button.buttonStyle);
break;
case DialogButtonType.Cancel:
buttonText = translate("dialog.cancel");
buttonClass = classnames(buttonClass, button.buttonStyle);
break;
case DialogButtonType.Close:
buttonText = translate("dialog.close");
buttonClass = classnames(buttonClass, button.buttonStyle);
break;
case DialogButtonType.Next:
buttonText = translate("dialog.next");
buttonClass = classnames(buttonClass, button.buttonStyle);
usePrimaryStyleType = true;
break;
case DialogButtonType.Previous:
buttonText = translate("dialog.previous");
buttonClass = classnames(buttonClass, button.buttonStyle);
usePrimaryStyleType = true;
break;
}
buttonText = button.label || buttonText;
return (
<Button
className={buttonClass}
disabled={button.disabled}
styleType={usePrimaryStyleType ? primaryStyleType : undefined}
onClick={button.onClick}
>
{buttonText}
</Button>
);
}