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";
|
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";
|
2022-12-13 14:39:16 +00:00
|
|
|
import {
|
|
|
|
|
Case,
|
|
|
|
|
Classes,
|
|
|
|
|
Icon,
|
|
|
|
|
IconSize,
|
|
|
|
|
Text,
|
|
|
|
|
TextInput,
|
|
|
|
|
TextInputProps,
|
|
|
|
|
TextType,
|
|
|
|
|
} from "design-system";
|
2021-12-27 12:04:45 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
2022-12-13 14:39:16 +00:00
|
|
|
const CenteredIcon = styled(Icon)`
|
|
|
|
|
align-self: center;
|
|
|
|
|
margin-left: 15px;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const AddMoreAction = styled.div`
|
|
|
|
|
width: fit-content;
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
display: flex;
|
|
|
|
|
margin-top: 16px;
|
|
|
|
|
margin-left: 12px;
|
|
|
|
|
.${Classes.TEXT} {
|
|
|
|
|
margin-left: 8px;
|
|
|
|
|
color: ${Colors.GRAY};
|
|
|
|
|
}
|
|
|
|
|
svg {
|
|
|
|
|
fill: ${Colors.GRAY};
|
|
|
|
|
path {
|
|
|
|
|
fill: unset;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
|
.${Classes.TEXT} {
|
|
|
|
|
color: ${Colors.CHARCOAL};
|
|
|
|
|
}
|
|
|
|
|
svg {
|
|
|
|
|
fill: ${Colors.CHARCOAL};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
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];
|
|
|
|
|
|
2022-12-13 14:39:16 +00:00
|
|
|
const addRow = useCallback(() => {
|
|
|
|
|
if (keyName && valueName) {
|
|
|
|
|
props.fields.push({ [keyName[1]]: "", [valueName[1]]: "" });
|
|
|
|
|
} else {
|
|
|
|
|
props.fields.push({ key: "", value: "" });
|
|
|
|
|
}
|
|
|
|
|
}, [keyName, valueName]);
|
|
|
|
|
|
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) {
|
2022-12-13 14:39:16 +00:00
|
|
|
addRow();
|
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
|
|
|
|
2022-05-18 17:35:03 +00:00
|
|
|
return regex.test(value)
|
|
|
|
|
? { isValid: true }
|
|
|
|
|
: { isValid: false, message: keyFieldProps.validationMessage };
|
2020-08-31 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
|
},
|
2021-12-27 12:04:45 +00:00
|
|
|
[keyFieldProps?.validationRegex, keyFieldProps?.validationMessage],
|
2020-08-31 05:15:04 +00:00
|
|
|
);
|
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)}
|
2022-01-14 13:50:54 +00:00
|
|
|
style={{ width: "20vw" }}
|
2021-12-07 09:45:18 +00:00
|
|
|
>
|
2021-12-27 12:04:45 +00:00
|
|
|
<Field
|
|
|
|
|
component={renderTextInput}
|
|
|
|
|
name={keyTextFieldName}
|
|
|
|
|
props={{
|
|
|
|
|
dataType: getType(extraData[0]?.dataType),
|
2022-03-16 14:26:57 +00:00
|
|
|
defaultValue: extraData[0]?.initialValue,
|
2021-12-27 12:04:45 +00:00
|
|
|
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 && (
|
2022-01-14 13:50:54 +00:00
|
|
|
<div style={{ marginLeft: "16px", width: "20vw" }}>
|
2021-12-27 12:04:45 +00:00
|
|
|
<div
|
2022-03-25 10:43:26 +00:00
|
|
|
data-replay-id={btoa(valueTextFieldName)}
|
2021-12-27 12:04:45 +00:00
|
|
|
style={{ display: "flex", flexDirection: "row" }}
|
|
|
|
|
>
|
|
|
|
|
<Field
|
|
|
|
|
component={renderTextInput}
|
|
|
|
|
name={valueTextFieldName}
|
|
|
|
|
props={{
|
|
|
|
|
dataType: getType(extraData[1]?.dataType),
|
2022-03-16 14:26:57 +00:00
|
|
|
defaultValue: extraData[1]?.initialValue,
|
2021-12-27 12:04:45 +00:00
|
|
|
placeholder: props.extraData
|
|
|
|
|
? props.extraData[1]?.placeholderText
|
|
|
|
|
: "",
|
|
|
|
|
name: valueTextFieldName,
|
|
|
|
|
isRequired: extraData[1]?.isRequired,
|
|
|
|
|
}}
|
|
|
|
|
/>
|
2022-12-13 14:39:16 +00:00
|
|
|
<CenteredIcon
|
|
|
|
|
className="t--delete-field"
|
|
|
|
|
name="delete"
|
|
|
|
|
onClick={() => props.fields.remove(index)}
|
|
|
|
|
size={IconSize.LARGE}
|
|
|
|
|
/>
|
2021-04-28 10:28:39 +00:00
|
|
|
</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>
|
|
|
|
|
);
|
|
|
|
|
})}
|
2022-12-13 14:39:16 +00:00
|
|
|
<AddMoreAction className="t--add-field" onClick={addRow}>
|
|
|
|
|
<Icon className="t--addApiHeader" name="add-more" size={IconSize.XXL} />
|
|
|
|
|
<Text case={Case.UPPERCASE} type={TextType.H5}>
|
|
|
|
|
Add more
|
|
|
|
|
</Text>
|
|
|
|
|
</AddMoreAction>
|
2021-04-28 10:28:39 +00:00
|
|
|
</>
|
|
|
|
|
) : 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;
|