import React from "react"; import BaseControl, { ControlProps } from "./BaseControl"; import { StyledDynamicInput } from "./StyledControls"; import CodeEditor, { CodeEditorExpected, } from "components/editorComponents/CodeEditor"; import { EditorModes, EditorSize, EditorTheme, TabBehaviour, } from "components/editorComponents/CodeEditor/EditorConfig"; import { ColumnProperties } from "widgets/TableWidgetV2/component/Constants"; import { isDynamicValue } from "utils/DynamicBindingUtils"; import styled from "styled-components"; import { isString } from "utils/helpers"; import { JSToString, stringToJS, } from "components/editorComponents/ActionCreator/utils"; import { AdditionalDynamicDataTree } from "utils/autocomplete/customTreeTypeDefCreator"; const PromptMessage = styled.span` line-height: 17px; `; const CurlyBraces = styled.span` color: ${(props) => props.theme.colors.codeMirror.background.hoverState}; background-color: #ffffff; border-radius: 2px; padding: 2px; margin: 0px 2px; font-size: 10px; `; type InputTextProp = { label: string; value: string; onChange: (event: React.ChangeEvent | string) => void; evaluatedValue?: any; expected?: CodeEditorExpected; placeholder?: string; dataTreePath?: string; additionalDynamicData: AdditionalDynamicDataTree; theme: EditorTheme; }; function InputText(props: InputTextProp) { const { additionalDynamicData, dataTreePath, evaluatedValue, expected, onChange, placeholder, theme, value, } = props; return ( Access the current cell using {"{{"} currentRow.columnName {"}}"} } size={EditorSize.EXTENDED} tabBehaviour={TabBehaviour.INDENT} theme={theme} /> ); } class ComputeTablePropertyControlV2 extends BaseControl< ComputeTablePropertyControlPropsV2 > { static getBindingPrefix(tableName: string) { return `{{${tableName}.processedTableData.map((currentRow, currentIndex) => ( `; } static bindingSuffix = `))}}`; render() { const { dataTreePath, defaultValue, expected, label, propertyValue, theme, } = this.props; const tableName = this.props.widgetProperties.widgetName; const value = propertyValue && isDynamicValue(propertyValue) ? ComputeTablePropertyControlV2.getInputComputedValue( propertyValue, tableName, ) : propertyValue ? propertyValue : defaultValue; const evaluatedProperties = this.props.widgetProperties; const columns: Record = evaluatedProperties.primaryColumns || {}; const currentRow: { [key: string]: any } = {}; Object.values(columns).forEach((column) => { currentRow[column.alias || column.originalId] = undefined; }); // Load default value in evaluated value if (value && !propertyValue) { this.onTextChange(value); } return ( ); } static getInputComputedValue = (propertyValue: string, tableName: string) => { const bindingPrefix = ComputeTablePropertyControlV2.getBindingPrefix( tableName, ); if (propertyValue.includes(bindingPrefix)) { const value = `${propertyValue.substring( bindingPrefix.length, propertyValue.length - ComputeTablePropertyControlV2.bindingSuffix.length, )}`; return JSToString(value); } else { return propertyValue; } }; getComputedValue = (value: string, tableName: string) => { if ( !isDynamicValue(value) && !this.props.additionalControlData?.isArrayValue ) { return value; } const stringToEvaluate = stringToJS(value); if (stringToEvaluate === "") { return stringToEvaluate; } return `${ComputeTablePropertyControlV2.getBindingPrefix( tableName, )}${stringToEvaluate}${ComputeTablePropertyControlV2.bindingSuffix}`; }; onTextChange = (event: React.ChangeEvent | string) => { let value = ""; if (typeof event !== "string") { value = event.target?.value; } else { value = event; } if (isString(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 "TABLE_COMPUTE_VALUE"; } } export interface ComputeTablePropertyControlPropsV2 extends ControlProps { defaultValue?: string; } export default ComputeTablePropertyControlV2;