2021-09-21 07:55:56 +00:00
|
|
|
import React, { useState, useEffect, useCallback } from "react";
|
2020-04-01 08:09:57 +00:00
|
|
|
|
|
|
|
|
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";
|
2021-09-09 15:10:22 +00:00
|
|
|
import { DropdownOption } from "components/constants";
|
2020-04-01 08:09:57 +00:00
|
|
|
import { generateReactKey } from "utils/generators";
|
2021-03-15 12:17:56 +00:00
|
|
|
import { Category, Size } from "components/ads/Button";
|
2021-09-21 07:55:56 +00:00
|
|
|
import { debounce } from "lodash";
|
2022-02-02 12:10:40 +00:00
|
|
|
import { getNextEntityName } from "utils/AppsmithUtils";
|
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)`
|
|
|
|
|
cursor: pointer;
|
2022-04-14 07:53:41 +00:00
|
|
|
|
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
|
|
|
|
2021-10-28 05:14:59 +00:00
|
|
|
&&:hover {
|
|
|
|
|
svg path {
|
|
|
|
|
fill: ${(props) => props.theme.colors.propertyPane.title};
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-01 08:09:57 +00:00
|
|
|
`;
|
|
|
|
|
|
2021-03-15 12:17:56 +00:00
|
|
|
const StyledBox = styled.div`
|
|
|
|
|
width: 10px;
|
|
|
|
|
`;
|
|
|
|
|
|
2022-04-14 07:53:41 +00:00
|
|
|
const StyledButton = styled.button`
|
|
|
|
|
width: 28px;
|
|
|
|
|
height: 28px;
|
|
|
|
|
|
|
|
|
|
&&& svg {
|
|
|
|
|
width: 14px;
|
|
|
|
|
height: 14px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
&&:focus {
|
|
|
|
|
svg path {
|
|
|
|
|
fill: ${(props) => props.theme.colors.propertyPane.title};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
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[]>([]);
|
2021-09-21 07:55:56 +00:00
|
|
|
const [typing, setTyping] = useState<boolean>(false);
|
2020-04-01 08:09:57 +00:00
|
|
|
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(),
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2021-09-21 07:55:56 +00:00
|
|
|
pairs.length !== 0 && !typing && setRenderPairs(newRenderPairs);
|
2020-04-01 08:09:57 +00:00
|
|
|
}, [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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-21 07:55:56 +00:00
|
|
|
const debouncedUpdatePairs = useCallback(
|
|
|
|
|
debounce((updatedPairs: DropdownOption[]) => {
|
|
|
|
|
props.updatePairs(updatedPairs);
|
|
|
|
|
}, 200),
|
|
|
|
|
[props.updatePairs],
|
|
|
|
|
);
|
|
|
|
|
|
2020-04-01 08:09:57 +00:00
|
|
|
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);
|
2021-09-21 07:55:56 +00:00
|
|
|
debouncedUpdatePairs(updatedPairs);
|
2020-04-01 08:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
2021-09-21 07:55:56 +00:00
|
|
|
debouncedUpdatePairs(updatedPairs);
|
2020-04-01 08:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function addPair() {
|
|
|
|
|
let { pairs } = props;
|
|
|
|
|
pairs = Array.isArray(pairs) ? pairs.slice() : [];
|
2022-02-02 12:10:40 +00:00
|
|
|
pairs.push({
|
|
|
|
|
label: getNextEntityName(
|
|
|
|
|
"Option",
|
|
|
|
|
pairs.map((pair: any) => pair.label),
|
|
|
|
|
),
|
|
|
|
|
value: getNextEntityName(
|
|
|
|
|
"OPTION",
|
|
|
|
|
pairs.map((pair: any) => pair.value),
|
|
|
|
|
),
|
|
|
|
|
});
|
2020-04-01 08:09:57 +00:00
|
|
|
const updatedRenderPairs = renderPairs.slice();
|
2022-02-02 12:10:40 +00:00
|
|
|
updatedRenderPairs.push({
|
|
|
|
|
label: getNextEntityName(
|
|
|
|
|
"Option",
|
|
|
|
|
renderPairs.map((pair: any) => pair.label),
|
|
|
|
|
),
|
|
|
|
|
value: getNextEntityName(
|
|
|
|
|
"OPTION",
|
|
|
|
|
renderPairs.map((pair: any) => pair.value),
|
|
|
|
|
),
|
|
|
|
|
key: getNextEntityName(
|
|
|
|
|
"OPTION",
|
|
|
|
|
renderPairs.map((pair: any) => pair.value),
|
|
|
|
|
),
|
|
|
|
|
});
|
2020-04-01 08:09:57 +00:00
|
|
|
|
|
|
|
|
setRenderPairs(updatedRenderPairs);
|
|
|
|
|
props.updatePairs(pairs);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-21 07:55:56 +00:00
|
|
|
function onInputFocus() {
|
|
|
|
|
setTyping(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onInputBlur() {
|
|
|
|
|
setTyping(false);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-01 08:09:57 +00:00
|
|
|
return (
|
2021-04-28 10:28:39 +00:00
|
|
|
<>
|
2020-04-01 08:09:57 +00:00
|
|
|
{renderPairs.map((pair: DropDownOptionWithKey, index) => {
|
|
|
|
|
return (
|
2022-04-14 07:53:41 +00:00
|
|
|
<ControlWrapper key={pair.key} orientation={"HORIZONTAL"}>
|
2021-10-28 05:14:59 +00:00
|
|
|
<StyledInputGroup
|
2021-03-15 12:17:56 +00:00
|
|
|
dataType={"text"}
|
2021-09-21 07:55:56 +00:00
|
|
|
onBlur={onInputBlur}
|
2021-03-15 12:17:56 +00:00
|
|
|
onChange={(value: string) => {
|
|
|
|
|
updateKey(index, value);
|
2020-04-01 08:09:57 +00:00
|
|
|
}}
|
2021-09-21 07:55:56 +00:00
|
|
|
onFocus={onInputFocus}
|
2021-04-28 10:28:39 +00:00
|
|
|
placeholder={"Name"}
|
2021-09-21 07:55:56 +00:00
|
|
|
value={pair.label}
|
2020-04-01 08:09:57 +00:00
|
|
|
/>
|
2021-03-15 12:17:56 +00:00
|
|
|
<StyledBox />
|
|
|
|
|
<StyledInputGroup
|
|
|
|
|
dataType={"text"}
|
2021-09-21 07:55:56 +00:00
|
|
|
onBlur={onInputBlur}
|
2021-03-15 12:17:56 +00:00
|
|
|
onChange={(value: string) => {
|
|
|
|
|
updateValue(index, value);
|
2020-04-01 08:09:57 +00:00
|
|
|
}}
|
2021-09-21 07:55:56 +00:00
|
|
|
onFocus={onInputFocus}
|
2021-04-28 10:28:39 +00:00
|
|
|
placeholder={"Value"}
|
2021-09-21 07:55:56 +00:00
|
|
|
value={pair.value}
|
2020-04-01 08:09:57 +00:00
|
|
|
/>
|
2022-04-14 07:53:41 +00:00
|
|
|
<StyledBox />
|
|
|
|
|
<StyledButton
|
2020-04-01 08:09:57 +00:00
|
|
|
onClick={() => {
|
|
|
|
|
deletePair(index);
|
|
|
|
|
}}
|
2022-04-14 07:53:41 +00:00
|
|
|
>
|
|
|
|
|
<StyledDeleteIcon />
|
|
|
|
|
</StyledButton>
|
|
|
|
|
</ControlWrapper>
|
2020-04-01 08:09:57 +00:00
|
|
|
);
|
|
|
|
|
})}
|
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}
|
2022-02-02 12:10:40 +00:00
|
|
|
className="t--property-control-options-add"
|
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
|
|
|
);
|
|
|
|
|
}
|