-
Notifications
You must be signed in to change notification settings - Fork 2
/
CardPopup.tsx
196 lines (183 loc) · 5.92 KB
/
CardPopup.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Popup
*/
import "./CardPopup.scss";
import * as React from "react";
import classnames from "classnames";
import { Key } from "ts-key-enum";
import type { CommonToolbarItem, PropertyRecord } from "@itwin/appui-abstract";
import type { RelativePosition } from "@itwin/appui-abstract";
import type { Orientation, SizeProps } from "@itwin/core-react";
import {
DivWithOutsideClick,
FocusTrap,
MessageRenderer,
Point,
Size,
} from "@itwin/core-react";
import { Text } from "@itwin/itwinui-react";
import { CursorPopup } from "../cursor/cursorpopup/CursorPopup";
import type { PopupContentType, PopupPropsBase } from "./PopupManager";
import { isReactContent, PopupManager } from "./PopupManager";
import { PositionPopup } from "./PositionPopup";
import {
Direction,
PropertyValueRendererManager,
ToolbarOpacitySetting,
ToolbarPanelAlignment,
} from "@itwin/components-react";
import { ToolbarWithOverflow } from "../toolbar/ToolbarWithOverflow";
import type { ToolbarItem } from "../toolbar/ToolbarItem";
import { mapToPlacement, type Placement } from "../utils/Placement";
import { WrapperContext } from "../configurableui/ConfigurableUiContent";
import type { RequireAtLeastOne } from "@itwin/core-bentley";
/** Props for defining a CardPopup editor
* @beta */
export type CardPopupProps = Omit<PopupPropsBase, "el"> & {
content: PopupContentType;
title: string | PropertyRecord | undefined;
items: CommonToolbarItem[] | ToolbarItem[] | undefined; // {@link @itwin/appui-abstract#CommonToolbarItem} will be deprecated in 4.11.x. Please use {@link ToolbarItem[]} instead.
orientation: Orientation;
onCancel: () => void;
onItemExecuted: (item: any) => void;
el?: HTMLElement;
} & RequireAtLeastOne<{
relativePosition: RelativePosition; // @deprecated in 4.11.x. Please use placement instead.
placement: Placement;
}>;
/** @internal */
interface CardPopupState {
size: Size;
}
/** Popup component for Input Editor
* @beta
*/
export class CardPopup extends React.PureComponent<
CardPopupProps,
CardPopupState
> {
/** @internal */
public static override contextType = WrapperContext;
/** @internal */
public declare context: React.ContextType<typeof WrapperContext>;
/** @internal */
public override readonly state = {
size: new Size(-1, -1),
};
private _onSizeKnown = (newSize: SizeProps) => {
// istanbul ignore else
if (!this.state.size.equals(newSize))
this.setState({ size: Size.create(newSize) });
};
private _handleKeyDown = (event: React.KeyboardEvent): void => {
switch (event.key) {
case Key.Escape.valueOf():
this._cancel();
break;
}
};
private _cancel() {
// istanbul ignore else
if (this.props.onCancel) {
this.props.onCancel();
}
}
public override render() {
let point = PopupManager.getPopupPosition(
this.props.el ?? this.context,
this.props.pt,
new Point(),
this.state.size
);
const popupRect = CursorPopup.getPopupRect(
point,
this.props.offset,
this.state.size,
this.props.placement ?? mapToPlacement(this.props.relativePosition)
);
point = new Point(popupRect.left, popupRect.top);
return (
<PositionPopup
key={this.props.id}
className={classnames("uifw-no-border", "uifw-card")}
point={point}
onSizeKnown={this._onSizeKnown}
>
{/* eslint-disable-next-line deprecation/deprecation */}
<DivWithOutsideClick
onOutsideClick={this.props.onCancel}
onKeyDown={this._handleKeyDown}
>
<FocusTrap active={true} returnFocusOnDeactivate={true}>
<Card
content={this.props.content}
title={this.props.title}
items={this.props.items}
// toolbarItems={this.props.toolbarItems}
onItemExecuted={this.props.onItemExecuted}
/>
</FocusTrap>
</DivWithOutsideClick>
</PositionPopup>
);
}
}
/** Props defining a Card component
* @beta */
export interface CardProps {
content: PopupContentType;
title: string | PropertyRecord | undefined;
items?: CommonToolbarItem[] | ToolbarItem[] | undefined; // @deprecated in 4.11.x. Please use {@link CardProps.toolbarItems}
onItemExecuted: (item: any) => void;
}
/** Card component
* @beta */
export function Card(props: CardProps) {
let titleNode: React.ReactNode;
// istanbul ignore else
if (props.title) {
if (typeof props.title === "string")
titleNode = <Text variant="leading">{props.title}</Text>;
else {
const propertyValueRendererManager =
PropertyValueRendererManager.defaultManager;
const titleValue = propertyValueRendererManager.render(props.title);
titleNode = <Text variant="leading">{titleValue}</Text>;
}
}
const content = isReactContent(props.content) ? (
props.content.reactNode
) : (
<MessageRenderer message={props.content} />
);
return (
<>
<div className="uifw-card-content">
{props.title && (
<>
{titleNode}
<div className="uifw-card-gap" />
</>
)}
{content}
</div>
{props.items && (
<>
<div className="uifw-card-separator" />
<ToolbarWithOverflow
expandsTo={Direction.Bottom}
panelAlignment={ToolbarPanelAlignment.Start}
items={props.items}
useDragInteraction={true}
toolbarOpacitySetting={ToolbarOpacitySetting.Transparent}
onItemExecuted={props.onItemExecuted}
/>
</>
)}
</>
);
}