-
Notifications
You must be signed in to change notification settings - Fork 2
/
ToggleEditor.tsx
191 lines (169 loc) · 5.22 KB
/
ToggleEditor.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
/*---------------------------------------------------------------------------------------------
* 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 "./ToggleEditor.scss";
import classnames from "classnames";
import * as React from "react";
import type { PropertyValue } from "@itwin/appui-abstract";
import {
PropertyValueFormat,
StandardEditorNames,
StandardTypeNames,
} from "@itwin/appui-abstract";
import type { PropertyEditorProps, TypeEditor } from "./EditorContainer";
import {
PropertyEditorBase,
PropertyEditorManager,
} from "./PropertyEditorManager";
import { ToggleSwitch } from "@itwin/itwinui-react";
/** @internal */
interface ToggleEditorState {
toggleValue: boolean;
isDisabled?: boolean;
}
/** ToggleEditor React component that is a property editor with checkbox input
* @public
*/
export class ToggleEditor
extends React.PureComponent<PropertyEditorProps, ToggleEditorState>
implements TypeEditor
{
private _isMounted = false;
private _inputElement = React.createRef<HTMLInputElement>();
/** @internal */
public override readonly state: Readonly<ToggleEditorState> = {
toggleValue: false,
isDisabled: 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 = {
valueFormat: PropertyValueFormat.Primitive,
value: this.state.toggleValue,
displayValue: "",
};
}
return propertyValue;
}
public get htmlElement(): HTMLElement | null {
return this._inputElement.current;
}
public get hasFocus(): boolean {
return document.activeElement === this._inputElement.current;
}
private _updateToggleValue = (
e: React.ChangeEvent<HTMLInputElement>
): any => {
// istanbul ignore else
if (this._isMounted) {
// istanbul ignore else
if (this._isMounted) {
const toggleValue = !!e.target.checked;
this.setState(
{
toggleValue,
},
async () => {
// istanbul ignore else
if (this.props.propertyRecord && this.props.onCommit) {
const propertyValue = await this.getPropertyValue();
// istanbul ignore else
if (propertyValue !== undefined) {
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 toggleValue = false;
let isDisabled = false;
// istanbul ignore else
if (
propertyRecord &&
propertyRecord.value.valueFormat === PropertyValueFormat.Primitive
) {
const primitiveValue = propertyRecord.value.value;
toggleValue = primitiveValue as boolean;
}
// istanbul ignore else
if (propertyRecord && propertyRecord.isDisabled)
isDisabled = propertyRecord.isDisabled;
// istanbul ignore else
if (this._isMounted) this.setState({ toggleValue, isDisabled });
}
/** @internal */
public override render() {
const className = classnames(
"components-cell-editor",
"components-toggle-editor",
this.props.className
);
const isChecked = this.state.toggleValue;
const isDisabled = !!this.state.isDisabled;
return (
<ToggleSwitch
ref={this._inputElement}
onBlur={this.props.onBlur}
className={className}
style={this.props.style}
checked={isChecked}
disabled={isDisabled}
onChange={this._updateToggleValue}
data-testid="components-toggle-editor"
autoFocus={this.props.setFocus} // eslint-disable-line jsx-a11y/no-autofocus
/>
);
}
}
/** Toggle Property Editor registered for the "bool" and "boolean" type names and "toggle" editor name.
* It uses the [[ToggleEditor]] React component.
* @public
*/
export class TogglePropertyEditor extends PropertyEditorBase {
// istanbul ignore next
public override get containerHandlesBlur(): boolean {
return false;
}
public get reactNode(): React.ReactNode {
return <ToggleEditor />;
}
}
PropertyEditorManager.registerEditor(
StandardTypeNames.Bool,
TogglePropertyEditor,
StandardEditorNames.Toggle
);
PropertyEditorManager.registerEditor(
StandardTypeNames.Boolean,
TogglePropertyEditor,
StandardEditorNames.Toggle
);