-
Notifications
You must be signed in to change notification settings - Fork 2
/
EnumEditor.tsx
239 lines (212 loc) · 6.77 KB
/
EnumEditor.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
/*---------------------------------------------------------------------------------------------
* 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 "./EnumEditor.scss";
import classnames from "classnames";
import * as React from "react";
import type { EnumerationChoice, PropertyValue } from "@itwin/appui-abstract";
import { PropertyValueFormat, StandardTypeNames } from "@itwin/appui-abstract";
import type { SelectOption } from "@itwin/itwinui-react";
import { Select } from "@itwin/itwinui-react";
import type { PropertyEditorProps, TypeEditor } from "./EditorContainer";
import {
PropertyEditorBase,
PropertyEditorManager,
} from "./PropertyEditorManager";
import { UiComponents } from "../UiComponents";
/** @internal */
interface EnumEditorState {
selectValue: string | number;
valueIsNumber: boolean;
options: SelectOption<string>[];
parentDiv: HTMLDivElement | null;
}
/** EnumEditor React component that is a property editor with select input
* @public
*/
export class EnumEditor
extends React.PureComponent<PropertyEditorProps, EnumEditorState>
implements TypeEditor
{
private _isMounted = false;
private _divElement = React.createRef<HTMLDivElement>();
/** @internal */
public override readonly state: Readonly<EnumEditorState> = {
selectValue: "",
valueIsNumber: false,
options: [],
parentDiv: null,
};
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.selectValue,
displayValue: "",
};
}
return propertyValue;
}
// istanbul ignore next
public get htmlElement(): HTMLElement | null {
return this._divElement.current;
}
// istanbul ignore next
public get hasFocus(): boolean {
let containsFocus = false;
// istanbul ignore else
if (this._divElement.current)
containsFocus = this._divElement.current.contains(document.activeElement);
return containsFocus;
}
private _updateSelectValue = (newValue: string) => {
// istanbul ignore else
if (this._isMounted) {
let selectValue: string | number;
// istanbul ignore if
if (this.state.valueIsNumber) selectValue = parseInt(newValue, 10);
else selectValue = newValue;
this.setState(
{
selectValue,
},
async () => {
// istanbul ignore else
if (this.props.propertyRecord && this.props.onCommit) {
const propertyValue = await this.getPropertyValue();
// istanbul ignore else
if (propertyValue) {
this.props.onCommit({
propertyRecord: this.props.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 { propertyRecord } = this.props;
let initialValue: string | number = "";
let valueIsNumber: boolean = false;
// istanbul ignore else
if (
propertyRecord &&
propertyRecord.value.valueFormat === PropertyValueFormat.Primitive
) {
const primitiveValue = propertyRecord.value.value;
if (typeof primitiveValue === "string") {
initialValue = primitiveValue;
valueIsNumber = false;
} else {
initialValue = primitiveValue as number;
valueIsNumber = true;
}
}
let choices: EnumerationChoice[] | undefined;
if (propertyRecord && propertyRecord.property.enum) {
// istanbul ignore else
if (propertyRecord.property.enum.choices instanceof Promise) {
choices = await propertyRecord.property.enum.choices;
} else {
choices = propertyRecord.property.enum.choices;
}
}
const options: SelectOption<string>[] = [];
if (choices) {
choices.forEach((choice: EnumerationChoice) => {
options.push({ value: choice.value.toString(), label: choice.label });
});
}
// istanbul ignore else
if (this._isMounted)
this.setState({
selectValue: initialValue,
valueIsNumber,
options,
parentDiv: this._divElement.current,
});
}
/** @internal */
public override render() {
const className = classnames(
"components-cell-editor",
"components-enum-editor",
this.props.className
);
const selectValue =
this.state.selectValue !== undefined
? this.state.selectValue.toString()
: /* istanbul ignore next */ undefined;
// set min-width to show about 4 characters + down arrow
const minWidthStyle: React.CSSProperties = {
minWidth: `6em`,
};
// The iTwinUI-react Select onBlur still does not work properly, so it cannot be used yet. NEEDSWORK
// onBlur={this.props.onBlur}
return (
<div ref={this._divElement}>
<Select
className={className}
style={this.props.style ? this.props.style : minWidthStyle}
value={selectValue}
onChange={this._updateSelectValue}
data-testid="components-select-editor"
options={this.state.options}
triggerProps={{
ref: (el) => {
if (!this.props.setFocus) return;
el?.focus();
},
className: "components-button",
}}
aria-label={UiComponents.translate("editor.enum")}
size="small"
/>
</div>
);
}
}
/** Enum Property Button Group Editor registered for the "enum" type name.
* It uses the [[EnumEditor]] React component.
* @public
*/
export class EnumPropertyEditor extends PropertyEditorBase {
// istanbul ignore next
public override get containerHandlesEnter(): boolean {
return false;
}
public override get containerStopsKeydownPropagation(): boolean {
return false;
}
public get reactNode(): React.ReactNode {
return <EnumEditor />;
}
}
PropertyEditorManager.registerEditor(
StandardTypeNames.Enum,
EnumPropertyEditor
);