2020-10-12 13:06:05 +00:00
|
|
|
import React, { JSXElementConstructor, useEffect } 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";
|
|
|
|
|
import BaseControl, { ControlProps, ControlData } from "./BaseControl";
|
|
|
|
|
import TextField from "components/editorComponents/form/fields/TextField";
|
|
|
|
|
import { ControlType } from "constants/PropertyControlConstants";
|
2020-12-02 10:36:35 +00:00
|
|
|
import { Colors } from "constants/Colors";
|
2020-04-28 06:52:53 +00:00
|
|
|
|
2021-12-27 12:04:45 +00:00
|
|
|
//TODO: combine it with KeyValueArrayControl and deprecate KeyValueInputControl
|
|
|
|
|
|
|
|
|
|
type KeyValueRowProps = {
|
|
|
|
|
name: string;
|
|
|
|
|
label: string;
|
|
|
|
|
rightIcon?: JSXElementConstructor<{ height: number; width: number }>;
|
|
|
|
|
description?: string;
|
|
|
|
|
actionConfig?: any;
|
|
|
|
|
extraData?: ControlData[];
|
|
|
|
|
isRequired?: boolean;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export interface KeyValueInputControlProps extends ControlProps {
|
|
|
|
|
name: string;
|
|
|
|
|
label: string;
|
|
|
|
|
rightIcon?: JSXElementConstructor<{ height: number; width: number }>;
|
|
|
|
|
description?: string;
|
|
|
|
|
actionConfig?: any;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
function KeyValueRow(props: KeyValueRowProps & WrappedFieldArrayProps) {
|
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) {
|
2020-04-28 06:52:53 +00:00
|
|
|
props.fields.push({ key: "", value: "" });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, [props.fields]);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
{typeof props.fields.getAll() === "object" && (
|
|
|
|
|
<div>
|
|
|
|
|
{props.fields.map((field: any, index: number) => (
|
|
|
|
|
<FormRowWithLabel key={index} style={{ marginTop: index ? 13 : 0 }}>
|
2021-12-07 09:45:18 +00:00
|
|
|
<div
|
|
|
|
|
data-replay-id={btoa(`${field}.key`)}
|
2022-01-14 13:50:54 +00:00
|
|
|
style={{ width: "20vw" }}
|
2021-12-07 09:45:18 +00:00
|
|
|
>
|
2020-04-28 06:52:53 +00:00
|
|
|
<TextField name={`${field}.key`} placeholder="Key" />
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div style={{ marginLeft: 16 }}>
|
|
|
|
|
<div style={{ display: "flex", flexDirection: "row" }}>
|
2021-12-07 09:45:18 +00:00
|
|
|
<div
|
|
|
|
|
data-replay-id={btoa(`${field}.value`)}
|
2022-01-14 13:50:54 +00:00
|
|
|
style={{ marginRight: 14, width: "20vw" }}
|
2021-12-07 09:45:18 +00:00
|
|
|
>
|
2020-04-28 06:52:53 +00:00
|
|
|
<TextField name={`${field}.value`} placeholder="Value" />
|
|
|
|
|
</div>
|
|
|
|
|
{index === props.fields.length - 1 ? (
|
|
|
|
|
<Icon
|
2020-12-02 10:36:35 +00:00
|
|
|
className="t--add-field"
|
2021-04-28 10:28:39 +00:00
|
|
|
color={"#A3B3BF"}
|
2020-04-28 06:52:53 +00:00
|
|
|
icon="plus"
|
|
|
|
|
iconSize={20}
|
|
|
|
|
onClick={() => props.fields.push({ key: "", value: "" })}
|
|
|
|
|
style={{ alignSelf: "center" }}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<FormIcons.DELETE_ICON
|
2020-12-02 10:36:35 +00:00
|
|
|
className="t--delete-field"
|
|
|
|
|
color={Colors.CADET_BLUE}
|
2021-04-28 10:28:39 +00:00
|
|
|
height={20}
|
2020-04-28 06:52:53 +00:00
|
|
|
onClick={() => props.fields.remove(index)}
|
|
|
|
|
style={{ alignSelf: "center" }}
|
2021-04-28 10:28:39 +00:00
|
|
|
width={20}
|
2020-04-28 06:52:53 +00:00
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</FormRowWithLabel>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2021-04-28 10:28:39 +00:00
|
|
|
}
|
2020-04-28 06:52:53 +00:00
|
|
|
|
2021-12-27 12:04:45 +00:00
|
|
|
class KeyValueFieldInputControl extends BaseControl<KeyValueInputControlProps> {
|
2020-04-28 06:52:53 +00:00
|
|
|
render() {
|
|
|
|
|
return (
|
|
|
|
|
<FieldArray
|
|
|
|
|
component={KeyValueRow}
|
|
|
|
|
rerenderOnEveryChange={false}
|
|
|
|
|
{...this.props}
|
2020-10-12 13:06:05 +00:00
|
|
|
name={this.props.configProperty}
|
2020-04-28 06:52:53 +00:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getControlType(): ControlType {
|
|
|
|
|
return "KEY_VAL_INPUT";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-27 12:04:45 +00:00
|
|
|
export default KeyValueFieldInputControl;
|