-- Implement dirty check logic for a form -- Expose an form property, hasChanges for checking if the user has changed any values in the form -- Add isDirty derived property for the following widgets: AudioRecorderWidget, CameraWidget, CheckboxGroupWidget, CheckboxWidget, CurrencyInputWidget, DatePickerWidget2, FilePickerWidgetV2, InputWidgetV2, MultiSelectTreeWidget, MultiSelectWidgetV2, PhoneInputWidget, RadioGroupWidget, RichTextEditorWidget, SelectWidget, SingleSelectTreeWidget, SwitchGroupWidget, SwitchWidget
163 lines
4.8 KiB
TypeScript
163 lines
4.8 KiB
TypeScript
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<SwitchWidgetProps, WidgetState> {
|
|
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,
|
|
},
|
|
],
|
|
},
|
|
];
|
|
}
|
|
getPageView() {
|
|
return (
|
|
<SwitchComponent
|
|
alignWidget={this.props.alignWidget ? this.props.alignWidget : "LEFT"}
|
|
isDisabled={this.props.isDisabled}
|
|
isLoading={this.props.isLoading}
|
|
isSwitchedOn={!!this.props.isSwitchedOn}
|
|
key={this.props.widgetId}
|
|
label={this.props.label}
|
|
onChange={this.onChange}
|
|
widgetId={this.props.widgetId}
|
|
/>
|
|
);
|
|
}
|
|
|
|
static getWidgetType(): WidgetType {
|
|
return "SWITCH_WIDGET";
|
|
}
|
|
|
|
static getDefaultPropertiesMap(): Record<string, string> {
|
|
return {
|
|
isSwitchedOn: "defaultSwitchState",
|
|
};
|
|
}
|
|
|
|
static getMetaPropertiesMap(): Record<string, any> {
|
|
return {
|
|
isSwitchedOn: undefined,
|
|
};
|
|
}
|
|
|
|
static getDerivedPropertiesMap(): DerivedPropertiesMap {
|
|
return {
|
|
value: `{{!!this.isSwitchedOn}}`,
|
|
isDirty: `{{ this.isSwitchedOn !== this.defaultSwitchState }}`,
|
|
};
|
|
}
|
|
|
|
onChange = (isSwitchedOn: boolean) => {
|
|
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;
|
|
}
|
|
|
|
export default SwitchWidget;
|