-
Notifications
You must be signed in to change notification settings - Fork 2
/
PropertyRenderer.tsx
217 lines (201 loc) · 7.28 KB
/
PropertyRenderer.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Properties
*/
import * as React from "react";
import type { PropertyRecord } from "@itwin/appui-abstract";
import { PropertyValueFormat } from "@itwin/appui-abstract";
import type { Orientation } from "@itwin/core-react";
import type { HighlightingComponentProps } from "../../common/HighlightingComponentProps";
import type { PropertyUpdatedArgs } from "../../editors/EditorContainer";
import { EditorContainer } from "../../editors/EditorContainer";
import { UiComponents } from "../../UiComponents";
import type { PropertyValueRendererManager } from "../ValueRendererManager";
import type { ActionButtonRenderer } from "./ActionButtonRenderer";
import { CommonPropertyRenderer } from "./CommonPropertyRenderer";
import { NonPrimitivePropertyRenderer } from "./NonPrimitivePropertyRenderer";
import type { PrimitiveRendererProps } from "./PrimitivePropertyRenderer";
import { PrimitivePropertyRenderer } from "./PrimitivePropertyRenderer";
import type { PropertyGridColumnInfo } from "./PropertyGridColumns";
/** Properties shared by all renderers and PropertyView
* @public
*/
export interface SharedRendererProps {
/** PropertyRecord to render */
propertyRecord: PropertyRecord;
/** Unique string, that identifies this property component. Should be used if onClick or onRightClick are provided */
uniqueKey?: string;
/** Orientation to use for displaying the property */
orientation: Orientation;
/** Controls component selection */
isSelected?: boolean;
/** Called when property gets clicked. If undefined, clicking is disabled */
onClick?: (property: PropertyRecord, key?: string) => void;
/** Called when property gets right clicked. If undefined, right clicking is not working */
onRightClick?: (property: PropertyRecord, key?: string) => void;
/** Called to show a context menu for properties */
onContextMenu?: (property: PropertyRecord, e: React.MouseEvent) => void;
/** Ratio between label and value cells */
columnRatio?: number;
/** Callback to column ratio changed event */
onColumnRatioChanged?: (ratio: number) => void | {
ratio: number;
};
/** Indicates that properties have *hover* effect */
isHoverable?: boolean;
/** Indicates that properties can be selected */
isSelectable?: boolean;
/** Width of the whole property element */
width?: number;
/** Array of action button renderers */
actionButtonRenderers?: ActionButtonRenderer[];
/** Is resize handle hovered */
isResizeHandleHovered?: boolean;
/** Callback to hover event change */
onResizeHandleHoverChanged?: (isHovered: boolean) => void;
/** Is resize handle being dragged */
isResizeHandleBeingDragged?: boolean;
/** Callback to drag event change */
onResizeHandleDragChanged?: (isDragStarted: boolean) => void;
/** Information for styling property grid columns */
columnInfo?: PropertyGridColumnInfo;
}
/** Properties of [[PropertyRenderer]] React component
* @public
*/
export interface PropertyRendererProps extends SharedRendererProps {
/** Custom value renderer */
propertyValueRendererManager?: PropertyValueRendererManager;
/** Multiplier of how much the property is indented to the right */
indentation?: number;
/** Indicates property is being edited */
isEditing?: boolean;
/** Called when property edit is committed. */
onEditCommit?: (args: PropertyUpdatedArgs) => void;
/** Called when property edit is cancelled. */
onEditCancel?: () => void;
/** Props used for highlighting. */
highlight?: HighlightingComponentProps;
}
/** State of [[PropertyRenderer]] React component
* @internal
*/
interface PropertyRendererState {
/** Currently loaded property value */
displayValue?: React.ReactNode;
}
/** A React component that renders properties
* @public
*/
export class PropertyRenderer extends React.Component<
PropertyRendererProps,
PropertyRendererState
> {
/** @internal */
public override readonly state: Readonly<PropertyRendererState> = {
displayValue: UiComponents.translate("general.loading"),
};
constructor(props: PropertyRendererProps) {
super(props);
}
public static getLabelOffset(
indentation?: number,
orientation?: Orientation,
width?: number,
columnRatio?: number,
minColumnLabelWidth?: number
): number {
return CommonPropertyRenderer.getLabelOffset(
indentation,
orientation,
width,
columnRatio,
minColumnLabelWidth
);
}
private updateDisplayValue(props: PropertyRendererProps) {
if (props.isEditing) {
this.updateDisplayValueAsEditor(props);
return;
}
const displayValue = CommonPropertyRenderer.createNewDisplayValue(
props.orientation,
props.propertyRecord,
props.indentation,
props.propertyValueRendererManager
);
this.setState({ displayValue });
}
private _onEditCommit = (args: PropertyUpdatedArgs) => {
// istanbul ignore else
if (this.props.onEditCommit) this.props.onEditCommit(args);
};
private _onEditCancel = () => {
// istanbul ignore else
if (this.props.onEditCancel) this.props.onEditCancel();
};
/** Display property record value in an editor */
public updateDisplayValueAsEditor(props: PropertyRendererProps) {
this.setState({
displayValue: (
<EditorContainer
propertyRecord={props.propertyRecord}
onCommit={this._onEditCommit}
onCancel={this._onEditCancel}
setFocus={true}
/>
),
});
}
/** @internal */
public override componentDidMount() {
this.updateDisplayValue(this.props);
}
/** @internal */
public override componentDidUpdate(prevProps: PropertyRendererProps) {
if (
prevProps.propertyRecord !== this.props.propertyRecord ||
prevProps.isEditing !== this.props.isEditing ||
prevProps.orientation !== this.props.orientation
)
this.updateDisplayValue(this.props);
}
/** @internal */
public override render() {
const {
propertyValueRendererManager,
isEditing,
onEditCommit,
onEditCancel,
...props
} = this.props;
const primitiveRendererProps: PrimitiveRendererProps = {
...props,
valueElement: this.state.displayValue,
indentation: this.props.indentation,
};
switch (this.props.propertyRecord.value.valueFormat) {
case PropertyValueFormat.Primitive:
return <PrimitivePropertyRenderer {...primitiveRendererProps} />;
case PropertyValueFormat.Array:
// If array is empty, render it as a primitive property
if (
this.props.propertyRecord.value.valueFormat ===
PropertyValueFormat.Array &&
this.props.propertyRecord.value.items.length === 0
)
return <PrimitivePropertyRenderer {...primitiveRendererProps} />;
// eslint-disable-next-line no-fallthrough
case PropertyValueFormat.Struct:
return (
<NonPrimitivePropertyRenderer
isCollapsible={true}
{...primitiveRendererProps}
/>
);
}
}
}