2019-11-05 05:09:50 +00:00
|
|
|
import React from "react";
|
|
|
|
|
import BaseControl, { ControlProps } from "./BaseControl";
|
|
|
|
|
import { ControlWrapper, StyledInputGroup } from "./StyledControls";
|
|
|
|
|
import { Button } from "@blueprintjs/core";
|
2019-11-25 05:07:27 +00:00
|
|
|
import { DropdownOption } from "widgets/DropdownWidget";
|
|
|
|
|
import { ControlType } from "constants/PropertyControlConstants";
|
|
|
|
|
import { generateReactKey } from "utils/generators";
|
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-11-13 10:35:35 +00:00
|
|
|
<ControlWrapper orientation={"HORIZONTAL"} key={option.id}>
|
2019-11-05 05:09:50 +00:00
|
|
|
<StyledInputGroup
|
|
|
|
|
type={"text"}
|
|
|
|
|
placeholder={"Name"}
|
|
|
|
|
onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
this.updateOptionLabel(index, event.target.value);
|
|
|
|
|
}}
|
|
|
|
|
defaultValue={option.label}
|
|
|
|
|
/>
|
|
|
|
|
<StyledInputGroup
|
|
|
|
|
type={"text"}
|
|
|
|
|
placeholder={"Value"}
|
|
|
|
|
onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
this.updateOptionValue(index, event.target.value);
|
|
|
|
|
}}
|
|
|
|
|
defaultValue={option.value}
|
|
|
|
|
/>
|
2019-11-13 10:35:35 +00:00
|
|
|
<Button
|
|
|
|
|
color={"#A3B3BF"}
|
|
|
|
|
icon={"delete"}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
this.deleteOption(index);
|
|
|
|
|
}}
|
|
|
|
|
></Button>
|
2019-11-05 05:09:50 +00:00
|
|
|
</ControlWrapper>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
<Button text={"Option"} icon={"add"} onClick={this.addOption} />
|
|
|
|
|
</ControlWrapper>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-13 10:35:35 +00:00
|
|
|
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);
|
|
|
|
|
};
|
|
|
|
|
|
2019-11-13 10:35:35 +00:00
|
|
|
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) => {
|
2019-11-13 10:35:35 +00:00
|
|
|
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 = () => {
|
2019-11-13 10:35:35 +00:00
|
|
|
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;
|