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

261 lines
7.1 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";
import {
ControlWrapper,
StyledInputGroup,
StyledPropertyPaneButton,
} from "./StyledControls";
import styled from "constants/DefaultTheme";
import { FormIcons } from "icons/FormIcons";
import { AnyStyledComponent } from "styled-components";
import DynamicAutocompleteInput from "components/editorComponents/DynamicAutocompleteInput";
const StyledOptionControlWrapper = styled(ControlWrapper)`
display: flex;
justify-content: flex-start;
padding: 0;
width: 100%;
`;
const StyledOptionControlInputGroup = styled(StyledInputGroup)`
margin-right: 2px;
width: 100%;
margin-bottom: 0;
&&& {
input {
border: none;
color: ${props => props.theme.colors.textOnDarkBG};
background: ${props => props.theme.colors.paneInputBG};
&:focus {
border: none;
color: ${props => props.theme.colors.textOnDarkBG};
background: ${props => props.theme.colors.paneInputBG};
}
}
}
`;
const StyledDynamicInput = styled.div`
width: 100%;
&&& {
input {
border: none;
color: ${props => props.theme.colors.textOnDarkBG};
background: ${props => props.theme.colors.paneInputBG};
&:focus {
border: none;
color: ${props => props.theme.colors.textOnDarkBG};
background: ${props => props.theme.colors.paneInputBG};
}
}
}
`;
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-05-07 10:51:37 +00:00
data: Array<{ x: string; y: string }> | any;
2020-04-15 11:42:11 +00:00
};
length: number;
isValid: boolean;
validationMessage: string;
2020-04-15 11:42:11 +00:00
deleteOption: Function;
updateOption: Function;
2020-06-04 13:49:22 +00:00
dataTreePath: string;
2020-04-15 11:42:11 +00:00
};
function DataControlComponent(props: RenderComponentProps) {
2020-06-04 13:49:22 +00:00
const { deleteOption, updateOption, item, index, length, isValid } = props;
2020-04-15 11:42:11 +00:00
return (
<StyledOptionControlWrapper orientation={"VERTICAL"}>
2020-04-29 10:29:02 +00:00
<StyledOptionControlWrapper orientation={"HORIZONTAL"}>
<StyledOptionControlInputGroup
type="text"
placeholder="Series Name"
onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
updateOption(index, "seriesName", event.target.value);
}}
defaultValue={item.seriesName}
/>
{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-04-15 11:42:11 +00:00
<StyledDynamicInput>
<DynamicAutocompleteInput
input={{
value: item.data,
onChange: (
event: React.ChangeEvent<HTMLTextAreaElement> | string,
) => {
let value = event;
if (typeof event !== "string") {
value = event.target.value;
}
updateOption(index, "data", value);
},
}}
2020-06-04 13:49:22 +00:00
dataTreePath={`${props.dataTreePath}`}
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={"DARK"}
singleLine={false}
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;
};
2020-04-15 11:42:11 +00:00
render() {
const chartData: Array<{
seriesName: string;
2020-05-07 10:51:37 +00:00
data: Array<{ x: string; y: string }> | any;
}> =
this.props.propertyValue && _.isString(this.props.propertyValue)
? JSON.parse(this.props.propertyValue)
: this.props.propertyValue;
2020-04-15 11:42:11 +00:00
const dataLength = chartData.length;
const { validationMessage, isValid } = this.props;
const validations: Array<{
isValid: boolean;
validationMessage: string;
}> = this.getValidations(
validationMessage || "",
isValid,
chartData.length,
);
2020-04-15 11:42:11 +00:00
return (
<React.Fragment>
{chartData.map((data, index) => {
return (
<DataControlComponent
key={index}
index={index}
item={data}
length={dataLength}
deleteOption={this.deleteOption}
updateOption={this.updateOption}
isValid={validations[index].isValid}
validationMessage={validations[index].validationMessage}
2020-06-04 13:49:22 +00:00
dataTreePath={`${this.props.dataTreePath}`}
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) => {
const chartData: object[] =
this.props.propertyValue && _.isString(this.props.propertyValue)
? JSON.parse(this.props.propertyValue)
: this.props.propertyValue;
2020-04-15 11:42:11 +00:00
chartData.splice(index, 1);
this.updateProperty(this.props.propertyName, JSON.stringify(chartData));
2020-04-15 11:42:11 +00:00
};
updateOption = (
index: number,
propertyName: string,
updatedValue: string,
) => {
const chartData: Array<{
seriesName: string;
2020-05-07 10:51:37 +00:00
data: Array<{ x: string; y: string }> | any;
}> =
this.props.propertyValue && _.isString(this.props.propertyValue)
? JSON.parse(this.props.propertyValue)
: this.props.propertyValue;
const updatedChartData = chartData.map((item, i) => {
if (index === i) {
if (propertyName === "seriesName") {
item.seriesName = updatedValue;
} else {
try {
item.data = JSON.parse(updatedValue);
} catch (err) {
item.data = updatedValue;
2020-04-15 11:42:11 +00:00
}
}
}
return item;
});
this.updateProperty(
this.props.propertyName,
JSON.stringify(updatedChartData),
2020-04-15 11:42:11 +00:00
);
};
addOption = () => {
const chartData: Array<{
seriesName: string;
2020-05-07 10:51:37 +00:00
data: Array<{ x: string; y: string }> | any;
}> =
this.props.propertyValue && _.isString(this.props.propertyValue)
? JSON.parse(this.props.propertyValue)
: this.props.propertyValue;
2020-04-15 11:42:11 +00:00
chartData.push({ seriesName: "", data: [{ x: "", y: "" }] });
this.updateProperty(this.props.propertyName, JSON.stringify(chartData));
2020-04-15 11:42:11 +00:00
};
static getControlType() {
return "CHART_DATA";
}
}
export default ChartDataControl;