PromucFlow_constructor/app/client/src/widgets/CheckboxWidget/widget/index.tsx

183 lines
5.5 KiB
TypeScript
Raw Normal View History

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 "../component";
import { EventType } from "constants/AppsmithActionConstants/ActionConstants";
import { ValidationTypes } from "constants/WidgetValidation";
import { DerivedPropertiesMap } from "utils/WidgetFactory";
import { AlignWidget } from "widgets/constants";
2019-03-21 12:10:32 +00:00
2019-09-09 09:08:54 +00:00
class CheckboxWidget extends BaseWidget<CheckboxWidgetProps, WidgetState> {
static getPropertyPaneConfig() {
return [
{
sectionName: "General",
children: [
{
propertyName: "label",
label: "Label",
controlType: "INPUT_TEXT",
helpText: "Displays a label next to the widget",
placeholderText: "I agree to the T&C",
isBindProperty: true,
isTriggerProperty: false,
validation: { type: ValidationTypes.TEXT },
},
{
propertyName: "defaultCheckedState",
label: "Default Selected",
helpText: "Sets the default checked state of the widget",
controlType: "SWITCH",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: false,
validation: { type: ValidationTypes.BOOLEAN },
},
{
propertyName: "isRequired",
label: "Required",
helpText: "Makes input to the widget mandatory",
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",
options: [
{
label: "Left",
value: "LEFT",
},
{
label: "Right",
value: "RIGHT",
},
],
isBindProperty: true,
isTriggerProperty: false,
},
],
},
{
sectionName: "Events",
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
2020-04-17 16:15:09 +00:00
static getDefaultPropertiesMap(): Record<string, string> {
return {
isChecked: "defaultCheckedState",
};
2020-03-13 07:24:03 +00:00
}
static getDerivedPropertiesMap(): DerivedPropertiesMap {
return {
value: `{{!!this.isChecked}}`,
isValid: `{{ this.isRequired ? !!this.isChecked : true }}`,
isDirty: `{{ this.isChecked !== this.defaultCheckedState }}`,
};
}
2020-04-17 16:15:09 +00:00
static getMetaPropertiesMap(): Record<string, any> {
return {
isChecked: undefined,
2020-04-17 16:15:09 +00:00
};
2020-03-13 07:24:03 +00:00
}
componentDidUpdate(prevProps: CheckboxWidgetProps) {
if (this.props.defaultCheckedState !== prevProps.defaultCheckedState) {
this.props.updateWidgetMetaProperty("isDirty", false);
}
}
2019-03-21 12:10:32 +00:00
getPageView() {
return (
<CheckboxComponent
alignWidget={this.props.alignWidget}
isChecked={!!this.props.isChecked}
isDisabled={this.props.isDisabled}
2019-12-03 04:41:10 +00:00
isLoading={this.props.isLoading}
isRequired={this.props.isRequired}
key={this.props.widgetId}
label={this.props.label}
onCheckChange={this.onCheckChange}
rowSpace={this.props.parentRowSpace}
widgetId={this.props.widgetId}
2019-03-21 12:10:32 +00:00
/>
2019-09-09 09:08:54 +00:00
);
2019-03-21 12:10:32 +00:00
}
onCheckChange = (isChecked: boolean) => {
2020-10-06 16:47:16 +00:00
this.props.updateWidgetMetaProperty("isChecked", isChecked, {
2021-04-23 13:50:55 +00:00
triggerPropertyName: "onCheckChange",
2020-10-06 16:47:16 +00:00
dynamicString: this.props.onCheckChange,
event: {
type: EventType.ON_CHECK_CHANGE,
},
});
};
static getWidgetType(): WidgetType {
return "CHECKBOX_WIDGET";
2019-03-21 12:10:32 +00:00
}
}
export interface CheckboxWidgetProps extends WidgetProps {
label: string;
defaultCheckedState: boolean;
isChecked?: boolean;
isDisabled?: boolean;
2020-02-18 10:41:52 +00:00
onCheckChange?: string;
isRequired?: boolean;
alignWidget: AlignWidget;
2019-03-21 12:10:32 +00:00
}
2019-09-09 09:08:54 +00:00
export default CheckboxWidget;