2020-10-12 13:06:05 +00:00
|
|
|
import React, { useEffect, useCallback, JSXElementConstructor } from "react";
|
2020-04-28 06:52:53 +00:00
|
|
|
import { FieldArray, WrappedFieldArrayProps } from "redux-form";
|
|
|
|
|
import styled from "styled-components";
|
|
|
|
|
import { Icon } from "@blueprintjs/core";
|
|
|
|
|
import { FormIcons } from "icons/FormIcons";
|
2020-08-31 05:15:04 +00:00
|
|
|
import BaseControl, { ControlProps } from "./BaseControl";
|
2020-04-28 06:52:53 +00:00
|
|
|
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-08-31 05:15:04 +00:00
|
|
|
const KeyValueRow = (props: KeyValueArrayProps & WrappedFieldArrayProps) => {
|
2020-04-28 06:52:53 +00:00
|
|
|
const { extraData = [] } = props;
|
|
|
|
|
const keyName = getFieldName(extraData[0].configProperty);
|
|
|
|
|
const valueName = getFieldName(extraData[1].configProperty);
|
|
|
|
|
const valueDataType = getType(extraData[1].dataType);
|
2020-08-31 05:15:04 +00:00
|
|
|
const keyFieldProps = extraData[0];
|
|
|
|
|
|
2020-04-28 06:52:53 +00:00
|
|
|
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") {
|
2020-10-12 13:06:05 +00:00
|
|
|
const fieldsValue: any[] = JSON.parse(`${props.fields.getAll()}`);
|
2020-04-28 06:52:53 +00:00
|
|
|
props.fields.removeAll();
|
|
|
|
|
fieldsValue.forEach((value, index) => {
|
|
|
|
|
props.fields.insert(index, value);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, [props.fields]);
|
|
|
|
|
|
2020-08-31 05:15:04 +00:00
|
|
|
const keyFieldValidate = useCallback(
|
|
|
|
|
(value: string) => {
|
|
|
|
|
if (value && keyFieldProps.validationRegex) {
|
|
|
|
|
const regex = new RegExp(keyFieldProps.validationRegex);
|
|
|
|
|
|
|
|
|
|
return regex.test(value) ? undefined : keyFieldProps.validationMessage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
|
},
|
|
|
|
|
[keyFieldProps.validationRegex, keyFieldProps.validationMessage],
|
|
|
|
|
);
|
|
|
|
|
|
2020-04-28 06:52:53 +00:00
|
|
|
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-08-31 05:15:04 +00:00
|
|
|
<TextField
|
|
|
|
|
name={`${field}.${keyName[1]}`}
|
|
|
|
|
showError
|
|
|
|
|
validate={keyFieldValidate}
|
2021-03-09 12:05:29 +00:00
|
|
|
placeholder={
|
|
|
|
|
(extraData && extraData[0].placeholderText) || ""
|
|
|
|
|
}
|
2020-08-31 05:15:04 +00:00
|
|
|
/>
|
2020-07-01 10:01:07 +00:00
|
|
|
</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}
|
2021-03-09 12:05:29 +00:00
|
|
|
placeholder={
|
|
|
|
|
(extraData && extraData[1].placeholderText) || ""
|
|
|
|
|
}
|
2020-07-01 10:01:07 +00:00
|
|
|
/>
|
|
|
|
|
</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>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class KeyValueFieldArray extends BaseControl<KeyValueArrayProps> {
|
|
|
|
|
render() {
|
|
|
|
|
const name = getFieldName(this.props.configProperty);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<FieldArray
|
|
|
|
|
component={KeyValueRow}
|
|
|
|
|
rerenderOnEveryChange={false}
|
|
|
|
|
{...this.props}
|
2020-10-12 13:06:05 +00:00
|
|
|
name={name[0]}
|
2020-04-28 06:52:53 +00:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2020-10-12 13:06:05 +00:00
|
|
|
rightIcon?: JSXElementConstructor<{ height: number; width: number }>;
|
2020-04-28 06:52:53 +00:00
|
|
|
description?: string;
|
|
|
|
|
actionConfig?: any;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default KeyValueFieldArray;
|