import React, { useEffect } from "react"; import { FieldArray, WrappedFieldArrayProps } from "redux-form"; import styled from "styled-components"; import { Icon } from "@blueprintjs/core"; import { FormIcons } from "icons/FormIcons"; import BaseControl, { ControlProps, ControlData } from "./BaseControl"; import TextField from "components/editorComponents/form/fields/TextField"; import { ControlType } from "constants/PropertyControlConstants"; import DynamicTextField from "components/editorComponents/form/fields/DynamicTextField"; import FormLabel from "components/editorComponents/FormLabel"; import { InputType } from "widgets/InputWidget"; const FormRowWithLabel = styled.div` display: flex; flex: 1; flex-direction: row; & svg { cursor: pointer; } `; const KeyValueRow = (props: Props & WrappedFieldArrayProps) => { const { extraData = [] } = props; const keyName = getFieldName(extraData[0].configProperty); const valueName = getFieldName(extraData[1].configProperty); const valueDataType = getType(extraData[1].dataType); let isRequired: boolean | undefined; useEffect(() => { // Always maintain 1 row if (props.fields.length < 1) { for (let i = props.fields.length; i < 1; i += 1) { props.fields.push({ [keyName[1]]: "", [valueName[1]]: "" }); } } }, [props.fields, keyName, valueName]); useEffect(() => { if (typeof props.fields.getAll() === "string") { const fieldsValue: [] = JSON.parse(`${props.fields.getAll()}`); props.fields.removeAll(); fieldsValue.forEach((value, index) => { props.fields.insert(index, value); }); } }, [props.fields]); if (extraData) { isRequired = extraData[0].isRequired || extraData[1].isRequired; } return ( {typeof props.fields.getAll() === "object" && ( {props.fields.map((field: any, index: number) => (
{extraData && extraData[0].label} {isRequired && "*"}
{!props.actionConfig && (
{extraData && extraData[1].label} {isRequired && "*"}
{index === props.fields.length - 1 ? ( props.fields.push({ key: "", value: "" }) } color={"#A3B3BF"} style={{ alignSelf: "center" }} /> ) : ( props.fields.remove(index)} style={{ alignSelf: "center" }} /> )}
)} {props.actionConfig && ( )}
))}
)}
); }; type Props = { name: string; label: string; rightIcon?: Function; description?: string; actionConfig?: any; extraData?: ControlData[]; isRequired?: boolean; }; class KeyValueFieldArray extends BaseControl { render() { const name = getFieldName(this.props.configProperty); return ( ); } getControlType(): ControlType { return "KEYVALUE_ARRAY"; } } const getFieldName = (configProperty: string) => { return configProperty.split("[*]."); }; const getType = (dataType: InputType | undefined) => { switch (dataType) { case "PASSWORD": return "password"; case "NUMBER": return "number"; default: return "text"; } }; export interface KeyValueArrayProps extends ControlProps { name: string; label: string; rightIcon?: Function; description?: string; actionConfig?: any; } export default KeyValueFieldArray;