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