-
Notifications
You must be signed in to change notification settings - Fork 2
/
PropertyView.tsx
146 lines (135 loc) · 4.96 KB
/
PropertyView.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
/*---------------------------------------------------------------------------------------------
* 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 "./PropertyView.scss";
import * as React from "react";
import { ElementSeparator, Orientation } from "@itwin/core-react";
import { ActionButtonList } from "./ActionButtonList";
import { PropertyGridColumnStyleProvider } from "./PropertyGridColumns";
import type { SharedRendererProps } from "./PropertyRenderer";
/** Properties of [[PropertyView]] React component
* @public
*/
export interface PropertyViewProps extends SharedRendererProps {
/** Property label as a React element */
labelElement: React.ReactNode;
/** Property value as a React element */
valueElement?: React.ReactNode;
/** Render callback for property value. If specified, `valueElement` is ignored */
valueElementRenderer?: () => React.ReactNode;
}
/** @internal */
interface PropertyViewState {
isHovered: boolean;
}
/**
* A React component that renders property as label/value pair
* @public
*/
export class PropertyView extends React.Component<
PropertyViewProps,
PropertyViewState
> {
constructor(props: PropertyViewProps) {
super(props);
this.state = {
isHovered: false,
};
}
private _onClick = () => {
if (this.props.onClick)
this.props.onClick(this.props.propertyRecord, this.props.uniqueKey);
};
private _onMouseEnter = () => {
if (this.props.isHoverable) this.setState({ isHovered: true });
};
private _onMouseLeave = () => {
if (this.props.isHoverable) this.setState({ isHovered: false });
};
private _onContextMenu = (e: React.MouseEvent) => {
if (this.props.onContextMenu)
this.props.onContextMenu(this.props.propertyRecord, e);
if (this.props.onRightClick)
this.props.onRightClick(this.props.propertyRecord, this.props.uniqueKey);
e.preventDefault();
return false;
};
private getClassName(props: PropertyViewProps) {
let propertyRecordClassName =
props.orientation === Orientation.Horizontal
? "components-property-record--horizontal"
: "components-property-record--vertical";
if (props.isSelected) propertyRecordClassName += " components--selected";
if (props.onClick) propertyRecordClassName += " components--clickable";
if (props.isHoverable) propertyRecordClassName += " components--hoverable";
return propertyRecordClassName;
}
/** @internal */
public override render() {
const ratio = this.props.columnRatio ? this.props.columnRatio : 0.25;
const needElementSeparator =
this.props.orientation === Orientation.Horizontal &&
!!this.props.onColumnRatioChanged;
const needActionButtons = !!this.props.actionButtonRenderers;
const columnsStyleProvider = new PropertyGridColumnStyleProvider(
this.props.columnInfo
);
const renderValueElement = !!(
this.props.valueElement || this.props.valueElementRenderer
);
return (
<div
style={columnsStyleProvider.getStyle(
this.props.orientation,
needActionButtons,
ratio,
needElementSeparator
)}
className={this.getClassName(this.props)}
onClick={this._onClick}
onContextMenu={this._onContextMenu}
onMouseEnter={this._onMouseEnter}
onMouseLeave={this._onMouseLeave}
role="presentation"
>
<div className="components-property-record-label">
{this.props.labelElement}
</div>
{needElementSeparator ? (
// eslint-disable-next-line deprecation/deprecation
<ElementSeparator
movableArea={this.props.width}
onRatioChanged={this.props.onColumnRatioChanged}
ratio={ratio}
orientation={this.props.orientation}
isResizeHandleHovered={this.props.isResizeHandleHovered}
onResizeHandleHoverChanged={this.props.onResizeHandleHoverChanged}
isResizeHandleBeingDragged={this.props.isResizeHandleBeingDragged}
onResizeHandleDragChanged={this.props.onResizeHandleDragChanged}
/>
) : undefined}
{renderValueElement ? (
<div className="components-property-record-value">
<span>
{this.props.valueElementRenderer
? this.props.valueElementRenderer()
: this.props.valueElement}
</span>
</div>
) : undefined}
{this.props.actionButtonRenderers ? (
<ActionButtonList
orientation={this.props.orientation}
property={this.props.propertyRecord}
isPropertyHovered={this.state.isHovered}
actionButtonRenderers={this.props.actionButtonRenderers}
/>
) : undefined}
</div>
);
}
}