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";
|
2022-05-25 11:33:33 +00:00
|
|
|
import { LabelPosition } from "components/constants";
|
|
|
|
|
import { AlignWidgetTypes } from "widgets/constants";
|
2022-11-28 04:44:31 +00:00
|
|
|
import { Stylesheet } from "entities/AppTheming";
|
2022-11-23 09:48:23 +00:00
|
|
|
import { isAutoHeightEnabledForWidget } from "widgets/WidgetUtils";
|
2019-03-21 12:10:32 +00:00
|
|
|
|
2019-09-09 09:08:54 +00:00
|
|
|
class CheckboxWidget extends BaseWidget<CheckboxWidgetProps, 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: "I agree to the T&C",
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
|
|
|
|
validation: { type: ValidationTypes.TEXT },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
helpText: "Sets the label position of the widget",
|
|
|
|
|
propertyName: "labelPosition",
|
|
|
|
|
label: "Position",
|
2022-10-20 14:06:32 +00:00
|
|
|
controlType: "ICON_TABS",
|
|
|
|
|
fullWidth: true,
|
2022-08-15 08:05:46 +00:00
|
|
|
options: [
|
|
|
|
|
{ label: "Left", value: LabelPosition.Left },
|
|
|
|
|
{ label: "Right", value: LabelPosition.Right },
|
|
|
|
|
],
|
2022-11-23 09:48:23 +00:00
|
|
|
defaultValue: LabelPosition.Left,
|
2022-08-15 08:05:46 +00:00
|
|
|
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: "Validations",
|
|
|
|
|
children: [
|
|
|
|
|
{
|
|
|
|
|
propertyName: "isRequired",
|
|
|
|
|
label: "Required",
|
|
|
|
|
helpText: "Makes input to the widget mandatory",
|
|
|
|
|
controlType: "SWITCH",
|
|
|
|
|
isJSConvertible: true,
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
|
|
|
|
validation: { type: ValidationTypes.BOOLEAN },
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
sectionName: "General",
|
|
|
|
|
children: [
|
|
|
|
|
{
|
|
|
|
|
propertyName: "defaultCheckedState",
|
|
|
|
|
label: "Default State",
|
|
|
|
|
helpText: "Sets the default checked state of the widget",
|
|
|
|
|
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 check state is changed",
|
|
|
|
|
propertyName: "onCheckChange",
|
|
|
|
|
label: "onCheckChange",
|
|
|
|
|
controlType: "ACTION_SELECTOR",
|
|
|
|
|
isJSConvertible: true,
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: true,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static getPropertyPaneStyleConfig() {
|
|
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
sectionName: "Label Styles",
|
|
|
|
|
children: [
|
|
|
|
|
{
|
|
|
|
|
propertyName: "labelTextColor",
|
|
|
|
|
label: "Font Color",
|
2022-10-20 14:06:32 +00:00
|
|
|
helpText: "Control the color of the label associated",
|
2022-08-15 08:05:46 +00:00
|
|
|
controlType: "COLOR_PICKER",
|
|
|
|
|
isJSConvertible: true,
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
|
|
|
|
validation: {
|
|
|
|
|
type: ValidationTypes.TEXT,
|
|
|
|
|
params: {
|
|
|
|
|
regex: /^(?![<|{{]).+/,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
propertyName: "labelTextSize",
|
|
|
|
|
label: "Font Size",
|
2022-10-20 14:06:32 +00:00
|
|
|
helpText: "Control the font size of the label associated",
|
2022-08-15 08:05:46 +00:00
|
|
|
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",
|
2022-10-20 14:06:32 +00:00
|
|
|
helpText: "Control if the label should be bold or italics",
|
2022-08-15 08:05:46 +00:00
|
|
|
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 checked state color of the checkbox",
|
|
|
|
|
label: "Accent Color",
|
|
|
|
|
controlType: "COLOR_PICKER",
|
|
|
|
|
isJSConvertible: true,
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
|
|
|
|
validation: { type: ValidationTypes.TEXT },
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
sectionName: "Border and Shadow",
|
|
|
|
|
children: [
|
|
|
|
|
{
|
|
|
|
|
propertyName: "borderRadius",
|
|
|
|
|
label: "Border Radius",
|
|
|
|
|
helpText:
|
|
|
|
|
"Rounds the corners of the icon button's outer border edge",
|
|
|
|
|
controlType: "BORDER_RADIUS_OPTIONS",
|
|
|
|
|
isJSConvertible: true,
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
|
|
|
|
validation: { type: ValidationTypes.TEXT },
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
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 }}`,
|
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,
|
2022-03-01 19:02:10 +00:00
|
|
|
isDirty: false,
|
2020-04-17 16:15:09 +00:00
|
|
|
};
|
2020-03-13 07:24:03 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-28 04:44:31 +00:00
|
|
|
static getStylesheetConfig(): Stylesheet {
|
|
|
|
|
return {
|
|
|
|
|
accentColor: "{{appsmith.theme.colors.primaryColor}}",
|
|
|
|
|
borderRadius: "{{appsmith.theme.borderRadius.appBorderRadius}}",
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
2022-03-01 19:02:10 +00:00
|
|
|
if (
|
|
|
|
|
this.props.defaultCheckedState !== prevProps.defaultCheckedState &&
|
|
|
|
|
this.props.isDirty
|
|
|
|
|
) {
|
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
|
|
|
this.props.updateWidgetMetaProperty("isDirty", false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-21 12:10:32 +00:00
|
|
|
getPageView() {
|
|
|
|
|
return (
|
|
|
|
|
<CheckboxComponent
|
2022-05-04 09:45:57 +00:00
|
|
|
accentColor={this.props.accentColor}
|
2021-02-16 12:15:17 +00:00
|
|
|
alignWidget={this.props.alignWidget}
|
2022-05-04 09:45:57 +00:00
|
|
|
borderRadius={this.props.borderRadius}
|
2021-04-28 10:28:39 +00:00
|
|
|
isChecked={!!this.props.isChecked}
|
2019-10-31 09:04:19 +00:00
|
|
|
isDisabled={this.props.isDisabled}
|
2022-11-23 09:48:23 +00:00
|
|
|
isDynamicHeightEnabled={isAutoHeightEnabledForWidget(this.props)}
|
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}
|
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
|
|
|
onCheckChange={this.onCheckChange}
|
|
|
|
|
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) => {
|
2022-03-01 19:02:10 +00:00
|
|
|
if (!this.props.isDirty) {
|
|
|
|
|
this.props.updateWidgetMetaProperty("isDirty", true);
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
2022-05-04 09:45:57 +00:00
|
|
|
accentColor: string;
|
|
|
|
|
borderRadius: string;
|
2022-05-25 11:33:33 +00:00
|
|
|
alignWidget: AlignWidgetTypes;
|
|
|
|
|
labelPosition: LabelPosition;
|
|
|
|
|
labelTextColor?: string;
|
|
|
|
|
labelTextSize?: string;
|
|
|
|
|
labelStyle?: string;
|
2019-03-21 12:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-09 09:08:54 +00:00
|
|
|
export default CheckboxWidget;
|