PromucFlow_constructor/app/client/src/components/propertyControls/ChartDataControl.tsx

274 lines
7.2 KiB
TypeScript
Raw Normal View History

2020-04-15 11:42:11 +00:00
import React from "react";
import _ from "lodash";
2020-04-15 11:42:11 +00:00
import BaseControl, { ControlProps } from "./BaseControl";
2020-06-10 12:16:50 +00:00
import { ControlWrapper, StyledPropertyPaneButton } from "./StyledControls";
2020-04-15 11:42:11 +00:00
import styled from "constants/DefaultTheme";
import { FormIcons } from "icons/FormIcons";
import { AnyStyledComponent } from "styled-components";
import CodeEditor from "components/editorComponents/CodeEditor";
import {
EditorModes,
EditorSize,
EditorTheme,
TabBehaviour,
} from "components/editorComponents/CodeEditor/EditorConfig";
2020-04-15 11:42:11 +00:00
const StyledOptionControlWrapper = styled(ControlWrapper)`
display: flex;
justify-content: flex-start;
padding: 0;
width: 100%;
`;
const StyledDynamicInput = styled.div`
width: 100%;
&&& {
input {
border: none;
2020-12-24 04:32:25 +00:00
color: ${(props) => props.theme.colors.textOnDarkBG};
background: ${(props) => props.theme.colors.paneInputBG};
2020-04-15 11:42:11 +00:00
&:focus {
border: none;
2020-12-24 04:32:25 +00:00
color: ${(props) => props.theme.colors.textOnDarkBG};
background: ${(props) => props.theme.colors.paneInputBG};
2020-04-15 11:42:11 +00:00
}
}
}
`;
const StyledDeleteIcon = styled(FormIcons.DELETE_ICON as AnyStyledComponent)`
padding: 0;
position: relative;
margin-left: 15px;
cursor: pointer;
`;
type RenderComponentProps = {
index: number;
item: {
seriesName: string;
2020-06-05 16:20:23 +00:00
data: Array<{ x: string; y: string }> | string;
2020-04-15 11:42:11 +00:00
};
length: number;
isValid: boolean;
validationMessage: string;
deleteOption: (index: number) => void;
updateOption: (index: number, key: string, value: string) => void;
2020-06-05 16:20:23 +00:00
evaluated: {
seriesName: string;
data: Array<{ x: string; y: string }> | any;
};
2020-04-15 11:42:11 +00:00
};
function DataControlComponent(props: RenderComponentProps) {
2020-06-05 16:20:23 +00:00
const {
deleteOption,
updateOption,
item,
index,
length,
isValid,
evaluated,
} = props;
2020-04-15 11:42:11 +00:00
return (
<StyledOptionControlWrapper orientation={"VERTICAL"}>
2020-04-29 10:29:02 +00:00
<StyledOptionControlWrapper orientation={"HORIZONTAL"}>
<CodeEditor
2020-06-05 16:20:23 +00:00
expected={"string"}
input={{
value: item.seriesName,
onChange: (
event: React.ChangeEvent<HTMLTextAreaElement> | string,
) => {
let value: string = event as string;
2020-06-05 16:20:23 +00:00
if (typeof event !== "string") {
value = event.target.value;
}
updateOption(index, "seriesName", value);
},
2020-04-29 10:29:02 +00:00
}}
2020-06-11 11:03:16 +00:00
evaluatedValue={evaluated?.seriesName}
theme={EditorTheme.DARK}
size={EditorSize.EXTENDED}
mode={EditorModes.TEXT_WITH_BINDING}
tabBehaviour={TabBehaviour.INPUT}
2020-06-05 16:20:23 +00:00
placeholder="Series Name"
2020-04-29 10:29:02 +00:00
/>
{length > 1 && (
2020-04-15 11:42:11 +00:00
<StyledDeleteIcon
height={20}
width={20}
onClick={() => {
deleteOption(index);
}}
/>
2020-04-29 10:29:02 +00:00
)}
</StyledOptionControlWrapper>
2020-06-05 16:20:23 +00:00
<StyledDynamicInput
className={"t--property-control-chart-series-data-control"}
>
<CodeEditor
2020-06-05 16:20:23 +00:00
expected={`Array<x:string, y:number>`}
2020-04-15 11:42:11 +00:00
input={{
value: item.data,
onChange: (
event: React.ChangeEvent<HTMLTextAreaElement> | string,
) => {
let value: string = event as string;
2020-04-15 11:42:11 +00:00
if (typeof event !== "string") {
value = event.target.value;
}
updateOption(index, "data", value);
},
}}
2020-06-11 11:03:16 +00:00
evaluatedValue={evaluated?.data}
2020-04-15 11:42:11 +00:00
meta={{
2020-06-04 13:49:22 +00:00
error: isValid ? "" : "There is an error",
2020-04-15 11:42:11 +00:00
touched: true,
}}
theme={EditorTheme.DARK}
size={EditorSize.EXTENDED}
mode={EditorModes.JSON_WITH_BINDING}
tabBehaviour={TabBehaviour.INPUT}
2020-04-15 11:42:11 +00:00
placeholder=""
/>
</StyledDynamicInput>
</StyledOptionControlWrapper>
);
}
class ChartDataControl extends BaseControl<ControlProps> {
getValidations = (message: string, isValid: boolean, len: number) => {
const validations: Array<{
isValid: boolean;
validationMessage: string;
}> = [];
let index = -1;
let validationMessage = "";
if (message.indexOf("##") !== -1) {
const messages = message.split("##");
index = Number(messages[0]);
validationMessage = messages[1];
}
for (let i = 0; i < len; i++) {
if (i === index) {
validations.push({
isValid: false,
validationMessage: validationMessage,
});
} else {
validations.push({
isValid: true,
validationMessage: "",
});
}
}
return validations;
};
getEvaluatedValue = () => {
if (Array.isArray(this.props.evaluatedValue)) {
return this.props.evaluatedValue;
}
return [];
};
render() {
const chartData: Array<{ seriesName: string; data: string }> = _.isString(
this.props.propertyValue,
)
? []
: this.props.propertyValue;
const dataLength = chartData.length;
const { validationMessage, isValid } = this.props;
const validations: Array<{
isValid: boolean;
validationMessage: string;
}> = this.getValidations(
validationMessage || "",
isValid,
chartData.length,
);
const evaluatedValue = this.getEvaluatedValue();
if (this.props.widgetProperties.chartType === "PIE_CHART") {
const data = chartData.length
? chartData[0]
: {
seriesName: "",
data: "",
};
return (
<DataControlComponent
index={0}
item={data}
length={1}
deleteOption={this.deleteOption}
updateOption={this.updateOption}
isValid={validations[0].isValid}
validationMessage={validations[0].validationMessage}
evaluated={evaluatedValue[0]}
/>
);
}
2020-04-15 11:42:11 +00:00
return (
<React.Fragment>
{chartData.map((data, index) => {
2020-04-15 11:42:11 +00:00
return (
<DataControlComponent
key={index}
index={index}
item={data}
length={dataLength}
deleteOption={this.deleteOption}
updateOption={this.updateOption}
isValid={validations[index].isValid}
validationMessage={validations[index].validationMessage}
evaluated={evaluatedValue[index]}
2020-04-15 11:42:11 +00:00
/>
);
})}
<StyledPropertyPaneButton
2020-04-29 10:29:02 +00:00
text="Add Series"
2020-04-15 11:42:11 +00:00
icon="plus"
color="#FFFFFF"
minimal
onClick={this.addOption}
/>
</React.Fragment>
);
}
deleteOption = (index: number) => {
this.deleteProperties([`${this.props.propertyName}[${index}]`]);
2020-04-15 11:42:11 +00:00
};
updateOption = (
index: number,
propertyName: string,
updatedValue: string,
) => {
this.updateProperty(
`${this.props.propertyName}[${index}].${propertyName}`,
updatedValue,
);
2020-04-15 11:42:11 +00:00
};
addOption = () => {
const chartData: Array<{
seriesName: string;
data: string;
}> = this.props.propertyValue;
this.updateProperty(`${this.props.propertyName}[${chartData.length}]`, {
seriesName: "",
data: JSON.stringify([{ x: "label", y: 50 }]),
});
2020-04-15 11:42:11 +00:00
};
static getControlType() {
return "CHART_DATA";
}
}
export default ChartDataControl;