2021-12-27 12:04:45 +00:00
|
|
|
import React, { useEffect, useCallback } from "react";
|
|
|
|
|
import {
|
|
|
|
|
Field,
|
|
|
|
|
FieldArray,
|
|
|
|
|
WrappedFieldArrayProps,
|
|
|
|
|
WrappedFieldMetaProps,
|
|
|
|
|
WrappedFieldInputProps,
|
|
|
|
|
} from "redux-form";
|
2020-04-28 06:52:53 +00:00
|
|
|
import styled from "styled-components";
|
|
|
|
|
import { Icon } from "@blueprintjs/core";
|
|
|
|
|
import { FormIcons } from "icons/FormIcons";
|
2021-12-27 12:04:45 +00:00
|
|
|
import BaseControl, { ControlProps, ControlData } from "./BaseControl";
|
2020-04-28 06:52:53 +00:00
|
|
|
import { ControlType } from "constants/PropertyControlConstants";
|
|
|
|
|
import DynamicTextField from "components/editorComponents/form/fields/DynamicTextField";
|
2020-07-28 05:15:26 +00:00
|
|
|
import { Colors } from "constants/Colors";
|
2021-12-27 12:04:45 +00:00
|
|
|
import TextInput, { TextInputProps } from "components/ads/TextInput";
|
|
|
|
|
export interface KeyValueArrayControlProps extends ControlProps {
|
|
|
|
|
name: string;
|
|
|
|
|
label: string;
|
|
|
|
|
maxLen?: number;
|
|
|
|
|
description?: string;
|
|
|
|
|
actionConfig?: any;
|
|
|
|
|
extraData?: ControlData[];
|
|
|
|
|
isRequired?: boolean;
|
|
|
|
|
}
|
2020-04-28 06:52:53 +00:00
|
|
|
|
|
|
|
|
const FormRowWithLabel = styled.div`
|
|
|
|
|
display: flex;
|
|
|
|
|
flex: 1;
|
|
|
|
|
flex-direction: row;
|
|
|
|
|
& svg {
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
2021-12-27 12:04:45 +00:00
|
|
|
const StyledTextInput = styled(TextInput)`
|
|
|
|
|
min-width: 66px;
|
2020-07-01 07:21:02 +00:00
|
|
|
input[type="number"]::-webkit-inner-spin-button,
|
|
|
|
|
input[type="number"]::-webkit-outer-spin-button {
|
|
|
|
|
-webkit-appearance: none;
|
2021-12-27 12:04:45 +00:00
|
|
|
margin: 0px;
|
2020-07-01 07:21:02 +00:00
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
2021-12-27 12:04:45 +00:00
|
|
|
function KeyValueRow(
|
|
|
|
|
props: KeyValueArrayControlProps & WrappedFieldArrayProps,
|
|
|
|
|
) {
|
2020-04-28 06:52:53 +00:00
|
|
|
const { extraData = [] } = props;
|
2021-12-27 12:04:45 +00:00
|
|
|
const keyName = getFieldName(extraData[0]?.configProperty);
|
|
|
|
|
const valueName = getFieldName(extraData[1]?.configProperty);
|
2020-08-31 05:15:04 +00:00
|
|
|
const keyFieldProps = extraData[0];
|
|
|
|
|
|
2020-04-28 06:52:53 +00:00
|
|
|
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) {
|
2021-12-27 12:04:45 +00:00
|
|
|
if (keyName && valueName) {
|
|
|
|
|
props.fields.push({ [keyName[1]]: "", [valueName[1]]: "" });
|
|
|
|
|
} else {
|
|
|
|
|
props.fields.push({ key: "", value: "" });
|
|
|
|
|
}
|
2020-04-28 06:52:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
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) => {
|
2021-12-27 12:04:45 +00:00
|
|
|
if (value && keyFieldProps?.validationRegex) {
|
|
|
|
|
const regex = new RegExp(keyFieldProps?.validationRegex);
|
2020-08-31 05:15:04 +00:00
|
|
|
|
|
|
|
|
return regex.test(value) ? undefined : keyFieldProps.validationMessage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
|
},
|
2021-12-27 12:04:45 +00:00
|
|
|
[keyFieldProps?.validationRegex, keyFieldProps?.validationMessage],
|
2020-08-31 05:15:04 +00:00
|
|
|
);
|
2021-12-27 12:04:45 +00:00
|
|
|
const maxLen = props.maxLen;
|
|
|
|
|
//if maxLen exists apply a check on the length
|
|
|
|
|
const showAddIcon = (index: number): boolean =>
|
|
|
|
|
maxLen
|
|
|
|
|
? index === props.fields.length - 1 && props.fields.length < maxLen
|
|
|
|
|
: index === props.fields.length - 1;
|
2020-04-28 06:52:53 +00:00
|
|
|
|
2021-04-28 10:28:39 +00:00
|
|
|
return typeof props.fields.getAll() === "object" ? (
|
|
|
|
|
<>
|
|
|
|
|
{props.fields.map((field: any, index: number) => {
|
2021-12-27 12:04:45 +00:00
|
|
|
let keyTextFieldName = `${field}.key`;
|
|
|
|
|
let valueTextFieldName = `${field}.value`;
|
|
|
|
|
|
|
|
|
|
if (keyName && Array.isArray(keyName) && keyName?.length)
|
|
|
|
|
keyTextFieldName = `${field}.${keyName[1]}`;
|
|
|
|
|
|
|
|
|
|
if (valueName && Array.isArray(valueName) && valueName?.length)
|
|
|
|
|
valueTextFieldName = `${field}.${valueName[1]}`;
|
|
|
|
|
|
2021-04-28 10:28:39 +00:00
|
|
|
return (
|
2021-12-27 12:04:45 +00:00
|
|
|
<FormRowWithLabel
|
|
|
|
|
key={index}
|
|
|
|
|
style={{ marginTop: index > 0 ? "16px" : "0px" }}
|
|
|
|
|
>
|
2021-12-07 09:45:18 +00:00
|
|
|
<div
|
2021-12-27 12:04:45 +00:00
|
|
|
data-replay-id={btoa(keyTextFieldName)}
|
2021-12-07 09:45:18 +00:00
|
|
|
style={{ width: "50vh" }}
|
|
|
|
|
>
|
2021-12-27 12:04:45 +00:00
|
|
|
<Field
|
|
|
|
|
component={renderTextInput}
|
|
|
|
|
name={keyTextFieldName}
|
|
|
|
|
props={{
|
|
|
|
|
dataType: getType(extraData[0]?.dataType),
|
|
|
|
|
defaultValue: props.initialValue,
|
|
|
|
|
keyFieldValidate,
|
|
|
|
|
placeholder: props.extraData
|
|
|
|
|
? props.extraData[1]?.placeholderText
|
|
|
|
|
: "",
|
|
|
|
|
isRequired: extraData[0]?.isRequired,
|
|
|
|
|
name: keyTextFieldName,
|
|
|
|
|
}}
|
2021-04-28 10:28:39 +00:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
{!props.actionConfig && (
|
2021-12-27 12:04:45 +00:00
|
|
|
<div style={{ marginLeft: "16px", width: "50vh" }}>
|
|
|
|
|
<div
|
|
|
|
|
data-replay-id={valueTextFieldName}
|
|
|
|
|
style={{ display: "flex", flexDirection: "row" }}
|
|
|
|
|
>
|
|
|
|
|
<Field
|
|
|
|
|
component={renderTextInput}
|
|
|
|
|
name={valueTextFieldName}
|
|
|
|
|
props={{
|
|
|
|
|
dataType: getType(extraData[1]?.dataType),
|
|
|
|
|
defaultValue: props.initialValue,
|
|
|
|
|
placeholder: props.extraData
|
|
|
|
|
? props.extraData[1]?.placeholderText
|
|
|
|
|
: "",
|
|
|
|
|
name: valueTextFieldName,
|
|
|
|
|
isRequired: extraData[1]?.isRequired,
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
{showAddIcon(index) ? (
|
2021-04-28 10:28:39 +00:00
|
|
|
<Icon
|
2021-12-27 12:04:45 +00:00
|
|
|
className="t--add-field"
|
2021-04-28 10:28:39 +00:00
|
|
|
color={Colors["CADET_BLUE"]}
|
|
|
|
|
icon="plus"
|
|
|
|
|
iconSize={20}
|
2021-12-27 12:04:45 +00:00
|
|
|
onClick={() => {
|
|
|
|
|
props.fields.push({ key: "", value: "" });
|
|
|
|
|
}}
|
|
|
|
|
style={{ marginLeft: "16px", alignSelf: "center" }}
|
2021-04-28 10:28:39 +00:00
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<FormIcons.DELETE_ICON
|
2021-12-27 12:04:45 +00:00
|
|
|
className="t--delete-field"
|
2021-04-28 10:28:39 +00:00
|
|
|
color={Colors["CADET_BLUE"]}
|
|
|
|
|
height={20}
|
|
|
|
|
onClick={() => props.fields.remove(index)}
|
2021-12-27 12:04:45 +00:00
|
|
|
style={{ marginLeft: "16px", alignSelf: "center" }}
|
2021-04-28 10:28:39 +00:00
|
|
|
width={20}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{props.actionConfig && (
|
|
|
|
|
<DynamicTextField
|
|
|
|
|
name={`${field}.value`}
|
|
|
|
|
placeholder={
|
|
|
|
|
props.actionConfig[index].mandatory &&
|
|
|
|
|
props.actionConfig[index].type
|
|
|
|
|
? `Value (Type: ${props.actionConfig[index].type})`
|
|
|
|
|
: `Value (optional)`
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</FormRowWithLabel>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</>
|
|
|
|
|
) : null;
|
|
|
|
|
}
|
2020-04-28 06:52:53 +00:00
|
|
|
|
2021-12-27 12:04:45 +00:00
|
|
|
class KeyValueArrayControl extends BaseControl<KeyValueArrayControlProps> {
|
2020-04-28 06:52:53 +00:00
|
|
|
render() {
|
|
|
|
|
const name = getFieldName(this.props.configProperty);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<FieldArray
|
|
|
|
|
component={KeyValueRow}
|
|
|
|
|
rerenderOnEveryChange={false}
|
|
|
|
|
{...this.props}
|
2021-12-27 12:04:45 +00:00
|
|
|
name={name ? name[0] : ""}
|
2020-04-28 06:52:53 +00:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getControlType(): ControlType {
|
|
|
|
|
return "KEYVALUE_ARRAY";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-27 12:04:45 +00:00
|
|
|
const getFieldName = (configProperty: string): string[] | undefined => {
|
|
|
|
|
if (configProperty) return configProperty.split("[*].");
|
2020-04-28 06:52:53 +00:00
|
|
|
};
|
|
|
|
|
|
2021-09-09 15:10:22 +00:00
|
|
|
const getType = (dataType: string | undefined) => {
|
2020-04-28 06:52:53 +00:00
|
|
|
switch (dataType) {
|
|
|
|
|
case "PASSWORD":
|
|
|
|
|
return "password";
|
|
|
|
|
case "NUMBER":
|
|
|
|
|
return "number";
|
|
|
|
|
default:
|
|
|
|
|
return "text";
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-12-27 12:04:45 +00:00
|
|
|
function renderTextInput(
|
|
|
|
|
props: TextInputProps & {
|
|
|
|
|
dataType?: "text" | "number" | "password";
|
|
|
|
|
placeholder?: string;
|
|
|
|
|
defaultValue: string | number;
|
|
|
|
|
isRequired: boolean;
|
|
|
|
|
keyFieldValidate?: (value: string) => { isValid: boolean; message: string };
|
|
|
|
|
errorMsg?: string;
|
|
|
|
|
helperText?: string;
|
|
|
|
|
} & {
|
|
|
|
|
meta: Partial<WrappedFieldMetaProps>;
|
|
|
|
|
input: Partial<WrappedFieldInputProps>;
|
|
|
|
|
},
|
|
|
|
|
): JSX.Element {
|
|
|
|
|
return (
|
|
|
|
|
<StyledTextInput
|
|
|
|
|
dataType={props.dataType}
|
|
|
|
|
defaultValue={props.defaultValue}
|
|
|
|
|
errorMsg={props.errorMsg}
|
|
|
|
|
helperText={props.helperText}
|
|
|
|
|
name={props.input?.name}
|
|
|
|
|
onChange={props.input.onChange}
|
|
|
|
|
placeholder={props.placeholder}
|
|
|
|
|
validator={props.keyFieldValidate}
|
|
|
|
|
value={props.input.value}
|
|
|
|
|
width="100%"
|
|
|
|
|
/>
|
|
|
|
|
);
|
2020-04-28 06:52:53 +00:00
|
|
|
}
|
|
|
|
|
|
2021-12-27 12:04:45 +00:00
|
|
|
export default KeyValueArrayControl;
|