2019-09-09 09:08:54 +00:00
|
|
|
import React from "react";
|
2021-09-09 15:10:22 +00:00
|
|
|
import BaseWidget, { WidgetProps, WidgetState } from "../../BaseWidget";
|
2019-11-25 05:07:27 +00:00
|
|
|
import { WidgetType } from "constants/WidgetConstants";
|
2021-09-09 15:10:22 +00:00
|
|
|
import CheckboxComponent from "../component";
|
2021-03-30 05:29:03 +00:00
|
|
|
import { EventType } from "constants/AppsmithActionConstants/ActionConstants";
|
2021-07-26 05:50:46 +00:00
|
|
|
import { ValidationTypes } from "constants/WidgetValidation";
|
2021-02-25 14:00:02 +00:00
|
|
|
import { DerivedPropertiesMap } from "utils/WidgetFactory";
|
2021-09-09 15:10:22 +00:00
|
|
|
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> {
|
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",
|
2021-09-20 10:43:44 +00:00
|
|
|
placeholderText: "I agree to the T&C",
|
2021-02-16 10:29:08 +00:00
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
2021-07-26 05:50:46 +00:00
|
|
|
validation: { type: ValidationTypes.TEXT },
|
2021-02-16 10:29:08 +00:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
propertyName: "defaultCheckedState",
|
|
|
|
|
label: "Default Selected",
|
2021-09-20 10:43:44 +00:00
|
|
|
helpText: "Sets the default checked state of the widget",
|
2021-02-16 10:29:08 +00:00
|
|
|
controlType: "SWITCH",
|
|
|
|
|
isJSConvertible: true,
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
2021-07-26 05:50:46 +00:00
|
|
|
validation: { type: ValidationTypes.BOOLEAN },
|
2021-02-16 10:29:08 +00:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
propertyName: "isRequired",
|
|
|
|
|
label: "Required",
|
|
|
|
|
helpText: "Makes input to the widget mandatory",
|
|
|
|
|
controlType: "SWITCH",
|
|
|
|
|
isJSConvertible: true,
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
2021-07-26 05:50:46 +00:00
|
|
|
validation: { type: ValidationTypes.BOOLEAN },
|
2021-02-16 10:29:08 +00:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
propertyName: "isVisible",
|
|
|
|
|
label: "Visible",
|
|
|
|
|
helpText: "Controls the visibility of the widget",
|
|
|
|
|
controlType: "SWITCH",
|
|
|
|
|
isJSConvertible: true,
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
2021-07-26 05:50:46 +00:00
|
|
|
validation: { type: ValidationTypes.BOOLEAN },
|
2021-02-16 10:29:08 +00:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
propertyName: "isDisabled",
|
|
|
|
|
label: "Disabled",
|
|
|
|
|
controlType: "SWITCH",
|
|
|
|
|
helpText: "Disables input to this widget",
|
|
|
|
|
isJSConvertible: true,
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
2021-07-26 05:50:46 +00:00
|
|
|
validation: { type: ValidationTypes.BOOLEAN },
|
2021-02-16 10:29:08 +00:00
|
|
|
},
|
2021-12-14 07:55:58 +00:00
|
|
|
{
|
|
|
|
|
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 },
|
|
|
|
|
},
|
2021-09-20 10:43:44 +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
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{
|
2021-11-26 09:32:04 +00:00
|
|
|
sectionName: "Events",
|
2021-02-16 10:29:08 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
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 }}`,
|
feat: Internal property to detect changes in a form
-- 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
2022-02-23 08:03:51 +00:00
|
|
|
isDirty: `{{ this.isChecked !== this.defaultCheckedState }}`,
|
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
|
|
|
}
|
|
|
|
|
|
feat: Internal property to detect changes in a form
-- 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
2022-02-23 08:03:51 +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
|
2021-02-16 12:15:17 +00:00
|
|
|
alignWidget={this.props.alignWidget}
|
2021-04-28 10:28:39 +00:00
|
|
|
isChecked={!!this.props.isChecked}
|
2019-10-31 09:04:19 +00:00
|
|
|
isDisabled={this.props.isDisabled}
|
2019-12-03 04:41:10 +00:00
|
|
|
isLoading={this.props.isLoading}
|
2021-04-28 10:28:39 +00:00
|
|
|
isRequired={this.props.isRequired}
|
|
|
|
|
key={this.props.widgetId}
|
|
|
|
|
label={this.props.label}
|
|
|
|
|
onCheckChange={this.onCheckChange}
|
2021-09-08 06:15:11 +00:00
|
|
|
rowSpace={this.props.parentRowSpace}
|
2021-04-28 10:28:39 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2019-10-31 09:04:19 +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,
|
|
|
|
|
},
|
|
|
|
|
});
|
2019-10-31 09:04:19 +00:00
|
|
|
};
|
|
|
|
|
|
2021-09-09 15:10:22 +00:00
|
|
|
static getWidgetType(): WidgetType {
|
2019-09-13 10:45:49 +00:00
|
|
|
return "CHECKBOX_WIDGET";
|
2019-03-21 12:10:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-09 15:10:22 +00:00
|
|
|
export interface CheckboxWidgetProps extends WidgetProps {
|
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;
|