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

49 lines
1.2 KiB
TypeScript
Raw Normal View History

2019-11-05 05:09:50 +00:00
import React from "react";
import BaseControl, { ControlData, ControlProps } from "./BaseControl";
import { DropdownOption } from "components/constants";
2020-04-01 08:09:57 +00:00
import { KeyValueComponent } from "./KeyValueComponent";
import { isDynamicValue } from "utils/DynamicBindingUtils";
2019-12-18 17:05:28 +00:00
2020-04-01 08:09:57 +00:00
export type DropDownOptionWithKey = DropdownOption & {
2020-03-19 03:07:31 +00:00
key: string;
};
2020-04-01 08:09:57 +00:00
class OptionControl extends BaseControl<ControlProps> {
2019-11-05 05:09:50 +00:00
render() {
return (
2020-04-01 08:09:57 +00:00
<KeyValueComponent
pairs={this.props.propertyValue}
updatePairs={this.updateOptions}
/>
2019-11-05 05:09:50 +00:00
);
}
updateOptions = (options: DropdownOption[], isUpdatedViaKeyboard = false) => {
this.updateProperty("options", options, isUpdatedViaKeyboard);
2019-11-05 05:09:50 +00:00
};
static getControlType() {
2019-11-05 05:09:50 +00:00
return "OPTION_INPUT";
}
static canDisplayValueInUI(config: ControlData, value: any): boolean {
if (isDynamicValue(value)) return false;
try {
const pairs: DropdownOption[] = JSON.parse(value);
for (const x of pairs) {
const keys = Object.keys(x);
if (!keys.includes("label") || !keys.includes("value")) {
return false;
}
}
} catch {
return false;
}
return true;
}
2019-11-05 05:09:50 +00:00
}
export default OptionControl;