PromucFlow_constructor/app/client/src/widgets/CheckboxWidget/widget/index.tsx
Rishabh Rathod b863b39d69
feat: Add linting error for assignment expression (#25140)
## Description


This change adds a linting error for direct mutation of widget property
like `Widget.property = "dsf"` and instead suggests to use setter
methods.


#### PR fixes following issue(s)
Fixes #5822


#### Type of change

- New feature (non-breaking change which adds functionality)

## Testing

- [x] Add jest tests as mentioned in the
[comments](https://www.notion.so/appsmith/Widget-Property-Setters-Tech-Spec-2a34730e2e6d4df8ae7637c363b1096c?pvs=4#276554d9875b42d68868aa969e9d7d03)
of the tech spec document for this project.
- [x] Add test to verify linting error for widget assignment
- [x] Add cypress test for autocomplete of more setter methods 
- [x] Add cypress test for currencyInput setValue 

#### How Has This Been Tested?

- [ ] Manual
- [ ] Jest
- [x] Cypress

#### Test Plan

#### Issues raised during DP testing

## 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
- [x] Cypress test cases have been added and approved by SDET/manual QA
- [x] Added `Test Plan Approved` label after Cypress tests were reviewed
- [ ] Added `Test Plan Approved` label after JUnit tests were reviewed

---------

Co-authored-by: Druthi Polisetty <druthi@appsmith.com>
2023-07-24 12:23:45 +05:30

402 lines
12 KiB
TypeScript

import { LabelPosition } from "components/constants";
import { EventType } from "constants/AppsmithActionConstants/ActionConstants";
import type { WidgetType } from "constants/WidgetConstants";
import { ValidationTypes } from "constants/WidgetValidation";
import type { SetterConfig, Stylesheet } from "entities/AppTheming";
import React from "react";
import { isAutoLayout } from "utils/autoLayout/flexWidgetUtils";
import type { DerivedPropertiesMap } from "utils/WidgetFactory";
import { AlignWidgetTypes } from "widgets/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 "widgets/constants";
class CheckboxWidget extends BaseWidget<CheckboxWidgetProps, WidgetState> {
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",
},
},
};
}
getPageView() {
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,
},
});
};
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;