2021-02-16 10:29:08 +00:00
|
|
|
import React from "react";
|
|
|
|
|
import BaseControl, { ControlProps } from "./BaseControl";
|
|
|
|
|
import { StyledDynamicInput } from "./StyledControls";
|
2021-08-04 05:34:44 +00:00
|
|
|
import CodeEditor, {
|
|
|
|
|
CodeEditorExpected,
|
|
|
|
|
} from "components/editorComponents/CodeEditor";
|
2021-02-16 10:29:08 +00:00
|
|
|
import {
|
|
|
|
|
EditorModes,
|
|
|
|
|
EditorSize,
|
|
|
|
|
EditorTheme,
|
|
|
|
|
TabBehaviour,
|
|
|
|
|
} from "components/editorComponents/CodeEditor/EditorConfig";
|
|
|
|
|
import { ColumnProperties } from "components/designSystems/appsmith/TableComponent/Constants";
|
|
|
|
|
import { isDynamicValue } from "utils/DynamicBindingUtils";
|
|
|
|
|
import styled from "styled-components";
|
|
|
|
|
import {
|
|
|
|
|
JSToString,
|
|
|
|
|
stringToJS,
|
2021-07-07 03:46:16 +00:00
|
|
|
} from "components/editorComponents/ActionCreator/Fields";
|
2021-02-16 10:29:08 +00:00
|
|
|
|
2021-03-15 12:17:56 +00:00
|
|
|
const PromptMessage = styled.span`
|
|
|
|
|
line-height: 17px;
|
|
|
|
|
`;
|
2021-02-16 10:29:08 +00:00
|
|
|
const CurlyBraces = styled.span`
|
2021-03-15 12:17:56 +00:00
|
|
|
color: ${(props) => props.theme.colors.codeMirror.background.hoverState};
|
|
|
|
|
background-color: #ffffff;
|
2021-02-16 10:29:08 +00:00
|
|
|
border-radius: 2px;
|
|
|
|
|
padding: 2px;
|
|
|
|
|
margin: 0px 2px;
|
2021-03-15 12:17:56 +00:00
|
|
|
font-size: 10px;
|
2021-02-16 10:29:08 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
export function InputText(props: {
|
|
|
|
|
label: string;
|
|
|
|
|
value: string;
|
|
|
|
|
onChange: (event: React.ChangeEvent<HTMLTextAreaElement> | string) => void;
|
|
|
|
|
evaluatedValue?: any;
|
2021-08-04 05:34:44 +00:00
|
|
|
expected?: CodeEditorExpected;
|
2021-02-16 10:29:08 +00:00
|
|
|
placeholder?: string;
|
|
|
|
|
dataTreePath?: string;
|
|
|
|
|
additionalDynamicData: Record<string, Record<string, unknown>>;
|
2021-03-15 12:17:56 +00:00
|
|
|
theme: EditorTheme;
|
2021-02-16 10:29:08 +00:00
|
|
|
}) {
|
|
|
|
|
const {
|
2021-05-13 08:35:39 +00:00
|
|
|
additionalDynamicData,
|
|
|
|
|
dataTreePath,
|
|
|
|
|
evaluatedValue,
|
2021-02-16 10:29:08 +00:00
|
|
|
expected,
|
|
|
|
|
onChange,
|
|
|
|
|
placeholder,
|
2021-03-15 12:17:56 +00:00
|
|
|
theme,
|
2021-05-13 08:35:39 +00:00
|
|
|
value,
|
2021-02-16 10:29:08 +00:00
|
|
|
} = props;
|
|
|
|
|
return (
|
|
|
|
|
<StyledDynamicInput>
|
|
|
|
|
<CodeEditor
|
2021-04-28 10:28:39 +00:00
|
|
|
additionalDynamicData={additionalDynamicData}
|
|
|
|
|
dataTreePath={dataTreePath}
|
|
|
|
|
evaluatedValue={evaluatedValue}
|
|
|
|
|
expected={expected}
|
2021-02-16 10:29:08 +00:00
|
|
|
input={{
|
|
|
|
|
value: value,
|
|
|
|
|
onChange: onChange,
|
|
|
|
|
}}
|
|
|
|
|
mode={EditorModes.TEXT_WITH_BINDING}
|
|
|
|
|
placeholder={placeholder}
|
|
|
|
|
promptMessage={
|
2021-03-15 12:17:56 +00:00
|
|
|
<PromptMessage>
|
2021-02-16 10:29:08 +00:00
|
|
|
Access the current cell using <CurlyBraces>{"{{"}</CurlyBraces>
|
|
|
|
|
currentRow.columnName
|
|
|
|
|
<CurlyBraces>{"}}"}</CurlyBraces>
|
2021-03-15 12:17:56 +00:00
|
|
|
</PromptMessage>
|
2021-02-16 10:29:08 +00:00
|
|
|
}
|
2021-04-28 10:28:39 +00:00
|
|
|
size={EditorSize.EXTENDED}
|
|
|
|
|
tabBehaviour={TabBehaviour.INDENT}
|
|
|
|
|
theme={theme}
|
2021-02-16 10:29:08 +00:00
|
|
|
/>
|
|
|
|
|
</StyledDynamicInput>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ComputeTablePropertyControl extends BaseControl<
|
|
|
|
|
ComputeTablePropertyControlProps
|
|
|
|
|
> {
|
|
|
|
|
render() {
|
|
|
|
|
const {
|
2021-05-13 08:35:39 +00:00
|
|
|
dataTreePath,
|
|
|
|
|
defaultValue,
|
2021-02-16 10:29:08 +00:00
|
|
|
expected,
|
|
|
|
|
label,
|
2021-05-13 08:35:39 +00:00
|
|
|
propertyValue,
|
2021-03-15 12:17:56 +00:00
|
|
|
theme,
|
2021-02-16 10:29:08 +00:00
|
|
|
} = this.props;
|
|
|
|
|
const tableId = this.props.widgetProperties.widgetName;
|
|
|
|
|
const value =
|
|
|
|
|
propertyValue && isDynamicValue(propertyValue)
|
|
|
|
|
? this.getInputComputedValue(propertyValue, tableId)
|
|
|
|
|
: propertyValue
|
|
|
|
|
? propertyValue
|
|
|
|
|
: defaultValue;
|
|
|
|
|
const evaluatedProperties = this.props.widgetProperties;
|
|
|
|
|
|
|
|
|
|
const columns: Record<string, ColumnProperties> =
|
|
|
|
|
evaluatedProperties.primaryColumns || {};
|
|
|
|
|
const currentRow: { [key: string]: any } = {};
|
|
|
|
|
Object.keys(columns).forEach((id: string) => {
|
|
|
|
|
currentRow[id] = undefined;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<InputText
|
|
|
|
|
additionalDynamicData={{
|
|
|
|
|
currentRow,
|
|
|
|
|
}}
|
2021-04-28 10:28:39 +00:00
|
|
|
dataTreePath={dataTreePath}
|
|
|
|
|
expected={expected}
|
|
|
|
|
label={label}
|
|
|
|
|
onChange={this.onTextChange}
|
|
|
|
|
theme={theme}
|
|
|
|
|
value={value}
|
2021-02-16 10:29:08 +00:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getInputComputedValue = (propertyValue: string, tableId: string) => {
|
|
|
|
|
const value = `${propertyValue.substring(
|
2021-07-22 08:43:58 +00:00
|
|
|
`{{${tableId}.sanitizedTableData.map((currentRow) => ( `.length,
|
2021-02-16 10:29:08 +00:00
|
|
|
propertyValue.length - 4,
|
|
|
|
|
)}`;
|
|
|
|
|
const stringValue = JSToString(value);
|
|
|
|
|
|
|
|
|
|
return stringValue;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getComputedValue = (value: string, tableId: string) => {
|
|
|
|
|
const stringToEvaluate = stringToJS(value);
|
2021-07-22 08:43:58 +00:00
|
|
|
return `{{${tableId}.sanitizedTableData.map((currentRow) => ( ${stringToEvaluate}))}}`;
|
2021-02-16 10:29:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
onTextChange = (event: React.ChangeEvent<HTMLTextAreaElement> | string) => {
|
|
|
|
|
let value = "";
|
|
|
|
|
if (typeof event !== "string") {
|
|
|
|
|
value = event.target.value;
|
|
|
|
|
} else {
|
|
|
|
|
value = event;
|
|
|
|
|
}
|
|
|
|
|
if (value) {
|
|
|
|
|
const output = this.getComputedValue(
|
|
|
|
|
value,
|
|
|
|
|
this.props.widgetProperties.widgetName,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
this.updateProperty(this.props.propertyName, output);
|
|
|
|
|
} else {
|
|
|
|
|
this.updateProperty(this.props.propertyName, value);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static getControlType() {
|
|
|
|
|
return "COMPUTE_VALUE";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface ComputeTablePropertyControlProps extends ControlProps {
|
|
|
|
|
defaultValue?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default ComputeTablePropertyControl;
|