PromucFlow_constructor/app/client/src/widgets/CheckboxWidget/widget/index.tsx
Ashok Kumar M 0cbec4283c
chore: BaseWidget Restructuring (#26562)
## Description

Create a Basewidget wrapper that supplies Widget Onion as per the layout
system.

involves extracting widget layers presently in the BaseWidget into HOCs
and hooks and make sure layout systems can be scaled.
Make sure Modal widget is handled as a overlay widget whose wrappers are
supplied by basewidget instead of modal widget implementing its own
editing blocks.
This PR also separates the drag n drop logic for both auto layout and
fixed layout. They are moved into respective Layout system folders to
have clear sepsration of concern

#### PR fixes following issue(s)
Fixes #26674
Fixes #26675
Fixes #26676
Fixes #26570
Fixes #26590 
Fixes #26591 
Fixes #26592 
<img width="931" alt="BaseWidgetHOC"
src="https://github.com/appsmithorg/appsmith/assets/35134347/22f4cf1e-e4c5-4475-83a8-6818e7cebe70">

[Miro Link to view the new
system](https://miro.com/app/board/uXjVM6vRgf8=/?moveToWidget=3458764560239189204&cot=14)


> if no issue exists, please create an issue and ask the maintainers
about this first
>
>
#### Media
> A video or a GIF is preferred. when using Loom, don’t embed because it
looks like it’s a GIF. instead, just link to the video
>
>
#### Type of change
> Please delete options that are not relevant.
- Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- Chore (housekeeping or task changes that don't impact user perception)
>
>
>
## Testing
>
#### How Has This Been Tested?
> Please describe the tests that you ran to verify your changes. Also
list any relevant details for your test configuration.
> Delete anything that is not relevant
- [x] Manual
- [ ] JUnit
- [ ] Jest
- [ ] Cypress
>
>
#### Test Plan
> Add Testsmith test cases links that relate to this PR
>
>
#### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)
>
>
>
## Checklist:
#### Dev activity
- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag


#### QA activity:
- [ ] [Speedbreak
features](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#speedbreakers-)
have been covered
- [ ] Test plan covers all impacted features and [areas of
interest](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#areas-of-interest-)
- [ ] Test plan has been peer reviewed by project stakeholders and other
QA members
- [ ] Manually tested functionality on DP
- [ ] We had an implementation alignment call with stakeholders post QA
Round 2
- [ ] Cypress test cases have been added and approved by SDET/manual QA
- [ ] Added `Test Plan Approved` label after Cypress tests were reviewed
- [ ] Added `Test Plan Approved` label after JUnit tests were reviewed

---------

Co-authored-by: rahulramesha <rahul@appsmith.com>
Co-authored-by: rahulramesha <71900764+rahulramesha@users.noreply.github.com>
Co-authored-by: Preet Sidhu <preetsidhu.bits@gmail.com>
Co-authored-by: Aswath K <aswath.sana@gmail.com>
2023-09-11 21:25:11 +05:30

483 lines
14 KiB
TypeScript

import { LabelPosition } from "components/constants";
import { EventType } from "constants/AppsmithActionConstants/ActionConstants";
import { ValidationTypes } from "constants/WidgetValidation";
import type { SetterConfig, Stylesheet } from "entities/AppTheming";
import React from "react";
import { isAutoLayout } from "layoutSystems/autolayout/utils/flexWidgetUtils";
import type { DerivedPropertiesMap } from "WidgetProvider/factory";
import { AlignWidgetTypes } from "WidgetProvider/constants";
import {
isAutoHeightEnabledForWidget,
DefaultAutocompleteDefinitions,
} from "widgets/WidgetUtils";
import type { WidgetProps, WidgetState } from "../../BaseWidget";
import BaseWidget from "../../BaseWidget";
import CheckboxComponent from "../component";
import type { AutocompletionDefinitions } from "WidgetProvider/constants";
import { FILL_WIDGET_MIN_WIDTH } from "constants/minWidthConstants";
import { ResponsiveBehavior } from "layoutSystems/autolayout/utils/constants";
import type {
SnipingModeProperty,
PropertyUpdates,
} from "WidgetProvider/constants";
import IconSVG from "../icon.svg";
import { WIDGET_TAGS } from "constants/WidgetConstants";
class CheckboxWidget extends BaseWidget<CheckboxWidgetProps, WidgetState> {
static type = "CHECKBOX_WIDGET";
static getConfig() {
return {
name: "Checkbox",
iconSVG: IconSVG,
tags: [WIDGET_TAGS.TOGGLES],
needsMeta: true,
searchTags: ["boolean"],
};
}
static getFeatures() {
return {
dynamicHeight: {
sectionIndex: 2,
active: true,
},
};
}
static getDefaults() {
return {
rows: 4,
columns: 12,
label: "Label",
defaultCheckedState: true,
widgetName: "Checkbox",
version: 1,
alignWidget: AlignWidgetTypes.LEFT,
labelPosition: LabelPosition.Left,
isDisabled: false,
isRequired: false,
animateLoading: true,
responsiveBehavior: ResponsiveBehavior.Fill,
minWidth: FILL_WIDGET_MIN_WIDTH,
};
}
static getMethods() {
return {
getSnipingModeUpdates: (
propValueMap: SnipingModeProperty,
): PropertyUpdates[] => {
return [
{
propertyPath: "defaultCheckedState",
propertyValue: propValueMap.data,
isDynamicPropertyPath: true,
},
];
},
};
}
static getAutoLayoutConfig() {
return {
disabledPropsDefaults: {
labelTextSize: "0.875rem",
},
widgetSize: [
{
viewportMinWidth: 0,
configuration: () => {
return {
minWidth: "120px",
minHeight: "40px",
};
},
},
],
disableResizeHandles: {
vertical: true,
},
};
}
static getAutocompleteDefinitions(): AutocompletionDefinitions {
return {
"!doc":
"Checkbox is a simple UI widget you can use when you want users to make a binary choice",
"!url": "https://docs.appsmith.com/widget-reference/checkbox",
isVisible: DefaultAutocompleteDefinitions.isVisible,
isChecked: "bool",
isDisabled: "bool",
};
}
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,
fullWidth: false,
options: [
{
startIcon: "align-left",
value: AlignWidgetTypes.LEFT,
},
{
startIcon: "align-right",
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: "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",
hidden: isAutoLayout,
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: "text-bold",
value: "BOLD",
},
{
icon: "text-italic",
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<string, string> {
return {
isChecked: "defaultCheckedState",
};
}
static getDerivedPropertiesMap(): DerivedPropertiesMap {
return {
value: `{{!!this.isChecked}}`,
isValid: `{{ this.isRequired ? !!this.isChecked : true }}`,
};
}
static getMetaPropertiesMap(): Record<string, any> {
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);
}
}
static getSetterConfig(): SetterConfig {
return {
__setters: {
setVisibility: {
path: "isVisible",
type: "boolean",
},
setDisabled: {
path: "isDisabled",
type: "boolean",
},
setRequired: {
path: "isRequired",
type: "boolean",
},
setValue: {
path: "defaultCheckedState",
type: "boolean",
accessor: "isChecked",
},
},
};
}
getWidgetView() {
return (
<CheckboxComponent
accentColor={this.props.accentColor}
alignWidget={this.props.alignWidget}
borderRadius={this.props.borderRadius}
isChecked={!!this.props.isChecked}
isDisabled={this.props.isDisabled}
isDynamicHeightEnabled={isAutoHeightEnabledForWidget(this.props)}
isLabelInline={this.isAutoLayoutMode}
isLoading={this.props.isLoading}
isRequired={this.props.isRequired}
key={this.props.widgetId}
label={this.props.label}
labelPosition={this.props.labelPosition}
labelStyle={this.props.labelStyle}
labelTextColor={this.props.labelTextColor}
labelTextSize={this.props.labelTextSize}
minHeight={this.props.minHeight}
onCheckChange={this.onCheckChange}
widgetId={this.props.widgetId}
/>
);
}
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,
},
});
};
}
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;