import React from "react"; import BaseControl, { ControlProps } from "./BaseControl"; import { StyledDynamicInput } from "./StyledControls"; import CodeEditor from "components/editorComponents/CodeEditor"; 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, } from "components/editorComponents/ActionCreator"; 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; `; export function InputText(props: { label: string; value: string; onChange: (event: React.ChangeEvent | string) => void; isValid: boolean; errorMessage?: string; evaluatedValue?: any; expected?: string; placeholder?: string; dataTreePath?: string; additionalDynamicData: Record>; theme: EditorTheme; }) { const { errorMessage, expected, value, isValid, onChange, placeholder, dataTreePath, evaluatedValue, additionalDynamicData, theme, } = props; return ( Access the current cell using {"{{"} currentRow.columnName {"}}"} } /> ); } class ComputeTablePropertyControl extends BaseControl< ComputeTablePropertyControlProps > { render() { const { expected, propertyValue, isValid, label, dataTreePath, validationMessage, defaultValue, theme, } = 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 = evaluatedProperties.primaryColumns || {}; const currentRow: { [key: string]: any } = {}; Object.keys(columns).forEach((id: string) => { currentRow[id] = undefined; }); return ( ); } getInputComputedValue = (propertyValue: string, tableId: string) => { const value = `${propertyValue.substring( `{{${tableId}.sanitizedTableData.map((currentRow) => { return `.length, propertyValue.length - 4, )}`; const stringValue = JSToString(value); return stringValue; }; getComputedValue = (value: string, tableId: string) => { const stringToEvaluate = stringToJS(value); return `{{${tableId}.sanitizedTableData.map((currentRow) => { return ${stringToEvaluate}})}}`; }; onTextChange = (event: React.ChangeEvent | 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;