-
Notifications
You must be signed in to change notification settings - Fork 2
/
WeightEditor.tsx
204 lines (179 loc) · 6 KB
/
WeightEditor.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module PropertyEditors
*/
import "./WeightEditor.scss";
import classnames from "classnames";
import * as React from "react";
import type { PropertyRecord, PropertyValue } from "@itwin/appui-abstract";
import {
PropertyValueFormat,
StandardEditorNames,
StandardTypeNames,
} from "@itwin/appui-abstract";
import type { PropertyEditorProps, TypeEditor } from "@itwin/components-react";
import {
PropertyEditorBase,
PropertyEditorManager,
} from "@itwin/components-react";
import { WeightPickerButton } from "../lineweight/WeightPickerButton";
// cspell:ignore lineweight
/** @internal */
interface WeightEditorState {
weightValue: number;
readonly: boolean;
isDisabled?: boolean;
}
/** WeightEditor React component that is a property editor for picking a weight using a [[WeightPickerButton]] component
* @public
*/
export class WeightEditor
extends React.PureComponent<PropertyEditorProps, WeightEditorState>
implements TypeEditor
{
private _control: any = null;
private _isMounted = false;
private _availableWeights: number[] = [];
private _divElement = React.createRef<HTMLDivElement>();
/** @internal */
public override readonly state: Readonly<WeightEditorState> = {
weightValue: 0,
readonly: false,
};
constructor(props: PropertyEditorProps) {
super(props);
// TODO: add support for following if we need to specify set of weights to display
// const record = this.props.propertyRecord;
// if (record && record.property && record.property.editor && record.property.editor.params) {
// const weightParams = record.property.editor.params.find((param: PropertyEditorParams) => param.type === PropertyEditorParamTypes.ColorData) as WeightEditorParams;
// // istanbul ignore else
// if (weightParams) {
// weightParams.weightValues.forEach((weight: number) => {
// this._availableWeights.push(weight);
// });
// }
// }
}
public async getPropertyValue(): Promise<PropertyValue | undefined> {
const record = this.props.propertyRecord;
let propertyValue: PropertyValue | undefined;
// istanbul ignore else
if (record && record.value.valueFormat === PropertyValueFormat.Primitive) {
propertyValue = {
valueFormat: PropertyValueFormat.Primitive,
value: this.state.weightValue,
displayValue: "",
};
}
return propertyValue;
}
public get htmlElement(): HTMLElement | null {
return this._divElement.current;
}
public get hasFocus(): boolean {
let containsFocus = false;
// istanbul ignore else
if (this._divElement.current)
containsFocus = this._divElement.current.contains(document.activeElement);
return containsFocus;
}
private setFocus(): void {
// istanbul ignore else
if (this._control && !this.state.isDisabled) {
this._control.setFocus();
}
}
private _onLineWeightPick = (weight: number) => {
const propertyRecord = this.props.propertyRecord as PropertyRecord;
this.setState(
{
weightValue: weight,
},
async () => {
// istanbul ignore else
if (propertyRecord && this.props.onCommit) {
const propertyValue = await this.getPropertyValue();
// istanbul ignore else
if (propertyValue !== undefined) {
this.props.onCommit({ propertyRecord, newValue: propertyValue });
}
}
}
);
};
/** @internal */
public override componentDidMount() {
this._isMounted = true;
void this.setStateFromProps();
}
/** @internal */
public override componentWillUnmount() {
this._isMounted = false;
}
/** @internal */
public override componentDidUpdate(prevProps: PropertyEditorProps) {
if (this.props.propertyRecord !== prevProps.propertyRecord) {
void this.setStateFromProps();
}
}
private async setStateFromProps() {
const record = this.props.propertyRecord;
let initialValue = 0;
// istanbul ignore else
if (record && record.value.valueFormat === PropertyValueFormat.Primitive) {
initialValue = record.value.value as number;
}
const readonly =
record && undefined !== record.isReadonly ? record.isReadonly : false;
const isDisabled = record ? record.isDisabled : undefined;
// istanbul ignore else
if (this._isMounted)
this.setState({ weightValue: initialValue, readonly, isDisabled }, () => {
if (this.props.setFocus) {
this.setFocus();
}
});
}
/** @internal */
public override render() {
return (
<div
className={classnames("components-weight-editor", this.props.className)}
style={this.props.style}
ref={this._divElement}
>
<WeightPickerButton
ref={(control) => (this._control = control)}
activeWeight={this.state.weightValue}
weights={
this._availableWeights.length > 0
? /* istanbul ignore next */ this._availableWeights
: undefined
}
disabled={this.state.isDisabled ? true : false}
readonly={this.state.readonly}
onLineWeightPick={this._onLineWeightPick}
data-testid="components-weight-editor"
/>
</div>
);
}
}
/** Weight Property Editor registered for the "number" type name and "weight-picker" editor name.
* It uses the [[WeightEditor]] React component.
* @public
*/
export class WeightPropertyEditor extends PropertyEditorBase {
// istanbul ignore next
public get reactNode(): React.ReactNode {
return <WeightEditor />;
}
}
PropertyEditorManager.registerEditor(
StandardTypeNames.Number,
WeightPropertyEditor,
StandardEditorNames.WeightPicker
);