PromucFlow_constructor/app/client/src/components/propertyControls/OptionControl.tsx

133 lines
3.7 KiB
TypeScript
Raw Normal View History

2019-11-05 05:09:50 +00:00
import React from "react";
import BaseControl, { ControlProps } from "./BaseControl";
2020-01-23 07:53:36 +00:00
import {
ControlWrapper,
StyledInputGroup,
StyledPropertyPaneButton,
} from "./StyledControls";
2019-11-25 05:07:27 +00:00
import { DropdownOption } from "widgets/DropdownWidget";
import { ControlType } from "constants/PropertyControlConstants";
import { generateReactKey } from "utils/generators";
2019-12-18 17:05:28 +00:00
import styled from "constants/DefaultTheme";
import { FormIcons } from "icons/FormIcons";
import { AnyStyledComponent } from "styled-components";
const StyledDeleteIcon = styled(FormIcons.DELETE_ICON as AnyStyledComponent)`
padding: 5px 5px;
position: absolute;
2020-01-28 08:21:22 +00:00
right: -4px;
2019-12-18 17:05:28 +00:00
cursor: pointer;
`;
const StyledOptionControlInputGroup = styled(StyledInputGroup)`
margin-right: 2px;
`;
const StyledOptionControlWrapper = styled(ControlWrapper)`
display: flex;
justify-content: flex-start;
padding-right: 16px;
`;
2019-11-05 05:09:50 +00:00
class OptionControl extends BaseControl<ControlProps> {
render() {
const options: DropdownOption[] = this.props.propertyValue || [{}];
return (
<ControlWrapper>
<label>{this.props.label}</label>
{options.map((option, index) => {
return (
2019-12-18 17:05:28 +00:00
<StyledOptionControlWrapper
orientation={"HORIZONTAL"}
key={option.id}
>
<StyledOptionControlInputGroup
2019-11-05 05:09:50 +00:00
type={"text"}
placeholder={"Name"}
onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
this.updateOptionLabel(index, event.target.value);
}}
defaultValue={option.label}
/>
2019-12-18 17:05:28 +00:00
<StyledOptionControlInputGroup
2019-11-05 05:09:50 +00:00
type={"text"}
placeholder={"Value"}
onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
this.updateOptionValue(index, event.target.value);
}}
defaultValue={option.value}
/>
2019-12-18 17:05:28 +00:00
<StyledDeleteIcon
height={20}
width={20}
onClick={() => {
this.deleteOption(index);
}}
2019-12-18 17:05:28 +00:00
/>
</StyledOptionControlWrapper>
2019-11-05 05:09:50 +00:00
);
})}
2020-01-23 07:53:36 +00:00
<StyledPropertyPaneButton
2019-12-18 17:05:28 +00:00
text={"Option"}
icon={"plus"}
color={"#FFFFFF"}
minimal={true}
onClick={this.addOption}
/>
2019-11-05 05:09:50 +00:00
</ControlWrapper>
);
}
deleteOption = (index: number) => {
const options: DropdownOption[] = this.props.propertyValue.slice();
options.splice(index, 1);
2019-11-05 05:09:50 +00:00
this.updateProperty("options", options);
};
updateOptionLabel = (index: number, updatedLabel: string) => {
const options: DropdownOption[] = this.props.propertyValue;
this.updateProperty(
"options",
options.map((option, optionIndex) => {
if (index !== optionIndex) {
return option;
}
return {
...option,
label: updatedLabel,
};
}),
);
};
2019-11-05 05:09:50 +00:00
updateOptionValue = (index: number, updatedValue: string) => {
const options: DropdownOption[] = this.props.propertyValue;
this.updateProperty(
"options",
options.map((option, optionIndex) => {
if (index !== optionIndex) {
return option;
}
return {
...option,
value: updatedValue,
};
}),
);
2019-11-05 05:09:50 +00:00
};
addOption = () => {
const options: DropdownOption[] = this.props.propertyValue
? this.props.propertyValue.slice()
: [];
options.push({ label: "", value: "", id: generateReactKey() });
2019-11-05 05:09:50 +00:00
this.updateProperty("options", options);
};
getControlType(): ControlType {
return "OPTION_INPUT";
}
}
export default OptionControl;