PromucFlow_constructor/app/client/src/widgets/SwitchGroupWidget/widget/index.tsx

266 lines
7.8 KiB
TypeScript
Raw Normal View History

feat: Switch Group widget (#7590) * Feat: Switch Group Widget -- The first MVP of the widget * feat: Switch Group Widget -- Follow the same config and implementation as the other group widgets * feat: Switch Group Widget -- Elaborate the help text for defaultSelectedValues * feat: Switch Group Widget -- Add a widget icon * feat: Switch Group Widget -- Remove unnecessary property control at ItemsControl.tsx -- Refactor some code snippets for higher code quality -- Add basic cypress test cases * fix: icon for the widget optimised & replaced * feat: Switch Group Widget -- Add a unit test for defaultSelectedValuesValidation * feat: Switch Group Widget -- Make the validation type for defaultSelectedValues property to ValidationTypes.ARRAY -- Remove original validation function and its unit test * feat: Switch Group Widget -- Fix on typo -- Fix on formatting issue -- Change the help text for isRequired property * feat: Switch Group Widget -- Revert help text for isRequired property to the original one * feat: Switch Group Widget -- Set strict property to true on defaultSelectedValues * feat: Switch group widget -- Refactor utility function, getCamelCaseString -- Add the corresponding test case * feat: Switch group widget -- Implement options property as a plain JS field -- Reimplement update logic for selectedValues when options changes -- Add a new utility function for checking equality of object arrays -- Add a unit test for the above function -- Rewrite the corresponding Cypress test cases * feat: Switch Group Widget -- Remove isArrayEqual utility function and directly use functions from lodash * feat: Swtich Group Widget -- Make selectedValues as a derived property * feat: Switch Group Widget -- Replace the widget icon * feat: Switch Group Widget -- Rewrite a test case for onSelectionChange property * feat: Switch Group Widget -- Remove redundant calls for openPropertyPane * feat: Switch Group Widget -- Remove closePropertyPane call from afterEach hook * feat: Switch Group Widget -- Change the selector for every switch element in onSelectionChange test case * feat: Switch Group Widget -- Fix on failed Cypress test case, adding closePropertyPane command to onSelectionChange * feat: Switch Group Widget -- Remove template literal from a selector * feat: Switch Group Widget -- Make click on onSelectionChange test case forced * feat: Switch Group Widget -- Fix on crash issue when editing on Options property * feat: Switch Group Widget -- Add the widget icon to show in entity explorer * feat: Switch Group Widget -- Fix on blue color on mouse down -- Add a new property for alignment Co-authored-by: somangshu <somangshu.goswami1508@gmail.com>
2021-12-09 12:02:47 +00:00
import React from "react";
import { Alignment } from "@blueprintjs/core";
import BaseWidget, { WidgetProps, WidgetState } from "widgets/BaseWidget";
import { DerivedPropertiesMap } from "utils/WidgetFactory";
import { ValidationTypes } from "constants/WidgetValidation";
import { EventType } from "constants/AppsmithActionConstants/ActionConstants";
import { EvaluationSubstitutionType } from "entities/DataTree/dataTreeFactory";
import SwitchGroupComponent, { OptionProps } from "../component";
class SwitchGroupWidget extends BaseWidget<
SwitchGroupWidgetProps,
WidgetState
> {
static getPropertyPaneConfig() {
return [
{
sectionName: "General",
children: [
{
helpText:
"Displays a list of options for a user to select. Values must be unique",
propertyName: "options",
label: "Options",
controlType: "INPUT_TEXT",
placeholderText: '[{ "label": "Option1", "value": "Option2" }]',
isBindProperty: true,
isTriggerProperty: false,
validation: {
type: ValidationTypes.ARRAY,
params: {
children: {
type: ValidationTypes.OBJECT,
params: {
allowedKeys: [
{
name: "label",
type: ValidationTypes.TEXT,
params: {
default: "",
required: true,
unique: true,
},
},
{
name: "value",
type: ValidationTypes.TEXT,
params: {
default: "",
required: true,
unique: true,
},
},
],
},
},
},
},
evaluationSubstitutionType:
EvaluationSubstitutionType.SMART_SUBSTITUTE,
},
{
helpText:
"Selects values of the options checked by default. Enter comma separated values for multiple selected",
propertyName: "defaultSelectedValues",
label: "Default Selected Values",
placeholderText: "Enter option values",
controlType: "INPUT_TEXT",
isBindProperty: true,
isTriggerProperty: false,
validation: {
type: ValidationTypes.ARRAY,
params: {
default: [],
children: {
type: ValidationTypes.TEXT,
},
strict: true,
},
},
},
{
propertyName: "isInline",
helpText:
"Whether switches are to be displayed inline horizontally",
label: "Inline",
controlType: "SWITCH",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: false,
validation: { type: ValidationTypes.BOOLEAN },
},
{
propertyName: "isRequired",
label: "Required",
helpText: "Makes input to the widget mandatory",
controlType: "SWITCH",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: false,
validation: { type: ValidationTypes.BOOLEAN },
},
{
propertyName: "isVisible",
helpText: "Controls the visibility of the widget",
label: "Visible",
controlType: "SWITCH",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: false,
validation: { type: ValidationTypes.BOOLEAN },
},
{
propertyName: "isDisabled",
helpText: "Disables input to the widget",
label: "Disabled",
controlType: "SWITCH",
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 },
},
feat: Switch Group widget (#7590) * Feat: Switch Group Widget -- The first MVP of the widget * feat: Switch Group Widget -- Follow the same config and implementation as the other group widgets * feat: Switch Group Widget -- Elaborate the help text for defaultSelectedValues * feat: Switch Group Widget -- Add a widget icon * feat: Switch Group Widget -- Remove unnecessary property control at ItemsControl.tsx -- Refactor some code snippets for higher code quality -- Add basic cypress test cases * fix: icon for the widget optimised & replaced * feat: Switch Group Widget -- Add a unit test for defaultSelectedValuesValidation * feat: Switch Group Widget -- Make the validation type for defaultSelectedValues property to ValidationTypes.ARRAY -- Remove original validation function and its unit test * feat: Switch Group Widget -- Fix on typo -- Fix on formatting issue -- Change the help text for isRequired property * feat: Switch Group Widget -- Revert help text for isRequired property to the original one * feat: Switch Group Widget -- Set strict property to true on defaultSelectedValues * feat: Switch group widget -- Refactor utility function, getCamelCaseString -- Add the corresponding test case * feat: Switch group widget -- Implement options property as a plain JS field -- Reimplement update logic for selectedValues when options changes -- Add a new utility function for checking equality of object arrays -- Add a unit test for the above function -- Rewrite the corresponding Cypress test cases * feat: Switch Group Widget -- Remove isArrayEqual utility function and directly use functions from lodash * feat: Swtich Group Widget -- Make selectedValues as a derived property * feat: Switch Group Widget -- Replace the widget icon * feat: Switch Group Widget -- Rewrite a test case for onSelectionChange property * feat: Switch Group Widget -- Remove redundant calls for openPropertyPane * feat: Switch Group Widget -- Remove closePropertyPane call from afterEach hook * feat: Switch Group Widget -- Change the selector for every switch element in onSelectionChange test case * feat: Switch Group Widget -- Fix on failed Cypress test case, adding closePropertyPane command to onSelectionChange * feat: Switch Group Widget -- Remove template literal from a selector * feat: Switch Group Widget -- Make click on onSelectionChange test case forced * feat: Switch Group Widget -- Fix on crash issue when editing on Options property * feat: Switch Group Widget -- Add the widget icon to show in entity explorer * feat: Switch Group Widget -- Fix on blue color on mouse down -- Add a new property for alignment Co-authored-by: somangshu <somangshu.goswami1508@gmail.com>
2021-12-09 12:02:47 +00:00
{
propertyName: "alignment",
helpText: "Sets the alignment of the widget",
label: "Alignment",
controlType: "DROP_DOWN",
isBindProperty: true,
isTriggerProperty: false,
options: [
{
label: "Left",
value: Alignment.LEFT,
},
{
label: "Right",
value: Alignment.RIGHT,
},
],
},
],
},
{
sectionName: "Actions",
children: [
{
helpText:
"Triggers an action when a switch state inside the group is changed",
propertyName: "onSelectionChange",
label: "onSelectionChange",
controlType: "ACTION_SELECTOR",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: true,
},
],
},
];
}
static getDefaultPropertiesMap(): Record<string, string> {
return {
selectedValuesArray: "defaultSelectedValues",
};
}
static getMetaPropertiesMap(): Record<string, any> {
return {
selectedValuesArray: undefined,
};
}
static getDerivedPropertiesMap(): DerivedPropertiesMap {
return {
isValid: `{{ this.isRequired ? !!this.selectedValues.length : true }}`,
selectedValues: `{{
this.selectedValuesArray.filter(
selectedValue => this.options.map(option => option.value).includes(selectedValue)
)
}}`,
};
}
static getWidgetType(): string {
return "SWITCH_GROUP_WIDGET";
}
getPageView() {
const {
alignment,
isDisabled,
isInline,
isRequired,
isValid,
options,
parentRowSpace,
selectedValues,
} = this.props;
return (
<SwitchGroupComponent
alignment={alignment}
disabled={isDisabled}
inline={isInline}
onChange={this.handleSwitchStateChange}
options={options}
required={isRequired}
rowSpace={parentRowSpace}
selected={selectedValues}
valid={isValid}
/>
);
}
private handleSwitchStateChange = (value: string) => {
return (event: React.FormEvent<HTMLElement>) => {
let { selectedValuesArray } = this.props;
const isChecked = (event.target as HTMLInputElement).checked;
if (isChecked) {
selectedValuesArray = [...selectedValuesArray, value];
} else {
selectedValuesArray = selectedValuesArray.filter(
(item: string) => item !== value,
);
}
this.props.updateWidgetMetaProperty(
"selectedValuesArray",
selectedValuesArray,
{
triggerPropertyName: "onSelectionChange",
dynamicString: this.props.onSelectionChange,
event: {
type: EventType.ON_SWITCH_GROUP_SELECTION_CHANGE,
},
},
);
};
};
}
export interface SwitchGroupWidgetProps extends WidgetProps {
options: OptionProps[];
defaultSelectedValues: string[];
isInline: boolean;
isRequired?: boolean;
isValid?: boolean;
isDisabled?: boolean;
alignment: Alignment;
onSelectionChange?: boolean;
}
export default SwitchGroupWidget;