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

245 lines
6.8 KiB
TypeScript
Raw Normal View History

2020-03-06 09:45:21 +00:00
import React from "react";
import BaseWidget, { WidgetProps, WidgetState } from "./BaseWidget";
import { WidgetType } from "constants/WidgetConstants";
import ButtonComponent, {
ButtonType,
} from "components/designSystems/blueprint/ButtonComponent";
import { EventType, ExecutionResult } from "constants/ActionConstants";
2020-03-16 07:59:07 +00:00
import {
BASE_WIDGET_VALIDATION,
WidgetPropertyValidationType,
} from "utils/WidgetValidation";
2020-03-06 09:45:21 +00:00
import { VALIDATION_TYPES } from "constants/WidgetValidation";
import { TriggerPropertiesMap } from "utils/WidgetFactory";
import * as Sentry from "@sentry/react";
import withMeta, { WithMeta } from "./MetaHOC";
2020-03-06 09:45:21 +00:00
class FormButtonWidget extends BaseWidget<
FormButtonWidgetProps,
2020-04-13 08:24:13 +00:00
FormButtonWidgetState
2020-03-06 09:45:21 +00:00
> {
onButtonClickBound: (event: React.MouseEvent<HTMLElement>) => void;
clickWithRecaptchaBound: (token: string) => void;
2020-03-06 09:45:21 +00:00
constructor(props: FormButtonWidgetProps) {
super(props);
this.onButtonClickBound = this.onButtonClick.bind(this);
this.clickWithRecaptchaBound = this.clickWithRecaptcha.bind(this);
2020-03-06 09:45:21 +00:00
this.state = {
isLoading: false,
};
}
static getPropertyPaneConfig() {
return [
{
sectionName: "General",
children: [
{
propertyName: "text",
label: "Label",
helpText: "Sets the label of the button",
controlType: "INPUT_TEXT",
placeholderText: "Enter label text",
isBindProperty: true,
isTriggerProperty: false,
},
{
propertyName: "buttonStyle",
label: "Button Style",
helpText: "Changes the style of the button",
controlType: "DROP_DOWN",
options: [
{
label: "Primary Button",
value: "PRIMARY_BUTTON",
},
{
label: "Secondary Button",
value: "SECONDARY_BUTTON",
},
{
label: "Danger Button",
value: "DANGER_BUTTON",
},
],
isBindProperty: false,
isTriggerProperty: false,
},
{
helpText:
"Disables the button when the parent form has a required widget that is not filled",
propertyName: "disabledWhenInvalid",
label: "Disabled Invalid Forms",
controlType: "SWITCH",
isBindProperty: false,
isTriggerProperty: false,
},
{
helpText:
"Resets the fields within the parent form when the click action succeeds",
propertyName: "resetFormOnClick",
label: "Reset Form on Success",
controlType: "SWITCH",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: false,
},
{
propertyName: "isVisible",
label: "Visible",
helpText: "Controls the visibility of the widget",
controlType: "SWITCH",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: false,
},
{
propertyName: "googleRecaptchaKey",
label: "Google Recaptcha Key",
helpText: "Sets Google Recaptcha v3 site key for button",
controlType: "INPUT_TEXT",
placeholderText: "Enter google recaptcha key",
isBindProperty: true,
isTriggerProperty: false,
},
],
},
{
sectionName: "Actions",
children: [
{
helpText: "Triggers an action when the button is clicked",
propertyName: "onClick",
label: "onClick",
controlType: "ACTION_SELECTOR",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: true,
},
],
},
];
}
static getMetaPropertiesMap(): Record<string, any> {
return {
recaptchaToken: undefined,
};
}
2020-03-06 09:45:21 +00:00
static getPropertyValidationMap(): WidgetPropertyValidationType {
return {
2020-03-16 07:59:07 +00:00
...BASE_WIDGET_VALIDATION,
2020-03-06 09:45:21 +00:00
text: VALIDATION_TYPES.TEXT,
disabledWhenInvalid: VALIDATION_TYPES.BOOLEAN,
buttonStyle: VALIDATION_TYPES.TEXT,
2020-03-16 07:59:07 +00:00
buttonType: VALIDATION_TYPES.TEXT,
// onClick: VALIDATION_TYPES.ACTION_SELECTOR,
2020-03-06 09:45:21 +00:00
};
}
static getTriggerPropertyMap(): TriggerPropertiesMap {
return {
onClick: true,
};
}
clickWithRecaptcha(token: string) {
if (this.props.onClick) {
this.setState({
isLoading: true,
});
}
this.props.updateWidgetMetaProperty("recaptchaToken", token, {
dynamicString: this.props.onClick,
event: {
type: EventType.ON_CLICK,
callback: this.handleActionResult,
},
});
}
2020-03-06 09:45:21 +00:00
onButtonClick() {
if (this.props.onClick) {
this.setState({
isLoading: true,
});
super.executeAction({
dynamicString: this.props.onClick,
event: {
type: EventType.ON_CLICK,
callback: this.handleActionResult,
},
});
2020-03-13 07:24:03 +00:00
} else if (this.props.resetFormOnClick && this.props.onReset) {
this.props.onReset();
2020-03-06 09:45:21 +00:00
}
}
handleActionResult = (result: ExecutionResult) => {
this.setState({
isLoading: false,
});
if (result.success) {
if (this.props.resetFormOnClick && this.props.onReset)
this.props.onReset();
}
};
getPageView() {
const disabled =
this.props.disabledWhenInvalid &&
"isFormValid" in this.props &&
!this.props.isFormValid;
return (
<ButtonComponent
buttonStyle={this.props.buttonStyle}
widgetId={this.props.widgetId}
key={this.props.widgetId}
widgetName={this.props.widgetName}
text={this.props.text}
disabled={disabled}
onClick={!disabled ? this.onButtonClickBound : undefined}
2020-03-06 09:45:21 +00:00
isLoading={this.props.isLoading || this.state.isLoading}
2020-03-13 07:24:03 +00:00
type={this.props.buttonType || ButtonType.BUTTON}
googleRecaptchaKey={this.props.googleRecaptchaKey}
clickWithRecaptcha={this.clickWithRecaptchaBound}
2020-03-06 09:45:21 +00:00
/>
);
}
getWidgetType(): WidgetType {
return "FORM_BUTTON_WIDGET";
}
}
export type ButtonStyle =
| "PRIMARY_BUTTON"
| "SECONDARY_BUTTON"
| "SUCCESS_BUTTON"
| "DANGER_BUTTON";
export interface FormButtonWidgetProps extends WidgetProps, WithMeta {
2020-03-06 09:45:21 +00:00
text?: string;
buttonStyle?: ButtonStyle;
onClick?: string;
isVisible?: boolean;
2020-03-13 07:24:03 +00:00
buttonType: ButtonType;
2020-03-06 09:45:21 +00:00
isFormValid?: boolean;
resetFormOnClick?: boolean;
onReset?: () => void;
disabledWhenInvalid?: boolean;
googleRecaptchaKey?: string;
2020-03-06 09:45:21 +00:00
}
2020-04-13 08:24:13 +00:00
export interface FormButtonWidgetState extends WidgetState {
isLoading: boolean;
}
2020-03-06 09:45:21 +00:00
export default FormButtonWidget;
export const ProfiledFormButtonWidget = Sentry.withProfiler(
withMeta(FormButtonWidget),
);