2020-04-28 06:52:53 +00:00
|
|
|
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";
|
2020-07-01 10:01:07 +00:00
|
|
|
import HelperTooltip from "components/editorComponents/HelperTooltip";
|
2020-07-28 05:15:26 +00:00
|
|
|
import { Colors } from "constants/Colors";
|
2020-04-28 06:52:53 +00:00
|
|
|
|
|
|
|
|
const FormRowWithLabel = styled.div`
|
|
|
|
|
display: flex;
|
|
|
|
|
flex: 1;
|
|
|
|
|
flex-direction: row;
|
|
|
|
|
& svg {
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
2020-07-01 07:21:02 +00:00
|
|
|
const StyledTextField = styled(TextField)`
|
|
|
|
|
input[type="number"]::-webkit-inner-spin-button,
|
|
|
|
|
input[type="number"]::-webkit-outer-spin-button {
|
|
|
|
|
-webkit-appearance: none;
|
|
|
|
|
margin: 0;
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
2020-04-28 06:52:53 +00:00
|
|
|
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(() => {
|
2020-05-19 07:03:11 +00:00
|
|
|
// Always maintain 1 row
|
|
|
|
|
if (props.fields.length < 1) {
|
|
|
|
|
for (let i = props.fields.length; i < 1; i += 1) {
|
2020-04-28 06:52:53 +00:00
|
|
|
props.fields.push({ [keyName[1]]: "", [valueName[1]]: "" });
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-04 13:49:22 +00:00
|
|
|
}, [props.fields, keyName, valueName]);
|
2020-04-28 06:52:53 +00:00
|
|
|
|
|
|
|
|
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 (
|
|
|
|
|
<React.Fragment>
|
|
|
|
|
{typeof props.fields.getAll() === "object" && (
|
|
|
|
|
<React.Fragment>
|
2020-07-01 10:01:07 +00:00
|
|
|
{props.fields.map((field: any, index: number) => {
|
|
|
|
|
const otherProps: Record<string, any> = {};
|
|
|
|
|
if (
|
|
|
|
|
props.actionConfig &&
|
|
|
|
|
props.actionConfig[index].description &&
|
|
|
|
|
props.rightIcon
|
|
|
|
|
) {
|
|
|
|
|
otherProps.rightIcon = (
|
|
|
|
|
<HelperTooltip
|
|
|
|
|
description={props.actionConfig[index].description}
|
|
|
|
|
rightIcon={
|
|
|
|
|
props.actionConfig[index].description && props.rightIcon
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return (
|
|
|
|
|
<FormRowWithLabel key={index} style={{ marginTop: 16 }}>
|
|
|
|
|
<div style={{ width: "50vh" }}>
|
2020-04-28 06:52:53 +00:00
|
|
|
<FormLabel>
|
2020-07-01 10:01:07 +00:00
|
|
|
{extraData && extraData[0].label} {isRequired && "*"}
|
2020-04-28 06:52:53 +00:00
|
|
|
</FormLabel>
|
2020-07-01 10:01:07 +00:00
|
|
|
<TextField name={`${field}.${keyName[1]}`} />
|
|
|
|
|
</div>
|
|
|
|
|
{!props.actionConfig && (
|
|
|
|
|
<div style={{ marginLeft: 16 }}>
|
|
|
|
|
<FormLabel>
|
|
|
|
|
{extraData && extraData[1].label} {isRequired && "*"}
|
|
|
|
|
</FormLabel>
|
|
|
|
|
<div style={{ display: "flex", flexDirection: "row" }}>
|
|
|
|
|
<div style={{ marginRight: 14, width: 72 }}>
|
|
|
|
|
<StyledTextField
|
|
|
|
|
name={`${field}.${valueName[1]}`}
|
|
|
|
|
type={valueDataType}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
{index === props.fields.length - 1 ? (
|
|
|
|
|
<Icon
|
|
|
|
|
icon="plus"
|
|
|
|
|
iconSize={20}
|
|
|
|
|
onClick={() =>
|
|
|
|
|
props.fields.push({ key: "", value: "" })
|
|
|
|
|
}
|
2020-07-28 05:15:26 +00:00
|
|
|
color={Colors["CADET_BLUE"]}
|
2020-07-01 10:01:07 +00:00
|
|
|
style={{ alignSelf: "center" }}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<FormIcons.DELETE_ICON
|
|
|
|
|
height={20}
|
|
|
|
|
width={20}
|
2020-07-28 05:15:26 +00:00
|
|
|
color={Colors["CADET_BLUE"]}
|
2020-07-01 10:01:07 +00:00
|
|
|
onClick={() => props.fields.remove(index)}
|
|
|
|
|
style={{ alignSelf: "center" }}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2020-04-28 06:52:53 +00:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2020-07-01 10:01:07 +00:00
|
|
|
)}
|
2020-04-28 06:52:53 +00:00
|
|
|
|
2020-07-01 10:01:07 +00:00
|
|
|
{props.actionConfig && (
|
|
|
|
|
<DynamicTextField
|
|
|
|
|
name={`${field}.value`}
|
|
|
|
|
placeholder={
|
|
|
|
|
props.actionConfig[index].mandatory &&
|
|
|
|
|
props.actionConfig[index].type
|
|
|
|
|
? `Value (Type: ${props.actionConfig[index].type})`
|
|
|
|
|
: `Value (optional)`
|
|
|
|
|
}
|
|
|
|
|
{...otherProps}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</FormRowWithLabel>
|
|
|
|
|
);
|
|
|
|
|
})}
|
2020-04-28 06:52:53 +00:00
|
|
|
</React.Fragment>
|
|
|
|
|
)}
|
|
|
|
|
</React.Fragment>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
name: string;
|
|
|
|
|
label: string;
|
|
|
|
|
rightIcon?: Function;
|
|
|
|
|
description?: string;
|
|
|
|
|
actionConfig?: any;
|
|
|
|
|
extraData?: ControlData[];
|
|
|
|
|
isRequired?: boolean;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class KeyValueFieldArray extends BaseControl<KeyValueArrayProps> {
|
|
|
|
|
render() {
|
|
|
|
|
const name = getFieldName(this.props.configProperty);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<FieldArray
|
|
|
|
|
name={name[0]}
|
|
|
|
|
component={KeyValueRow}
|
|
|
|
|
rerenderOnEveryChange={false}
|
|
|
|
|
{...this.props}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|