import React from "react"; import BaseWidget, { WidgetProps, WidgetState } from "../../BaseWidget"; 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 { LabelPosition } from "components/constants"; import { AlignWidgetTypes } from "widgets/constants"; import { Stylesheet } from "entities/AppTheming"; import { isAutoHeightEnabledForWidget } from "widgets/WidgetUtils"; class CheckboxWidget extends BaseWidget { 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", controlType: "ICON_TABS", fullWidth: true, options: [ { label: "Left", value: LabelPosition.Left }, { label: "Right", value: LabelPosition.Right }, ], defaultValue: LabelPosition.Left, 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", helpText: "Control the color of the label associated", controlType: "COLOR_PICKER", isJSConvertible: true, isBindProperty: true, isTriggerProperty: false, validation: { type: ValidationTypes.TEXT, params: { regex: /^(?![<|{{]).+/, }, }, }, { propertyName: "labelTextSize", label: "Font Size", helpText: "Control the font size of the label associated", 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", helpText: "Control if the label should be bold or italics", controlType: "BUTTON_GROUP", 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 }, }, ], }, ]; } static getDefaultPropertiesMap(): Record { return { isChecked: "defaultCheckedState", }; } static getDerivedPropertiesMap(): DerivedPropertiesMap { return { value: `{{!!this.isChecked}}`, isValid: `{{ this.isRequired ? !!this.isChecked : true }}`, }; } static getMetaPropertiesMap(): Record { return { isChecked: undefined, isDirty: false, }; } static getStylesheetConfig(): Stylesheet { return { accentColor: "{{appsmith.theme.colors.primaryColor}}", borderRadius: "{{appsmith.theme.borderRadius.appBorderRadius}}", }; } componentDidUpdate(prevProps: CheckboxWidgetProps) { if ( this.props.defaultCheckedState !== prevProps.defaultCheckedState && this.props.isDirty ) { this.props.updateWidgetMetaProperty("isDirty", false); } } getPageView() { return ( ); } onCheckChange = (isChecked: boolean) => { if (!this.props.isDirty) { this.props.updateWidgetMetaProperty("isDirty", true); } this.props.updateWidgetMetaProperty("isChecked", isChecked, { triggerPropertyName: "onCheckChange", dynamicString: this.props.onCheckChange, event: { type: EventType.ON_CHECK_CHANGE, }, }); }; static getWidgetType(): WidgetType { return "CHECKBOX_WIDGET"; } } export interface CheckboxWidgetProps extends WidgetProps { label: string; defaultCheckedState: boolean; isChecked?: boolean; isDisabled?: boolean; onCheckChange?: string; isRequired?: boolean; accentColor: string; borderRadius: string; alignWidget: AlignWidgetTypes; labelPosition: LabelPosition; labelTextColor?: string; labelTextSize?: string; labelStyle?: string; } export default CheckboxWidget;