2020-04-01 08:09:57 +00:00
|
|
|
import React, { useState, useEffect } from "react";
|
|
|
|
|
|
|
|
|
|
import styled from "constants/DefaultTheme";
|
|
|
|
|
import { FormIcons } from "icons/FormIcons";
|
|
|
|
|
import { AnyStyledComponent } from "styled-components";
|
|
|
|
|
import {
|
|
|
|
|
ControlWrapper,
|
|
|
|
|
StyledInputGroup,
|
|
|
|
|
StyledPropertyPaneButton,
|
|
|
|
|
} from "./StyledControls";
|
|
|
|
|
|
|
|
|
|
import { DropDownOptionWithKey } from "./OptionControl";
|
|
|
|
|
import { DropdownOption } from "widgets/DropdownWidget";
|
|
|
|
|
import { generateReactKey } from "utils/generators";
|
2021-03-15 12:17:56 +00:00
|
|
|
import { Category, Size } from "components/ads/Button";
|
2020-04-01 08:09:57 +00:00
|
|
|
|
|
|
|
|
function updateOptionLabel<T>(
|
|
|
|
|
options: Array<T>,
|
|
|
|
|
index: number,
|
|
|
|
|
updatedLabel: string,
|
|
|
|
|
) {
|
|
|
|
|
return options.map((option: T, optionIndex) => {
|
|
|
|
|
if (index !== optionIndex) {
|
|
|
|
|
return option;
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
...option,
|
|
|
|
|
label: updatedLabel,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function updateOptionValue<T>(
|
|
|
|
|
options: Array<T>,
|
|
|
|
|
index: number,
|
|
|
|
|
updatedValue: string,
|
|
|
|
|
) {
|
|
|
|
|
return options.map((option, optionIndex) => {
|
|
|
|
|
if (index !== optionIndex) {
|
|
|
|
|
return option;
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
...option,
|
|
|
|
|
value: updatedValue,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const StyledDeleteIcon = styled(FormIcons.DELETE_ICON as AnyStyledComponent)`
|
|
|
|
|
padding: 0px 5px;
|
|
|
|
|
position: absolute;
|
2021-03-15 12:17:56 +00:00
|
|
|
right: 4px;
|
2020-04-01 08:09:57 +00:00
|
|
|
cursor: pointer;
|
2021-03-15 12:17:56 +00:00
|
|
|
&& svg path {
|
|
|
|
|
fill: ${(props) => props.theme.colors.propertyPane.deleteIconColor};
|
|
|
|
|
}
|
2020-04-01 08:09:57 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const StyledOptionControlInputGroup = styled(StyledInputGroup)`
|
2021-03-15 12:17:56 +00:00
|
|
|
margin-right: 5px;
|
2020-04-01 08:09:57 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const StyledOptionControlWrapper = styled(ControlWrapper)`
|
|
|
|
|
display: flex;
|
|
|
|
|
justify-content: flex-start;
|
|
|
|
|
padding-right: 16px;
|
2020-06-03 04:44:11 +00:00
|
|
|
width: calc(100% - 10px);
|
2020-04-01 08:09:57 +00:00
|
|
|
`;
|
|
|
|
|
|
2021-03-15 12:17:56 +00:00
|
|
|
const StyledBox = styled.div`
|
|
|
|
|
width: 10px;
|
|
|
|
|
`;
|
|
|
|
|
|
2020-10-20 14:59:28 +00:00
|
|
|
type UpdatePairFunction = (pair: DropdownOption[]) => any;
|
|
|
|
|
|
2020-04-01 08:09:57 +00:00
|
|
|
type KeyValueComponentProps = {
|
|
|
|
|
pairs: DropdownOption[];
|
2020-10-20 14:59:28 +00:00
|
|
|
updatePairs: UpdatePairFunction;
|
2020-04-01 08:09:57 +00:00
|
|
|
addLabel?: string;
|
|
|
|
|
};
|
|
|
|
|
export function KeyValueComponent(props: KeyValueComponentProps) {
|
|
|
|
|
const [renderPairs, setRenderPairs] = useState<DropDownOptionWithKey[]>([]);
|
|
|
|
|
const { pairs } = props;
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
let { pairs } = props;
|
|
|
|
|
pairs = Array.isArray(pairs) ? pairs.slice() : [];
|
|
|
|
|
|
2020-12-24 04:32:25 +00:00
|
|
|
const newRenderPairs: DropDownOptionWithKey[] = pairs.map((pair) => {
|
2020-04-01 08:09:57 +00:00
|
|
|
return {
|
|
|
|
|
...pair,
|
|
|
|
|
key: generateReactKey(),
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
pairs.length !== 0 &&
|
|
|
|
|
renderPairs.length === 0 &&
|
|
|
|
|
setRenderPairs(newRenderPairs);
|
|
|
|
|
}, [props, pairs.length, renderPairs.length]);
|
|
|
|
|
|
|
|
|
|
function deletePair(index: number) {
|
|
|
|
|
let { pairs } = props;
|
|
|
|
|
pairs = Array.isArray(pairs) ? pairs : [];
|
|
|
|
|
|
|
|
|
|
const newPairs = pairs.filter((o, i) => i !== index);
|
|
|
|
|
const newRenderPairs = renderPairs.filter((o, i) => i !== index);
|
|
|
|
|
|
|
|
|
|
setRenderPairs(newRenderPairs);
|
|
|
|
|
props.updatePairs(newPairs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function updateKey(index: number, updatedKey: string) {
|
|
|
|
|
let { pairs } = props;
|
|
|
|
|
pairs = Array.isArray(pairs) ? pairs : [];
|
|
|
|
|
const updatedPairs = updateOptionLabel(pairs, index, updatedKey);
|
|
|
|
|
const updatedRenderPairs = updateOptionLabel(
|
|
|
|
|
renderPairs,
|
|
|
|
|
index,
|
|
|
|
|
updatedKey,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
setRenderPairs(updatedRenderPairs);
|
|
|
|
|
props.updatePairs(updatedPairs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function updateValue(index: number, updatedValue: string) {
|
|
|
|
|
let { pairs } = props;
|
|
|
|
|
pairs = Array.isArray(pairs) ? pairs : [];
|
|
|
|
|
const updatedPairs = updateOptionValue(pairs, index, updatedValue);
|
|
|
|
|
const updatedRenderPairs = updateOptionValue(
|
|
|
|
|
renderPairs,
|
|
|
|
|
index,
|
|
|
|
|
updatedValue,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
setRenderPairs(updatedRenderPairs);
|
|
|
|
|
props.updatePairs(updatedPairs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function addPair() {
|
|
|
|
|
let { pairs } = props;
|
|
|
|
|
pairs = Array.isArray(pairs) ? pairs.slice() : [];
|
|
|
|
|
pairs.push({ label: "", value: "" });
|
|
|
|
|
const updatedRenderPairs = renderPairs.slice();
|
|
|
|
|
updatedRenderPairs.push({ label: "", value: "", key: generateReactKey() });
|
|
|
|
|
|
|
|
|
|
setRenderPairs(updatedRenderPairs);
|
|
|
|
|
props.updatePairs(pairs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2021-04-28 10:28:39 +00:00
|
|
|
<>
|
2020-04-01 08:09:57 +00:00
|
|
|
{renderPairs.map((pair: DropDownOptionWithKey, index) => {
|
|
|
|
|
return (
|
2021-04-28 10:28:39 +00:00
|
|
|
<StyledOptionControlWrapper key={pair.key} orientation={"HORIZONTAL"}>
|
2020-04-01 08:09:57 +00:00
|
|
|
<StyledOptionControlInputGroup
|
2021-03-15 12:17:56 +00:00
|
|
|
dataType={"text"}
|
2021-04-28 10:28:39 +00:00
|
|
|
defaultValue={pair.label}
|
2021-03-15 12:17:56 +00:00
|
|
|
onChange={(value: string) => {
|
|
|
|
|
updateKey(index, value);
|
2020-04-01 08:09:57 +00:00
|
|
|
}}
|
2021-04-28 10:28:39 +00:00
|
|
|
placeholder={"Name"}
|
2020-04-01 08:09:57 +00:00
|
|
|
/>
|
2021-03-15 12:17:56 +00:00
|
|
|
<StyledBox />
|
|
|
|
|
<StyledInputGroup
|
|
|
|
|
dataType={"text"}
|
2021-04-28 10:28:39 +00:00
|
|
|
defaultValue={pair.value}
|
2021-03-15 12:17:56 +00:00
|
|
|
onChange={(value: string) => {
|
|
|
|
|
updateValue(index, value);
|
2020-04-01 08:09:57 +00:00
|
|
|
}}
|
2021-04-28 10:28:39 +00:00
|
|
|
placeholder={"Value"}
|
2020-04-01 08:09:57 +00:00
|
|
|
/>
|
|
|
|
|
<StyledDeleteIcon
|
|
|
|
|
height={20}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
deletePair(index);
|
|
|
|
|
}}
|
2021-04-28 10:28:39 +00:00
|
|
|
width={20}
|
2020-04-01 08:09:57 +00:00
|
|
|
/>
|
|
|
|
|
</StyledOptionControlWrapper>
|
|
|
|
|
);
|
|
|
|
|
})}
|
2021-03-15 12:17:56 +00:00
|
|
|
|
2020-04-01 08:09:57 +00:00
|
|
|
<StyledPropertyPaneButton
|
2021-04-28 10:28:39 +00:00
|
|
|
category={Category.tertiary}
|
2021-03-15 12:17:56 +00:00
|
|
|
icon="plus"
|
2020-04-01 08:09:57 +00:00
|
|
|
onClick={addPair}
|
2021-03-15 12:17:56 +00:00
|
|
|
size={Size.medium}
|
2021-04-28 10:28:39 +00:00
|
|
|
tag="button"
|
|
|
|
|
text={props.addLabel || "Option"}
|
|
|
|
|
type="button"
|
2020-04-01 08:09:57 +00:00
|
|
|
/>
|
2021-04-28 10:28:39 +00:00
|
|
|
</>
|
2020-04-01 08:09:57 +00:00
|
|
|
);
|
|
|
|
|
}
|