-
Notifications
You must be signed in to change notification settings - Fork 2
/
ColorEditor.tsx
208 lines (188 loc) · 5.95 KB
/
ColorEditor.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
/*---------------------------------------------------------------------------------------------
* 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 "./ColorEditor.scss";
import classnames from "classnames";
import * as React from "react";
import { ColorDef } from "@itwin/core-common";
import type {
ColorEditorParams,
PropertyEditorParams,
PropertyRecord,
PropertyValue,
} from "@itwin/appui-abstract";
import {
PropertyEditorParamTypes,
PropertyValueFormat,
StandardEditorNames,
StandardTypeNames,
} from "@itwin/appui-abstract";
import type { PropertyEditorProps, TypeEditor } from "@itwin/components-react";
import {
PropertyEditorBase,
PropertyEditorManager,
} from "@itwin/components-react";
import { ColorPickerButton } from "../color/ColorPickerButton";
/** @internal */
interface ColorEditorState {
colorValue: number;
readonly: boolean;
isDisabled?: boolean;
numColumns: number;
availableColors: ColorDef[];
}
/** ColorEditor React component that is a property editor with text input
* @beta
*/
export class ColorEditor
extends React.PureComponent<PropertyEditorProps, ColorEditorState>
implements TypeEditor
{
private _buttonElement: React.RefObject<HTMLButtonElement> =
React.createRef();
/** @internal */
public override readonly state: Readonly<ColorEditorState> = {
colorValue: 0,
readonly: false,
numColumns: 4,
availableColors: [],
};
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.colorValue,
displayValue: "",
};
}
return propertyValue;
}
public get htmlElement(): HTMLElement | null {
return this._buttonElement.current;
}
public get hasFocus(): boolean {
return document.activeElement === this._buttonElement.current;
}
private setFocus(): void {
// istanbul ignore else
if (
this._buttonElement &&
this._buttonElement.current &&
!this.state.isDisabled
) {
this._buttonElement.current.focus();
}
}
private _onColorPick = (color: ColorDef) => {
const propertyRecord = this.props.propertyRecord as PropertyRecord;
this.setState(
{
colorValue: color.tbgr,
},
async () => {
// istanbul ignore else
if (propertyRecord && this.props.onCommit) {
const propertyValue = await this.getPropertyValue();
// istanbul ignore else
if (propertyValue !== undefined) {
this.props.onCommit({ propertyRecord, newValue: propertyValue });
}
}
}
);
};
/** @internal */
public override componentDidMount() {
void this.setStateFromProps();
}
/** @internal */
public override componentDidUpdate(prevProps: PropertyEditorProps) {
if (this.props.propertyRecord !== prevProps.propertyRecord) {
void this.setStateFromProps();
}
}
private async setStateFromProps() {
const record = this.props.propertyRecord;
// istanbul ignore else
if (record && record.value.valueFormat === PropertyValueFormat.Primitive) {
const colorValue = record.value.value as number;
let numColumns = 4;
const availableColors = new Array<ColorDef>();
const readonly =
record && undefined !== record.isReadonly
? record.isReadonly
: /* istanbul ignore next */ false;
const isDisabled = record
? record.isDisabled
: /* istanbul ignore next */ undefined;
if (record.property.editor && record.property.editor.params) {
const colorParams = record.property.editor.params.find(
(param: PropertyEditorParams) =>
param.type === PropertyEditorParamTypes.ColorData.valueOf()
) as ColorEditorParams;
// istanbul ignore else
if (colorParams) {
colorParams.colorValues.forEach((colorNumber: number) => {
availableColors.push(ColorDef.create(colorNumber));
});
// istanbul ignore else
if (colorParams.numColumns) numColumns = colorParams.numColumns;
}
}
this.setState(
{ colorValue, readonly, isDisabled, numColumns, availableColors },
() => {
if (this.props.setFocus) {
this.setFocus();
}
}
);
}
}
/** @internal */
public override render() {
const colorDef = ColorDef.create(this.state.colorValue);
return (
<div
className={classnames("components-color-editor", this.props.className)}
style={this.props.style}
>
<ColorPickerButton // eslint-disable-line deprecation/deprecation
ref={this._buttonElement}
initialColor={colorDef}
colorDefs={
this.state.availableColors.length > 0
? this.state.availableColors
: [colorDef]
}
numColumns={this.state.numColumns}
disabled={this.state.isDisabled ? true : false}
readonly={this.state.readonly}
onColorPick={this._onColorPick}
data-testid="components-color-editor"
/>
</div>
);
}
}
/** Color Property Editor registered for the "number" type name and "color-picker" editor name.
* It uses the [[ColorEditor]] React component.
* @beta
*/
export class ColorPropertyEditor extends PropertyEditorBase {
public get reactNode(): React.ReactNode {
return <ColorEditor />;
}
}
PropertyEditorManager.registerEditor(
StandardTypeNames.Number,
ColorPropertyEditor,
StandardEditorNames.ColorPicker
);