2021-02-16 12:15:17 +00:00
|
|
|
import React from "react";
|
2021-09-09 15:10:22 +00:00
|
|
|
import BaseWidget, { WidgetProps, WidgetState } from "../../BaseWidget";
|
2021-02-16 12:15:17 +00:00
|
|
|
import { WidgetType } from "constants/WidgetConstants";
|
2022-05-25 09:46:14 +00:00
|
|
|
import SwitchComponent from "../component";
|
2021-09-09 15:10:22 +00:00
|
|
|
|
2021-07-26 05:50:46 +00:00
|
|
|
import { ValidationTypes } from "constants/WidgetValidation";
|
2021-09-09 15:10:22 +00:00
|
|
|
|
2021-03-30 05:29:03 +00:00
|
|
|
import { EventType } from "constants/AppsmithActionConstants/ActionConstants";
|
2021-02-25 14:00:02 +00:00
|
|
|
import { DerivedPropertiesMap } from "utils/WidgetFactory";
|
2022-05-25 11:33:33 +00:00
|
|
|
import { LabelPosition } from "components/constants";
|
|
|
|
|
import { AlignWidgetTypes } from "widgets/constants";
|
2021-02-16 12:15:17 +00:00
|
|
|
class SwitchWidget extends BaseWidget<SwitchWidgetProps, WidgetState> {
|
2022-08-15 08:05:46 +00:00
|
|
|
static getPropertyPaneContentConfig() {
|
|
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
sectionName: "Label",
|
|
|
|
|
children: [
|
|
|
|
|
{
|
|
|
|
|
propertyName: "label",
|
|
|
|
|
label: "Text",
|
|
|
|
|
controlType: "INPUT_TEXT",
|
|
|
|
|
helpText: "Displays a label next to the widget",
|
|
|
|
|
placeholderText: "Enable Option",
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
|
|
|
|
validation: { type: ValidationTypes.TEXT },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
helpText: "Sets the label position of the widget",
|
|
|
|
|
propertyName: "labelPosition",
|
|
|
|
|
label: "Position",
|
|
|
|
|
controlType: "DROP_DOWN",
|
|
|
|
|
options: [
|
|
|
|
|
{ label: "Left", value: LabelPosition.Left },
|
|
|
|
|
{ label: "Right", value: LabelPosition.Right },
|
|
|
|
|
],
|
|
|
|
|
isBindProperty: false,
|
|
|
|
|
isTriggerProperty: false,
|
|
|
|
|
validation: { type: ValidationTypes.TEXT },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
propertyName: "alignWidget",
|
|
|
|
|
helpText: "Sets the alignment of the widget",
|
|
|
|
|
label: "Alignment",
|
|
|
|
|
controlType: "LABEL_ALIGNMENT_OPTIONS",
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
|
|
|
|
options: [
|
|
|
|
|
{
|
|
|
|
|
icon: "LEFT_ALIGN",
|
|
|
|
|
value: AlignWidgetTypes.LEFT,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
icon: "RIGHT_ALIGN",
|
|
|
|
|
value: AlignWidgetTypes.RIGHT,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
validation: { type: ValidationTypes.TEXT },
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
sectionName: "General",
|
|
|
|
|
children: [
|
|
|
|
|
{
|
|
|
|
|
propertyName: "defaultSwitchState",
|
|
|
|
|
label: "Default State",
|
|
|
|
|
helpText:
|
|
|
|
|
"On / Off the Switch by default. Changes to the default selection update the widget state",
|
|
|
|
|
controlType: "SWITCH",
|
|
|
|
|
isJSConvertible: true,
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
|
|
|
|
validation: { type: ValidationTypes.BOOLEAN },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
propertyName: "isVisible",
|
|
|
|
|
label: "Visible",
|
|
|
|
|
helpText: "Controls the visibility of the widget",
|
|
|
|
|
controlType: "SWITCH",
|
|
|
|
|
isJSConvertible: true,
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
|
|
|
|
validation: { type: ValidationTypes.BOOLEAN },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
propertyName: "isDisabled",
|
|
|
|
|
label: "Disabled",
|
|
|
|
|
controlType: "SWITCH",
|
|
|
|
|
helpText: "Disables input to this widget",
|
|
|
|
|
isJSConvertible: true,
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
|
|
|
|
validation: { type: ValidationTypes.BOOLEAN },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
propertyName: "animateLoading",
|
|
|
|
|
label: "Animate Loading",
|
|
|
|
|
controlType: "SWITCH",
|
|
|
|
|
helpText: "Controls the loading of the widget",
|
|
|
|
|
defaultValue: true,
|
|
|
|
|
isJSConvertible: true,
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
|
|
|
|
validation: { type: ValidationTypes.BOOLEAN },
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
sectionName: "Events",
|
|
|
|
|
children: [
|
|
|
|
|
{
|
|
|
|
|
helpText: "Triggers an action when the switch state is changed",
|
|
|
|
|
propertyName: "onChange",
|
|
|
|
|
label: "onChange",
|
|
|
|
|
controlType: "ACTION_SELECTOR",
|
|
|
|
|
isJSConvertible: true,
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: true,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static getPropertyPaneStyleConfig() {
|
|
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
sectionName: "Label Styles",
|
|
|
|
|
children: [
|
|
|
|
|
{
|
|
|
|
|
propertyName: "labelTextColor",
|
|
|
|
|
label: "Font Color",
|
|
|
|
|
controlType: "COLOR_PICKER",
|
|
|
|
|
isJSConvertible: true,
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
|
|
|
|
validation: {
|
|
|
|
|
type: ValidationTypes.TEXT,
|
|
|
|
|
params: {
|
|
|
|
|
regex: /^(?![<|{{]).+/,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
propertyName: "labelTextSize",
|
|
|
|
|
label: "Font Size",
|
|
|
|
|
controlType: "DROP_DOWN",
|
|
|
|
|
defaultValue: "0.875rem",
|
|
|
|
|
options: [
|
|
|
|
|
{
|
|
|
|
|
label: "S",
|
|
|
|
|
value: "0.875rem",
|
|
|
|
|
subText: "0.875rem",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: "M",
|
|
|
|
|
value: "1rem",
|
|
|
|
|
subText: "1rem",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: "L",
|
|
|
|
|
value: "1.25rem",
|
|
|
|
|
subText: "1.25rem",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: "XL",
|
|
|
|
|
value: "1.875rem",
|
|
|
|
|
subText: "1.875rem",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: "XXL",
|
|
|
|
|
value: "3rem",
|
|
|
|
|
subText: "3rem",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: "3XL",
|
|
|
|
|
value: "3.75rem",
|
|
|
|
|
subText: "3.75rem",
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
isJSConvertible: true,
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
|
|
|
|
validation: { type: ValidationTypes.TEXT },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
propertyName: "labelStyle",
|
|
|
|
|
label: "Emphasis",
|
|
|
|
|
controlType: "BUTTON_TABS",
|
|
|
|
|
options: [
|
|
|
|
|
{
|
|
|
|
|
icon: "BOLD_FONT",
|
|
|
|
|
value: "BOLD",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
icon: "ITALICS_FONT",
|
|
|
|
|
value: "ITALIC",
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
isJSConvertible: true,
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
|
|
|
|
validation: { type: ValidationTypes.TEXT },
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
sectionName: "Color",
|
|
|
|
|
children: [
|
|
|
|
|
{
|
|
|
|
|
propertyName: "accentColor",
|
|
|
|
|
helpText: "Sets the background color of the widget",
|
|
|
|
|
label: "Accent Color",
|
|
|
|
|
controlType: "COLOR_PICKER",
|
|
|
|
|
isJSConvertible: true,
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
|
|
|
|
validation: { type: ValidationTypes.TEXT },
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-16 12:15:17 +00:00
|
|
|
getPageView() {
|
|
|
|
|
return (
|
|
|
|
|
<SwitchComponent
|
2022-05-04 09:45:57 +00:00
|
|
|
accentColor={this.props.accentColor}
|
2022-05-25 11:33:33 +00:00
|
|
|
alignWidget={this.props.alignWidget}
|
2021-02-16 12:15:17 +00:00
|
|
|
isDisabled={this.props.isDisabled}
|
|
|
|
|
isLoading={this.props.isLoading}
|
2021-04-28 10:28:39 +00:00
|
|
|
isSwitchedOn={!!this.props.isSwitchedOn}
|
|
|
|
|
key={this.props.widgetId}
|
|
|
|
|
label={this.props.label}
|
2022-05-25 11:33:33 +00:00
|
|
|
labelPosition={this.props.labelPosition}
|
|
|
|
|
labelStyle={this.props.labelStyle}
|
|
|
|
|
labelTextColor={this.props.labelTextColor}
|
|
|
|
|
labelTextSize={this.props.labelTextSize}
|
2021-04-28 10:28:39 +00:00
|
|
|
onChange={this.onChange}
|
|
|
|
|
widgetId={this.props.widgetId}
|
2021-02-16 12:15:17 +00:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-09 15:10:22 +00:00
|
|
|
static getWidgetType(): WidgetType {
|
2021-02-16 12:15:17 +00:00
|
|
|
return "SWITCH_WIDGET";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static getDefaultPropertiesMap(): Record<string, string> {
|
|
|
|
|
return {
|
|
|
|
|
isSwitchedOn: "defaultSwitchState",
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static getMetaPropertiesMap(): Record<string, any> {
|
|
|
|
|
return {
|
|
|
|
|
isSwitchedOn: undefined,
|
2022-03-01 19:02:10 +00:00
|
|
|
isDirty: false,
|
2021-02-16 12:15:17 +00:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static getDerivedPropertiesMap(): DerivedPropertiesMap {
|
|
|
|
|
return {
|
|
|
|
|
value: `{{!!this.isSwitchedOn}}`,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-01 19:02:10 +00:00
|
|
|
componentDidUpdate(prevProps: SwitchWidgetProps): void {
|
|
|
|
|
if (
|
|
|
|
|
this.props.defaultSwitchState !== prevProps.defaultSwitchState &&
|
|
|
|
|
this.props.isDirty
|
|
|
|
|
) {
|
|
|
|
|
this.props.updateWidgetMetaProperty("isDirty", false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-16 12:15:17 +00:00
|
|
|
onChange = (isSwitchedOn: boolean) => {
|
2022-03-01 19:02:10 +00:00
|
|
|
if (!this.props.isDirty) {
|
|
|
|
|
this.props.updateWidgetMetaProperty("isDirty", true);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-16 12:15:17 +00:00
|
|
|
this.props.updateWidgetMetaProperty("isSwitchedOn", isSwitchedOn, {
|
2021-04-23 13:50:55 +00:00
|
|
|
triggerPropertyName: "onChange",
|
2021-02-16 12:15:17 +00:00
|
|
|
dynamicString: this.props.onChange,
|
|
|
|
|
event: {
|
|
|
|
|
type: EventType.ON_SWITCH_CHANGE,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-09 15:10:22 +00:00
|
|
|
export interface SwitchWidgetProps extends WidgetProps {
|
2021-02-16 12:15:17 +00:00
|
|
|
isSwitchedOn: boolean;
|
|
|
|
|
defaultSwitchState: boolean;
|
2022-05-25 11:33:33 +00:00
|
|
|
alignWidget: AlignWidgetTypes;
|
|
|
|
|
labelPosition: LabelPosition;
|
2021-02-16 12:15:17 +00:00
|
|
|
label: string;
|
2022-05-04 09:45:57 +00:00
|
|
|
accentColor: string;
|
2022-03-01 19:02:10 +00:00
|
|
|
isDirty: boolean;
|
2022-05-25 11:33:33 +00:00
|
|
|
labelTextColor?: string;
|
|
|
|
|
labelTextSize?: string;
|
|
|
|
|
labelStyle?: string;
|
2021-02-16 12:15:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default SwitchWidget;
|