* Use triggerPaths and remove isDynamicTrigger argument * Remove triggerMaps from widgets * Fix property updates
205 lines
5.7 KiB
TypeScript
205 lines
5.7 KiB
TypeScript
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 } from "constants/ActionConstants";
|
|
import {
|
|
WidgetPropertyValidationType,
|
|
BASE_WIDGET_VALIDATION,
|
|
} from "utils/WidgetValidation";
|
|
import { VALIDATION_TYPES } from "constants/WidgetValidation";
|
|
import * as Sentry from "@sentry/react";
|
|
import withMeta, { WithMeta } from "./MetaHOC";
|
|
|
|
class ButtonWidget extends BaseWidget<ButtonWidgetProps, ButtonWidgetState> {
|
|
onButtonClickBound: (event: React.MouseEvent<HTMLElement>) => void;
|
|
clickWithRecaptchaBound: (token: string) => void;
|
|
constructor(props: ButtonWidgetProps) {
|
|
super(props);
|
|
this.onButtonClickBound = this.onButtonClick.bind(this);
|
|
this.clickWithRecaptchaBound = this.clickWithRecaptcha.bind(this);
|
|
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",
|
|
controlType: "DROP_DOWN",
|
|
helpText: "Changes the style of the button",
|
|
options: [
|
|
{
|
|
label: "Primary Button",
|
|
value: "PRIMARY_BUTTON",
|
|
},
|
|
{
|
|
label: "Secondary Button",
|
|
value: "SECONDARY_BUTTON",
|
|
},
|
|
{
|
|
label: "Danger Button",
|
|
value: "DANGER_BUTTON",
|
|
},
|
|
],
|
|
isBindProperty: false,
|
|
isTriggerProperty: false,
|
|
},
|
|
{
|
|
propertyName: "isVisible",
|
|
label: "Visible",
|
|
helpText: "Controls the visibility of the widget",
|
|
controlType: "SWITCH",
|
|
isJSConvertible: true,
|
|
isBindProperty: true,
|
|
isTriggerProperty: false,
|
|
},
|
|
{
|
|
propertyName: "isDisabled",
|
|
label: "Disabled",
|
|
controlType: "SWITCH",
|
|
helpText: "Disables clicks to this widget",
|
|
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 getPropertyValidationMap(): WidgetPropertyValidationType {
|
|
return {
|
|
...BASE_WIDGET_VALIDATION,
|
|
text: VALIDATION_TYPES.TEXT,
|
|
buttonStyle: VALIDATION_TYPES.TEXT,
|
|
// onClick: VALIDATION_TYPES.ACTION_SELECTOR,
|
|
};
|
|
}
|
|
static getMetaPropertiesMap(): Record<string, any> {
|
|
return {
|
|
recaptchaToken: undefined,
|
|
};
|
|
}
|
|
|
|
onButtonClick() {
|
|
if (this.props.onClick) {
|
|
this.setState({
|
|
isLoading: true,
|
|
});
|
|
super.executeAction({
|
|
dynamicString: this.props.onClick,
|
|
event: {
|
|
type: EventType.ON_CLICK,
|
|
callback: this.handleActionComplete,
|
|
},
|
|
});
|
|
}
|
|
}
|
|
|
|
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.handleActionComplete,
|
|
},
|
|
});
|
|
}
|
|
|
|
handleActionComplete = () => {
|
|
this.setState({
|
|
isLoading: false,
|
|
});
|
|
};
|
|
|
|
getPageView() {
|
|
return (
|
|
<ButtonComponent
|
|
buttonStyle={this.props.buttonStyle}
|
|
widgetId={this.props.widgetId}
|
|
key={this.props.widgetId}
|
|
widgetName={this.props.widgetName}
|
|
text={this.props.text}
|
|
disabled={this.props.isDisabled}
|
|
onClick={!this.props.isDisabled ? this.onButtonClickBound : undefined}
|
|
isLoading={this.props.isLoading || this.state.isLoading}
|
|
type={this.props.buttonType || ButtonType.BUTTON}
|
|
googleRecaptchaKey={this.props.googleRecaptchaKey}
|
|
clickWithRecaptcha={this.clickWithRecaptchaBound}
|
|
/>
|
|
);
|
|
}
|
|
|
|
getWidgetType(): WidgetType {
|
|
return "BUTTON_WIDGET";
|
|
}
|
|
}
|
|
|
|
export type ButtonStyle =
|
|
| "PRIMARY_BUTTON"
|
|
| "SECONDARY_BUTTON"
|
|
| "SUCCESS_BUTTON"
|
|
| "DANGER_BUTTON";
|
|
|
|
export interface ButtonWidgetProps extends WidgetProps, WithMeta {
|
|
text?: string;
|
|
buttonStyle?: ButtonStyle;
|
|
onClick?: string;
|
|
isDisabled?: boolean;
|
|
isVisible?: boolean;
|
|
buttonType?: ButtonType;
|
|
googleRecaptchaKey?: string;
|
|
}
|
|
|
|
interface ButtonWidgetState extends WidgetState {
|
|
isLoading: boolean;
|
|
}
|
|
|
|
export default ButtonWidget;
|
|
export const ProfiledButtonWidget = Sentry.withProfiler(withMeta(ButtonWidget));
|