2021-06-07 05:13:16 +00:00
|
|
|
import React, { useEffect } from "react";
|
|
|
|
|
import FormControl from "pages/Editor/FormControl";
|
2022-07-11 04:55:28 +00:00
|
|
|
import { Text, TextType } from "design-system";
|
2021-06-07 05:13:16 +00:00
|
|
|
import Icon, { IconSize } from "components/ads/Icon";
|
|
|
|
|
import { Classes } from "components/ads/common";
|
|
|
|
|
import styled from "styled-components";
|
|
|
|
|
import { FieldArray } from "redux-form";
|
|
|
|
|
import { ControlProps } from "./BaseControl";
|
|
|
|
|
|
|
|
|
|
const CenteredIcon = styled(Icon)`
|
2021-10-20 09:33:04 +00:00
|
|
|
margin-top: 26px;
|
2021-06-07 05:13:16 +00:00
|
|
|
&.hide {
|
|
|
|
|
opacity: 0;
|
|
|
|
|
pointer-events: none;
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const PrimaryBox = styled.div`
|
|
|
|
|
display: flex;
|
2021-09-06 05:53:46 +00:00
|
|
|
width: min-content;
|
2021-06-07 05:13:16 +00:00
|
|
|
flex-direction: column;
|
|
|
|
|
border: 2px solid ${(props) => props.theme.colors.apiPane.dividerBg};
|
|
|
|
|
padding: 10px;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const SecondaryBox = styled.div`
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-direction: row;
|
2021-09-06 05:53:46 +00:00
|
|
|
width: min-content;
|
2021-06-07 05:13:16 +00:00
|
|
|
align-items: center;
|
|
|
|
|
justify-content: space-between;
|
|
|
|
|
padding: 5px;
|
2021-10-20 09:33:04 +00:00
|
|
|
|
|
|
|
|
& > div {
|
|
|
|
|
margin-right: 8px;
|
|
|
|
|
height: 60px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
& > .t--form-control-QUERY_DYNAMIC_INPUT_TEXT > div {
|
|
|
|
|
width: 20vw !important;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-16 09:55:58 +00:00
|
|
|
& > .t--form-control-DROP_DOWN,
|
2021-10-20 09:33:04 +00:00
|
|
|
& > .t--form-control-DROP_DOWN > div > div,
|
|
|
|
|
& > .t--form-control-DROP_DOWN > div > div > div > div {
|
2021-11-16 09:55:58 +00:00
|
|
|
width: 12vw;
|
2021-10-20 09:33:04 +00:00
|
|
|
}
|
2021-06-07 05:13:16 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const AddMoreAction = styled.div`
|
|
|
|
|
width: fit-content;
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
display: flex;
|
|
|
|
|
margin-top: 16px;
|
|
|
|
|
.${Classes.TEXT} {
|
|
|
|
|
margin-left: 8px;
|
|
|
|
|
color: #03b365;
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
function NestedComponents(props: any) {
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (props.fields.length < 1) {
|
2022-04-07 16:18:29 +00:00
|
|
|
props.fields.push({ path: "", value: "" });
|
2021-06-07 05:13:16 +00:00
|
|
|
}
|
|
|
|
|
}, [props.fields.length]);
|
|
|
|
|
return (
|
|
|
|
|
<PrimaryBox>
|
|
|
|
|
{props.fields &&
|
|
|
|
|
props.fields.length > 0 &&
|
|
|
|
|
props.fields.map((field: string, index: number) => {
|
|
|
|
|
return (
|
|
|
|
|
<SecondaryBox key={index}>
|
|
|
|
|
{props.schema.map((sch: any, idx: number) => {
|
|
|
|
|
sch = {
|
|
|
|
|
...sch,
|
|
|
|
|
configProperty: `${field}.${sch.key}`,
|
2022-05-18 14:37:47 +00:00
|
|
|
customStyles: {
|
|
|
|
|
width: "20vw",
|
|
|
|
|
...props.customStyles,
|
|
|
|
|
},
|
2021-06-07 05:13:16 +00:00
|
|
|
};
|
|
|
|
|
return (
|
|
|
|
|
<FormControl
|
|
|
|
|
config={sch}
|
|
|
|
|
formName={props.formName}
|
|
|
|
|
key={idx}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
<CenteredIcon
|
|
|
|
|
name="delete"
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
props.fields.remove(index);
|
|
|
|
|
}}
|
|
|
|
|
size={IconSize.XXL}
|
|
|
|
|
/>
|
|
|
|
|
</SecondaryBox>
|
|
|
|
|
);
|
|
|
|
|
})}
|
2022-04-07 16:18:29 +00:00
|
|
|
<AddMoreAction onClick={() => props.fields.push({ path: "", value: "" })}>
|
2021-06-07 05:13:16 +00:00
|
|
|
{/*Hardcoded label to be removed */}
|
|
|
|
|
<Text type={TextType.H5}>+ Add Condition (And)</Text>
|
|
|
|
|
</AddMoreAction>
|
|
|
|
|
</PrimaryBox>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function FieldArrayControl(props: FieldArrayControlProps) {
|
2021-12-27 12:04:45 +00:00
|
|
|
const { configProperty, formName, schema } = props;
|
2021-06-07 05:13:16 +00:00
|
|
|
return (
|
2021-12-27 12:04:45 +00:00
|
|
|
<FieldArray
|
|
|
|
|
component={NestedComponents}
|
|
|
|
|
name={configProperty}
|
|
|
|
|
props={{ formName, schema }}
|
|
|
|
|
rerenderOnEveryChange={false}
|
|
|
|
|
/>
|
2021-06-07 05:13:16 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type FieldArrayControlProps = ControlProps;
|