-
Notifications
You must be signed in to change notification settings - Fork 2
/
AccuDrawInputField.tsx
221 lines (202 loc) · 6.43 KB
/
AccuDrawInputField.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module AccuDraw
*/
import "./AccuDrawInputField.scss";
import classnames from "classnames";
import * as React from "react";
import { Key } from "ts-key-enum";
import type { ItemField } from "@itwin/core-frontend";
import type { CommonProps, IconSpec } from "@itwin/core-react";
import { Icon, useRefs } from "@itwin/core-react";
import { Input } from "@itwin/itwinui-react";
import { FrameworkAccuDraw } from "./FrameworkAccuDraw";
import { UiFramework } from "../UiFramework";
import { SvgLock } from "@itwin/itwinui-icons-react";
function isLetter(char: string): boolean {
return char.length === 1 && char.toLowerCase() !== char.toUpperCase();
}
/** Properties for [[AccuDrawInputField]] component
* @beta
*/
export interface AccuDrawInputFieldProps extends CommonProps {
/** Which AccuDraw field this represents */
field: ItemField;
/** id for the input element */
id: string;
/** Indicates whether field is locked */
isLocked?: boolean;
/** label for the input element */
label?: string;
/** icon for the input element */
iconSpec?: IconSpec;
/** Custom CSS class name for the label */
labelClassName?: string;
/** Custom CSS Style for the label */
labelStyle?: React.CSSProperties;
/** Center justified label */
labelCentered?: boolean;
/** Triggered when the content is changed */
onValueChanged: (stringValue: string) => void;
/** Frequency to poll for changes in value, in milliseconds */
valueChangedDelay?: number;
/** Listens for <Enter> keypress */
onEnterPressed?: () => void;
/** Listens for <Esc> keypress */
onEscPressed?: () => void;
/** Provides ability to return reference to HTMLInputElement */
ref?: React.Ref<HTMLInputElement>;
}
const ForwardRefAccuDrawInput = React.forwardRef<
HTMLInputElement,
AccuDrawInputFieldProps
>(function ForwardRefAccuDrawInputField(props: AccuDrawInputFieldProps, ref) {
const {
className,
style,
id,
label,
iconSpec,
labelClassName,
labelStyle,
labelCentered,
field,
isLocked,
onValueChanged,
valueChangedDelay,
onEnterPressed,
onEscPressed,
...inputProps
} = props;
const [stringValue, setStringValue] = React.useState("");
const timeoutId = React.useRef(0);
const [needValueChanged, setNeedValueChanged] = React.useState(false);
const [needSelection, setNeedSelection] = React.useState(false);
const [isFocusField, setIsFocusField] = React.useState(false);
const inputElementRef = React.useRef<HTMLInputElement>(null);
const refs = useRefs(inputElementRef, ref); // combine ref needed for target with the forwardRef needed by the Parent when parent is a Type Editor.
React.useEffect(() => {
const formattedValue = FrameworkAccuDraw.getFieldDisplayValue(field);
setStringValue(formattedValue);
}, [field]);
const handleChange = React.useCallback(
(event: React.ChangeEvent<HTMLInputElement>): void => {
const value = event.currentTarget.value;
// istanbul ignore next
if (value === undefined) return;
// istanbul ignore else
if (stringValue !== value) {
setStringValue(value);
setNeedValueChanged(true);
}
},
[stringValue]
);
const unsetTimeout = (): void => {
// istanbul ignore else
if (timeoutId) {
window.clearTimeout(timeoutId.current);
timeoutId.current = 0;
}
};
React.useEffect(() => {
// After setStringValue & re-render
// istanbul ignore else
if (needValueChanged) {
if (valueChangedDelay) {
unsetTimeout();
timeoutId.current = window.setTimeout(() => {
onValueChanged(stringValue);
}, valueChangedDelay);
} else {
onValueChanged(stringValue);
}
setNeedValueChanged(false);
}
}, [onValueChanged, valueChangedDelay, stringValue, needValueChanged]);
React.useEffect(() => {
// On unmount
return () => unsetTimeout();
}, []);
const handleKeyDown = React.useCallback(
(e: React.KeyboardEvent) => {
switch (e.key) {
case Key.Escape.valueOf():
onEscPressed && onEscPressed();
return;
case Key.Enter.valueOf():
onEnterPressed && onEnterPressed();
return;
}
if (isLetter(e.key)) {
e.preventDefault();
UiFramework.keyboardShortcuts.processKey(e.key);
return;
}
},
[onEscPressed, onEnterPressed]
);
React.useEffect(() => {
return FrameworkAccuDraw.onAccuDrawSetFieldValueToUiEvent.addListener(
(args) => {
if (args.field === field && stringValue !== args.formattedValue) {
setStringValue(args.formattedValue);
// istanbul ignore else
if (isFocusField) setNeedSelection(true);
}
}
);
}, [field, isFocusField, stringValue]);
React.useEffect(() => {
if (needSelection) {
// istanbul ignore else
if (inputElementRef.current) inputElementRef.current.select();
setNeedSelection(false);
}
}, [needSelection]);
React.useEffect(() => {
return FrameworkAccuDraw.onAccuDrawSetFieldFocusEvent.addListener(
(args) => {
setIsFocusField(args.field === field);
}
);
}, [field]);
const inputClassNames = classnames("uifw-accudraw-input-field", className);
const labelClassNames = classnames(
"uifw-accudraw-input-label",
labelCentered && "uifw-label-centered",
labelClassName
);
return (
<>
<label htmlFor={id} className={labelClassNames} style={labelStyle}>
{label}
{iconSpec && <Icon iconSpec={iconSpec} />}
</label>
<Input
{...inputProps}
ref={refs}
id={id}
value={stringValue}
className={inputClassNames}
style={style}
autoComplete="off"
onChange={handleChange}
onKeyDown={handleKeyDown}
size="small"
/>
<span className="uifw-accudraw-lock">
{isLocked && <Icon iconSpec={<SvgLock />} />}
</span>
</>
);
});
/** Input field for AccuDraw UI.
* @beta
*/
export const AccuDrawInputField: (
props: AccuDrawInputFieldProps
) => React.ReactElement | null = ForwardRefAccuDrawInput;