-
Notifications
You must be signed in to change notification settings - Fork 2
/
EditorContainer.tsx
263 lines (232 loc) · 7.18 KB
/
EditorContainer.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*---------------------------------------------------------------------------------------------
* 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 "./EditorContainer.scss";
import * as React from "react";
import { Key } from "ts-key-enum";
import type { PropertyRecord, PropertyValue } from "@itwin/appui-abstract";
import { UiAdmin } from "@itwin/appui-abstract";
import type { CommonProps } from "@itwin/core-react";
import type {
AsyncErrorMessage,
PropertyEditorBase,
} from "./PropertyEditorManager";
import { PropertyEditorManager } from "./PropertyEditorManager";
/** Arguments for the Property Updated event callback
* @public
*/
export interface PropertyUpdatedArgs {
/** The property being updated. */
propertyRecord: PropertyRecord;
/** The new value for the property. */
newValue: PropertyValue;
}
/** Properties for a property editor component
* @public
*/
export interface PropertyEditorProps extends CommonProps {
/** The property being updated. */
propertyRecord?: PropertyRecord;
/** Handler for commit */
onCommit?: (args: PropertyUpdatedArgs) => void;
/** Handler for cancel */
onCancel?: () => void;
/** Handler for blur */
onBlur?: (event: React.FocusEvent) => void;
/** Indicates whether the Property Editor should set focus */
setFocus?: boolean;
/**
* Indicates whether value change should call onCommit().
* @internal
*/
shouldCommitOnChange?: boolean;
}
/** [[EditorContainer]] React component properties
* @public
*/
export interface EditorContainerProps extends CommonProps {
/** The property being updated. */
propertyRecord: PropertyRecord;
/** Tooltip text */
title?: string;
/** Handler for commit */
onCommit: (args: PropertyUpdatedArgs) => void;
/** Handler for cancel */
onCancel: () => void;
/** Indicates whether the Property Editor should set focus */
setFocus?: boolean;
/** @internal */
ignoreEditorBlur?: boolean;
/**
* Indicates whether value change should call onCommit().
* @internal
*/
shouldCommitOnChange?: boolean;
}
interface CloneProps extends PropertyEditorProps {
ref: (ref: TypeEditor | null) => void;
}
/** Interface implemented by React based type editors
* @public
*/
export interface TypeEditor {
getPropertyValue: () => Promise<PropertyValue | undefined>;
htmlElement: HTMLElement | null;
hasFocus: boolean;
}
/** EditorContainer React component used by the Tree and PropertyGrid for cell editing.
* @public
*/
export function EditorContainer(props: EditorContainerProps) {
const {
ignoreEditorBlur,
title,
shouldCommitOnChange,
propertyRecord,
setFocus,
onCommit,
onCancel,
...rest
} = props;
const editorRef = React.useRef<TypeEditor | undefined>();
const propertyEditorRef = React.useRef<PropertyEditorBase | undefined>();
const handleClick = (e: React.MouseEvent) => e.stopPropagation();
const handleContainerBlur = (e: React.FocusEvent) => e.stopPropagation();
const handleEditorCommit = (args: PropertyUpdatedArgs) => void commit(args);
const handleContainerCommit = async () => {
const newValue = editorRef && (await editorRef.current?.getPropertyValue());
if (newValue === undefined) return;
void commit({
propertyRecord,
newValue,
});
};
const onPressEscape = () => {
if (!propertyEditorRef.current?.containerHandlesEscape) return;
onCancel();
};
const onPressEnter = (e: React.KeyboardEvent) => {
if (!propertyEditorRef.current?.containerHandlesEnter) return;
if (editorRef?.current?.hasFocus) e.stopPropagation();
void handleContainerCommit();
};
const onPressTab = (e: React.KeyboardEvent) => {
if (!propertyEditorRef.current?.containerHandlesTab) return;
e.stopPropagation();
void handleContainerCommit();
};
const handleKeyDown = (e: React.KeyboardEvent) => {
switch (e.key) {
case Key.Escape.valueOf():
onPressEscape();
break;
case Key.Enter.valueOf():
onPressEnter(e);
break;
case Key.Tab.valueOf():
onPressTab(e);
break;
default:
if (propertyEditorRef.current?.containerStopsKeydownPropagation)
e.stopPropagation();
}
};
const handleEditorBlur = () => {
if (ignoreEditorBlur) return;
if (!propertyEditorRef.current?.containerHandlesBlur) return;
void handleContainerCommit();
};
const isNewValueValid = async (value: PropertyValue) => {
if (!propertyEditorRef.current) return false;
if (!propertyRecord) return false;
const validateResult = await propertyEditorRef.current.validateValue(
value,
propertyRecord
);
if (validateResult.encounteredError) {
displayOutputMessage(validateResult.errorMessage);
return false;
}
return true;
};
const displayOutputMessage = (
errorMessage: AsyncErrorMessage | undefined
) => {
if (!errorMessage) return;
if (!editorRef) return;
const htmlElement = editorRef.current?.htmlElement;
if (htmlElement)
UiAdmin.messagePresenter.displayInputFieldMessage(
htmlElement,
errorMessage.severity,
errorMessage.briefMessage,
errorMessage.detailedMessage
);
else
UiAdmin.messagePresenter.displayMessage(
errorMessage.severity,
errorMessage.briefMessage,
errorMessage.detailedMessage,
errorMessage.messageType
);
};
const commit = async (args: PropertyUpdatedArgs) => {
const newValue = args.newValue;
const isValid = await isNewValueValid(newValue);
if (!isValid) return;
if (propertyEditorRef.current && args.propertyRecord) {
const commitResult = await propertyEditorRef.current.commitValue(
newValue,
args.propertyRecord
);
if (commitResult.encounteredError) {
displayOutputMessage(commitResult.errorMessage);
return;
}
}
onCommit(args);
};
const editorProps: CloneProps = {
ref: (ref) => {
editorRef.current = ref ?? undefined;
},
onCommit: handleEditorCommit,
onCancel,
onBlur: handleEditorBlur,
setFocus: setFocus !== undefined ? setFocus : true,
propertyRecord,
shouldCommitOnChange,
...rest,
};
const propDescription = propertyRecord.property;
const editorName =
propDescription.editor !== undefined
? propDescription.editor.name
: undefined;
propertyEditorRef.current = PropertyEditorManager.createEditor(
propDescription.typename,
editorName,
propDescription.dataController
);
const clonedNode = React.isValidElement(propertyEditorRef.current.reactNode)
? React.cloneElement(propertyEditorRef.current.reactNode, editorProps)
: undefined;
return (
<span
className="components-editor-container"
onBlur={handleContainerBlur}
onKeyDown={handleKeyDown}
onClick={handleClick}
onContextMenu={handleClick}
title={title}
data-testid="editor-container"
role="presentation"
>
{clonedNode}
</span>
);
}