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

284 lines
7.3 KiB
TypeScript
Raw Normal View History

2020-04-15 11:42:11 +00:00
import React from "react";
import { get, has, isString } 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";
import { Size, Category } from "components/ads/Button";
import { AllChartData, ChartData } from "widgets/ChartWidget";
import { generateReactKey } from "utils/generators";
const Wrapper = styled.div`
background-color: ${(props) =>
props.theme.colors.propertyPane.dropdownSelectBg};
padding: 0 8px;
`;
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;
&&& svg {
path {
fill: ${(props) => props.theme.colors.propertyPane.jsIconBg};
}
}
`;
const ActionHolder = styled.div`
display: flex;
justify-content: space-between;
align-items: center;
width: 100%;
`;
const StyledLabel = styled.label`
margin: 8px auto 8px 0;
&& {
color: ${(props) => props.theme.colors.propertyPane.label};
}
`;
const Box = styled.div`
height: 16px;
2020-04-15 11:42:11 +00:00
`;
type RenderComponentProps = {
index: string;
item: ChartData;
length: number;
validationMessage: {
data: string;
2020-04-15 11:42:11 +00:00
seriesName: string;
};
deleteOption: (index: string) => void;
updateOption: (index: string, key: string, value: string) => void;
2020-06-05 16:20:23 +00:00
evaluated: {
seriesName: string;
data: Array<{ x: string; y: string }> | any;
};
theme: EditorTheme;
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,
evaluated,
validationMessage,
2020-06-05 16:20:23 +00:00
} = props;
2020-04-15 11:42:11 +00:00
return (
<StyledOptionControlWrapper orientation={"VERTICAL"}>
<ActionHolder>
2021-03-17 11:34:53 +00:00
<StyledLabel>Series Title</StyledLabel>
{length > 1 && (
<StyledDeleteIcon
height={20}
width={20}
onClick={() => {
deleteOption(index);
}}
/>
)}
</ActionHolder>
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={props.theme}
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
/>
</StyledOptionControlWrapper>
<StyledLabel>Series Data</StyledLabel>
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={{
error: has(validationMessage, "data")
? get(validationMessage, "data")
: "",
2020-04-15 11:42:11 +00:00
touched: true,
}}
theme={props.theme}
size={EditorSize.EXTENDED}
mode={EditorModes.JSON_WITH_BINDING}
tabBehaviour={TabBehaviour.INPUT}
2020-04-15 11:42:11 +00:00
placeholder=""
/>
</StyledDynamicInput>
<Box></Box>
2020-04-15 11:42:11 +00:00
</StyledOptionControlWrapper>
);
}
class ChartDataControl extends BaseControl<ControlProps> {
render() {
const chartData: AllChartData = isString(this.props.propertyValue)
? {}
: this.props.propertyValue;
const dataLength = Object.keys(chartData).length;
const { validationMessage } = this.props;
const evaluatedValue = this.props.evaluatedValue;
const firstKey = Object.keys(chartData)[0] as string;
if (this.props.widgetProperties.chartType === "PIE_CHART") {
const data = dataLength
? get(chartData, `${firstKey}`)
: {
seriesName: "",
data: [],
};
return (
<DataControlComponent
index={firstKey}
item={data}
length={1}
deleteOption={this.deleteOption}
updateOption={this.updateOption}
validationMessage={get(validationMessage, `${firstKey}`)}
evaluated={get(evaluatedValue, `${firstKey}`)}
theme={this.props.theme}
/>
);
}
2020-04-15 11:42:11 +00:00
return (
<React.Fragment>
<Wrapper>
{Object.keys(chartData).map((key: string) => {
const data = get(chartData, `${key}`);
return (
<DataControlComponent
key={key}
index={key}
item={data}
length={dataLength}
deleteOption={this.deleteOption}
updateOption={this.updateOption}
validationMessage={get(validationMessage, `${key}`)}
evaluated={get(evaluatedValue, `${key}`)}
theme={this.props.theme}
/>
);
})}
</Wrapper>
2020-04-15 11:42:11 +00:00
<StyledPropertyPaneButton
icon="plus"
tag="button"
type="button"
text="Add Series"
2020-04-15 11:42:11 +00:00
onClick={this.addOption}
size={Size.medium}
category={Category.tertiary}
2020-04-15 11:42:11 +00:00
/>
</React.Fragment>
);
}
deleteOption = (index: string) => {
this.deleteProperties([`${this.props.propertyName}.${index}`]);
2020-04-15 11:42:11 +00:00
};
updateOption = (
index: string,
2020-04-15 11:42:11 +00:00
propertyName: string,
updatedValue: string,
) => {
this.updateProperty(
`${this.props.propertyName}.${index}.${propertyName}`,
updatedValue,
);
2020-04-15 11:42:11 +00:00
};
/**
* it adds new series data object in the chartData
*/
2020-04-15 11:42:11 +00:00
addOption = () => {
const randomString = generateReactKey();
this.updateProperty(`${this.props.propertyName}.${randomString}`, {
seriesName: "",
data: JSON.stringify([{ x: "label", y: 50 }]),
});
2020-04-15 11:42:11 +00:00
};
static getControlType() {
return "CHART_DATA";
}
}
export default ChartDataControl;