-
Notifications
You must be signed in to change notification settings - Fork 2
/
TextEditor.tsx
235 lines (210 loc) · 6.8 KB
/
TextEditor.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
/*---------------------------------------------------------------------------------------------
* 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 "./TextEditor.scss";
import classnames from "classnames";
import * as React from "react";
import type {
IconEditorParams,
InputEditorSizeParams,
PrimitiveValue,
PropertyEditorParams,
PropertyValue,
} from "@itwin/appui-abstract";
import {
PropertyEditorParamTypes,
PropertyValueFormat,
} from "@itwin/appui-abstract";
import { Icon, IconInput } from "@itwin/core-react";
import { Input } from "@itwin/itwinui-react";
import { TypeConverterManager } from "../converters/TypeConverterManager";
import type { PropertyEditorProps, TypeEditor } from "./EditorContainer";
import { UiComponents } from "../UiComponents";
type InputProps = React.ComponentPropsWithoutRef<typeof Input>;
/** @internal */
interface TextEditorState {
inputValue: string;
readonly: boolean;
isDisabled?: boolean;
size?: number;
maxLength?: number;
iconSpec?: string;
}
/** TextEditor React component that is a property editor with text input
* @public
*/
export class TextEditor
extends React.PureComponent<PropertyEditorProps, TextEditorState>
implements TypeEditor
{
private _isMounted = false;
private _inputElement = React.createRef<HTMLInputElement>();
/** @internal */
public override readonly state: Readonly<TextEditorState> = {
inputValue: "",
readonly: false,
};
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 = await TypeConverterManager.getConverter(
record.property.typename,
record.property.converter?.name
).convertFromStringToPropertyValue(this.state.inputValue, record);
(propertyValue as PrimitiveValue).displayValue = this.state.inputValue;
}
return propertyValue;
}
public get htmlElement(): HTMLElement | null {
return this._inputElement.current;
}
public get hasFocus(): boolean {
return document.activeElement === this._inputElement.current;
}
private _updateInputValue = (e: React.ChangeEvent<HTMLInputElement>) => {
// istanbul ignore else
if (this._isMounted)
this.setState(
{
inputValue: e.target.value,
},
async () => {
if (
this.props.shouldCommitOnChange &&
this.props.onCommit &&
this.props.propertyRecord
) {
const newValue = await this.getPropertyValue();
// istanbul ignore else
if (newValue)
this.props.onCommit({
propertyRecord: this.props.propertyRecord,
newValue,
});
}
}
);
};
/** @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 = "";
// istanbul ignore else
if (record && record.value.valueFormat === PropertyValueFormat.Primitive) {
const value = record.value.value;
initialValue = await TypeConverterManager.getConverter(
record.property.typename,
record.property.converter?.name
).convertPropertyToString(record.property, value);
}
const readonly =
record && undefined !== record.isReadonly ? record.isReadonly : false;
let size: number | undefined;
let maxLength: number | undefined;
let iconSpec: string | undefined;
const isDisabled = record ? record.isDisabled : undefined;
if (
record &&
record.property &&
record.property.editor &&
record.property.editor.params
) {
const editorSizeParams = record.property.editor.params.find(
(param: PropertyEditorParams) =>
param.type === PropertyEditorParamTypes.InputEditorSize.valueOf()
) as InputEditorSizeParams;
if (editorSizeParams) {
// istanbul ignore else
if (editorSizeParams.size) size = editorSizeParams.size;
// istanbul ignore else
if (editorSizeParams.maxLength) maxLength = editorSizeParams.maxLength;
}
const iconParams = record.property.editor.params.find(
(param: PropertyEditorParams) =>
param.type === PropertyEditorParamTypes.Icon.valueOf()
) as IconEditorParams;
if (iconParams) {
iconSpec = iconParams.definition.iconSpec;
}
}
// istanbul ignore else
if (this._isMounted)
this.setState({
inputValue: initialValue,
readonly,
size,
maxLength,
isDisabled,
iconSpec,
});
}
/** @internal */
public override render(): React.ReactNode {
const className = classnames(
"components-cell-editor",
"components-text-editor",
this.props.className
);
const minSize = this.state.size ? this.state.size : 8;
const minWidthStyle: React.CSSProperties = {
minWidth: `${minSize * 0.75}em`,
};
const inputProps: InputProps = {
type: "text",
className,
style: this.props.style ? this.props.style : minWidthStyle,
readOnly: this.state.readonly,
disabled: this.state.isDisabled,
maxLength: this.state.maxLength,
value: this.state.inputValue,
onBlur: this.props.onBlur,
onChange: this._updateInputValue,
autoFocus: this.props.setFocus && !this.state.isDisabled,
};
inputProps["aria-label"] = UiComponents.translate("editor.text");
let reactNode: React.ReactNode;
if (this.state.iconSpec) {
const icon = <Icon iconSpec={this.state.iconSpec} />;
reactNode = (
// eslint-disable-next-line deprecation/deprecation
<IconInput
{...inputProps}
ref={this._inputElement}
icon={icon}
data-testid="components-text-editor"
/>
);
} else {
reactNode = (
<Input
{...inputProps}
ref={this._inputElement}
data-testid="components-text-editor"
size="small"
id={this.props.propertyRecord?.property.name}
/>
);
}
return reactNode;
}
}