PromucFlow_constructor/app/client/src/widgets/RadioGroupWidget.tsx

179 lines
5.5 KiB
TypeScript
Raw Normal View History

2019-11-07 10:15:33 +00:00
import React from "react";
2019-09-09 09:08:54 +00:00
import BaseWidget, { WidgetProps, WidgetState } from "./BaseWidget";
2019-11-25 05:07:27 +00:00
import { WidgetType } from "constants/WidgetConstants";
import RadioGroupComponent from "components/designSystems/blueprint/RadioGroupComponent";
import { EventType } from "constants/AppsmithActionConstants/ActionConstants";
import { ValidationTypes } from "constants/WidgetValidation";
import * as Sentry from "@sentry/react";
2020-10-06 09:01:51 +00:00
import withMeta, { WithMeta } from "./MetaHOC";
import { EvaluationSubstitutionType } from "entities/DataTree/dataTreeFactory";
2019-03-21 12:10:32 +00:00
class RadioGroupWidget extends BaseWidget<RadioGroupWidgetProps, WidgetState> {
static getPropertyPaneConfig() {
return [
{
sectionName: "General",
children: [
{
helpText:
"Displays a list of options for a user to select. Values must be unique",
propertyName: "options",
label: "Options",
controlType: "OPTION_INPUT",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: false,
validation: {
type: ValidationTypes.ARRAY,
params: {
children: {
type: ValidationTypes.OBJECT,
params: {
allowedKeys: [
{
name: "label",
isUnique: true,
type: ValidationTypes.TEXT,
},
{
name: "value",
isUnique: true,
type: ValidationTypes.TEXT,
},
],
},
},
},
},
evaluationSubstitutionType:
EvaluationSubstitutionType.SMART_SUBSTITUTE,
},
{
helpText: "Selects a value of the options entered by default",
propertyName: "defaultOptionValue",
label: "Default Selected Value",
placeholderText: "Enter option value",
controlType: "INPUT_TEXT",
isBindProperty: true,
isTriggerProperty: false,
validation: { type: ValidationTypes.TEXT },
},
{
propertyName: "isRequired",
label: "Required",
helpText: "Makes input to the widget mandatory",
controlType: "SWITCH",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: false,
validation: { type: ValidationTypes.BOOLEAN },
},
{
helpText: "Controls the visibility of the widget",
propertyName: "isVisible",
label: "Visible",
controlType: "SWITCH",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: false,
validation: { type: ValidationTypes.BOOLEAN },
},
{
propertyName: "isDisabled",
label: "Disabled",
helpText: "Disables input to this widget",
controlType: "SWITCH",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: false,
validation: { type: ValidationTypes.BOOLEAN },
},
],
},
{
sectionName: "Actions",
children: [
{
helpText:
"Triggers an action when a user changes the selected option",
propertyName: "onSelectionChange",
label: "onSelectionChange",
controlType: "ACTION_SELECTOR",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: true,
},
],
},
];
}
2020-01-17 09:28:26 +00:00
static getDerivedPropertiesMap() {
return {
selectedOption:
"{{_.find(this.options, { value: this.selectedOptionValue })}}",
2020-03-06 09:45:21 +00:00
isValid: `{{ this.isRequired ? !!this.selectedOptionValue : true }}`,
value: `{{this.selectedOptionValue}}`,
2020-01-17 09:28:26 +00:00
};
}
2020-03-13 07:24:03 +00:00
2020-04-17 16:15:09 +00:00
static getDefaultPropertiesMap(): Record<string, string> {
return {
selectedOptionValue: "defaultOptionValue",
};
2020-03-13 07:24:03 +00:00
}
2020-04-17 16:15:09 +00:00
static getMetaPropertiesMap(): Record<string, any> {
return {
selectedOptionValue: undefined,
2020-04-17 16:15:09 +00:00
};
2020-03-13 07:24:03 +00:00
}
2020-04-17 16:15:09 +00:00
2019-03-21 12:10:32 +00:00
getPageView() {
return (
<RadioGroupComponent
isDisabled={this.props.isDisabled}
isLoading={this.props.isLoading}
2019-03-21 12:10:32 +00:00
key={this.props.widgetId}
2020-05-25 09:32:01 +00:00
label={`${this.props.label}`}
onRadioSelectionChange={this.onRadioSelectionChange}
2019-03-21 12:10:32 +00:00
options={this.props.options}
selectedOptionValue={this.props.selectedOptionValue}
widgetId={this.props.widgetId}
2019-03-21 12:10:32 +00:00
/>
2019-09-09 09:08:54 +00:00
);
2019-03-21 12:10:32 +00:00
}
2019-11-05 05:09:50 +00:00
onRadioSelectionChange = (updatedValue: string) => {
2020-10-06 16:47:16 +00:00
this.props.updateWidgetMetaProperty("selectedOptionValue", updatedValue, {
2021-04-23 13:50:55 +00:00
triggerPropertyName: "onSelectionChange",
2020-10-06 16:47:16 +00:00
dynamicString: this.props.onSelectionChange,
event: {
type: EventType.ON_OPTION_CHANGE,
},
});
2019-11-05 05:09:50 +00:00
};
2019-03-21 12:10:32 +00:00
getWidgetType(): WidgetType {
2019-09-09 09:08:54 +00:00
return "RADIO_GROUP_WIDGET";
2019-03-21 12:10:32 +00:00
}
}
2019-09-12 08:11:25 +00:00
export interface RadioOption {
label: string;
value: string;
2019-09-12 08:11:25 +00:00
}
2020-10-06 09:01:51 +00:00
export interface RadioGroupWidgetProps extends WidgetProps, WithMeta {
2019-08-29 11:22:09 +00:00
label: string;
options: RadioOption[];
2019-11-05 05:09:50 +00:00
selectedOptionValue: string;
2020-02-18 10:41:52 +00:00
onSelectionChange: string;
2020-02-24 14:55:09 +00:00
defaultOptionValue: string;
2020-03-06 09:45:21 +00:00
isRequired?: boolean;
2019-03-21 12:10:32 +00:00
}
export default RadioGroupWidget;
2020-10-06 09:01:51 +00:00
export const ProfiledRadioGroupWidget = Sentry.withProfiler(
withMeta(RadioGroupWidget),
);