2019-09-09 09:08:54 +00:00
|
|
|
import React from "react";
|
|
|
|
|
import BaseWidget, { WidgetProps, WidgetState } from "./BaseWidget";
|
2019-11-25 05:07:27 +00:00
|
|
|
import { WidgetType } from "constants/WidgetConstants";
|
|
|
|
|
import CheckboxComponent from "components/designSystems/blueprint/CheckboxComponent";
|
2020-02-18 10:41:52 +00:00
|
|
|
import { EventType } from "constants/ActionConstants";
|
2019-11-25 05:07:27 +00:00
|
|
|
import { VALIDATION_TYPES } from "constants/WidgetValidation";
|
2020-03-16 07:59:07 +00:00
|
|
|
import {
|
|
|
|
|
WidgetPropertyValidationType,
|
|
|
|
|
BASE_WIDGET_VALIDATION,
|
2020-10-21 04:25:32 +00:00
|
|
|
} from "utils/WidgetValidation";
|
2020-06-09 12:04:38 +00:00
|
|
|
import {
|
|
|
|
|
TriggerPropertiesMap,
|
|
|
|
|
DerivedPropertiesMap,
|
|
|
|
|
} from "utils/WidgetFactory";
|
2020-08-28 17:23:07 +00:00
|
|
|
import * as Sentry from "@sentry/react";
|
2020-10-06 09:01:51 +00:00
|
|
|
import withMeta, { WithMeta } from "./MetaHOC";
|
2021-02-16 12:15:17 +00:00
|
|
|
import { AlignWidget } from "./SwitchWidget";
|
2019-03-21 12:10:32 +00:00
|
|
|
|
2019-09-09 09:08:54 +00:00
|
|
|
class CheckboxWidget extends BaseWidget<CheckboxWidgetProps, WidgetState> {
|
2021-02-16 10:29:08 +00:00
|
|
|
static getPropertyPaneConfig() {
|
|
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
sectionName: "General",
|
|
|
|
|
children: [
|
|
|
|
|
{
|
|
|
|
|
propertyName: "label",
|
|
|
|
|
label: "Label",
|
|
|
|
|
controlType: "INPUT_TEXT",
|
|
|
|
|
helpText: "Displays a label next to the widget",
|
|
|
|
|
placeholderText: "Enter label text",
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
|
|
|
|
},
|
2021-02-16 12:15:17 +00:00
|
|
|
{
|
|
|
|
|
propertyName: "alignWidget",
|
|
|
|
|
helpText: "Sets the alignment of the widget",
|
|
|
|
|
label: "Alignment",
|
|
|
|
|
controlType: "DROP_DOWN",
|
|
|
|
|
options: [
|
|
|
|
|
{
|
|
|
|
|
label: "Left",
|
|
|
|
|
value: "LEFT",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: "Right",
|
|
|
|
|
value: "RIGHT",
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
|
|
|
|
},
|
2021-02-16 10:29:08 +00:00
|
|
|
{
|
|
|
|
|
propertyName: "defaultCheckedState",
|
|
|
|
|
label: "Default Selected",
|
|
|
|
|
helpText:
|
|
|
|
|
"Checks / un-checks the checkbox by default. Changes to the default selection update the widget state",
|
|
|
|
|
controlType: "SWITCH",
|
|
|
|
|
isJSConvertible: true,
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
propertyName: "isRequired",
|
|
|
|
|
label: "Required",
|
|
|
|
|
helpText: "Makes input to the widget mandatory",
|
|
|
|
|
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: "isDisabled",
|
|
|
|
|
label: "Disabled",
|
|
|
|
|
controlType: "SWITCH",
|
|
|
|
|
helpText: "Disables input to this widget",
|
|
|
|
|
isJSConvertible: true,
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
sectionName: "Actions",
|
|
|
|
|
children: [
|
|
|
|
|
{
|
|
|
|
|
helpText: "Triggers an action when the check state is changed",
|
|
|
|
|
propertyName: "onCheckChange",
|
|
|
|
|
label: "onCheckChange",
|
|
|
|
|
controlType: "ACTION_SELECTOR",
|
|
|
|
|
isJSConvertible: true,
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: true,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
}
|
2019-11-19 12:44:58 +00:00
|
|
|
static getPropertyValidationMap(): WidgetPropertyValidationType {
|
|
|
|
|
return {
|
2020-03-16 07:59:07 +00:00
|
|
|
...BASE_WIDGET_VALIDATION,
|
2019-11-19 12:44:58 +00:00
|
|
|
label: VALIDATION_TYPES.TEXT,
|
2019-11-22 13:12:39 +00:00
|
|
|
defaultCheckedState: VALIDATION_TYPES.BOOLEAN,
|
2020-07-06 08:16:42 +00:00
|
|
|
// onCheckChange: VALIDATION_TYPES.ACTION_SELECTOR,
|
2019-11-19 12:44:58 +00:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 10:41:52 +00:00
|
|
|
static getTriggerPropertyMap(): TriggerPropertiesMap {
|
|
|
|
|
return {
|
|
|
|
|
onCheckChange: true,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-17 16:15:09 +00:00
|
|
|
static getDefaultPropertiesMap(): Record<string, string> {
|
|
|
|
|
return {
|
|
|
|
|
isChecked: "defaultCheckedState",
|
2021-02-16 12:15:17 +00:00
|
|
|
alignWidget: "LEFT",
|
2020-04-17 16:15:09 +00:00
|
|
|
};
|
2020-03-13 07:24:03 +00:00
|
|
|
}
|
|
|
|
|
|
2020-06-09 12:04:38 +00:00
|
|
|
static getDerivedPropertiesMap(): DerivedPropertiesMap {
|
|
|
|
|
return {
|
2021-02-16 12:15:17 +00:00
|
|
|
value: `{{!!this.isChecked}}`,
|
2020-11-26 11:21:23 +00:00
|
|
|
isValid: `{{ this.isRequired ? !!this.isChecked : true }}`,
|
2020-06-09 12:04:38 +00:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-17 16:15:09 +00:00
|
|
|
static getMetaPropertiesMap(): Record<string, any> {
|
|
|
|
|
return {
|
2020-04-21 07:54:23 +00:00
|
|
|
isChecked: undefined,
|
2020-04-17 16:15:09 +00:00
|
|
|
};
|
2020-03-13 07:24:03 +00:00
|
|
|
}
|
|
|
|
|
|
2019-03-21 12:10:32 +00:00
|
|
|
getPageView() {
|
|
|
|
|
return (
|
|
|
|
|
<CheckboxComponent
|
2020-11-26 11:21:23 +00:00
|
|
|
isRequired={this.props.isRequired}
|
2020-03-13 07:24:03 +00:00
|
|
|
isChecked={!!this.props.isChecked}
|
2021-02-16 12:15:17 +00:00
|
|
|
alignWidget={this.props.alignWidget}
|
2019-09-12 08:11:25 +00:00
|
|
|
label={this.props.label}
|
2019-03-21 12:10:32 +00:00
|
|
|
widgetId={this.props.widgetId}
|
|
|
|
|
key={this.props.widgetId}
|
2019-10-31 09:04:19 +00:00
|
|
|
isDisabled={this.props.isDisabled}
|
|
|
|
|
onCheckChange={this.onCheckChange}
|
2019-12-03 04:41:10 +00:00
|
|
|
isLoading={this.props.isLoading}
|
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-10-31 09:04:19 +00:00
|
|
|
onCheckChange = (isChecked: boolean) => {
|
2020-10-06 16:47:16 +00:00
|
|
|
this.props.updateWidgetMetaProperty("isChecked", isChecked, {
|
|
|
|
|
dynamicString: this.props.onCheckChange,
|
|
|
|
|
event: {
|
|
|
|
|
type: EventType.ON_CHECK_CHANGE,
|
|
|
|
|
},
|
|
|
|
|
});
|
2019-10-31 09:04:19 +00:00
|
|
|
};
|
|
|
|
|
|
2019-03-21 12:10:32 +00:00
|
|
|
getWidgetType(): WidgetType {
|
2019-09-13 10:45:49 +00:00
|
|
|
return "CHECKBOX_WIDGET";
|
2019-03-21 12:10:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-06 09:01:51 +00:00
|
|
|
export interface CheckboxWidgetProps extends WidgetProps, WithMeta {
|
2019-09-13 10:45:49 +00:00
|
|
|
label: string;
|
|
|
|
|
defaultCheckedState: boolean;
|
2019-10-31 09:04:19 +00:00
|
|
|
isChecked?: boolean;
|
|
|
|
|
isDisabled?: boolean;
|
2020-02-18 10:41:52 +00:00
|
|
|
onCheckChange?: string;
|
2020-11-26 11:21:23 +00:00
|
|
|
isRequired?: boolean;
|
2021-02-16 12:15:17 +00:00
|
|
|
alignWidget: AlignWidget;
|
2019-03-21 12:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-09 09:08:54 +00:00
|
|
|
export default CheckboxWidget;
|
2020-10-06 09:01:51 +00:00
|
|
|
export const ProfiledCheckboxWidget = Sentry.withProfiler(
|
|
|
|
|
withMeta(CheckboxWidget),
|
|
|
|
|
);
|