import React from "react"; import BaseWidget, { WidgetProps, WidgetState } from "../../BaseWidget"; import { WidgetType } from "constants/WidgetConstants"; import { SwitchComponent } from "../component"; import { ValidationTypes } from "constants/WidgetValidation"; import { EventType } from "constants/AppsmithActionConstants/ActionConstants"; import { DerivedPropertiesMap } from "utils/WidgetFactory"; import { AlignWidget } from "widgets/constants"; class SwitchWidget extends BaseWidget { static getPropertyPaneConfig() { return [ { sectionName: "General", children: [ { propertyName: "label", label: "Label", controlType: "INPUT_TEXT", helpText: "Displays a label next to the widget", placeholderText: "Enable Option", isBindProperty: true, isTriggerProperty: false, validation: { type: ValidationTypes.TEXT }, }, { propertyName: "defaultSwitchState", label: "Default Selected", 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 }, }, { propertyName: "alignWidget", helpText: "Sets the alignment of the widget", label: "Alignment", controlType: "DROP_DOWN", isBindProperty: true, isTriggerProperty: false, options: [ { label: "Left", value: "LEFT", }, { label: "Right", value: "RIGHT", }, ], }, ], }, { 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, }, ], }, { sectionName: "Styles", 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 }, }, ], }, ]; } getPageView() { return ( ); } static getWidgetType(): WidgetType { return "SWITCH_WIDGET"; } static getDefaultPropertiesMap(): Record { return { isSwitchedOn: "defaultSwitchState", }; } static getMetaPropertiesMap(): Record { return { isSwitchedOn: undefined, isDirty: false, }; } static getDerivedPropertiesMap(): DerivedPropertiesMap { return { value: `{{!!this.isSwitchedOn}}`, }; } componentDidUpdate(prevProps: SwitchWidgetProps): void { if ( this.props.defaultSwitchState !== prevProps.defaultSwitchState && this.props.isDirty ) { this.props.updateWidgetMetaProperty("isDirty", false); } } onChange = (isSwitchedOn: boolean) => { if (!this.props.isDirty) { this.props.updateWidgetMetaProperty("isDirty", true); } this.props.updateWidgetMetaProperty("isSwitchedOn", isSwitchedOn, { triggerPropertyName: "onChange", dynamicString: this.props.onChange, event: { type: EventType.ON_SWITCH_CHANGE, }, }); }; } export interface SwitchWidgetProps extends WidgetProps { isSwitchedOn: boolean; defaultSwitchState: boolean; alignWidget: AlignWidget; label: string; accentColor: string; isDirty: boolean; } export default SwitchWidget;