PromucFlow_constructor/app/client/src/widgets/DropdownWidget/widget/index.tsx
ChandanBalajiBP b72dea33f3
feat: Error handling phase 1 (#20629)
## Description
This PR updates the error logs 
- Establishing a consistent format for all error messages.
- Revising error titles and details for improved understanding.
- Compiling internal documentation of all error categories,
subcategories, and error descriptions.

Updated Error Interface:
https://www.notion.so/appsmith/Error-Interface-for-Plugin-Execution-Error-7b3f5323ba4c40bfad281ae717ccf79b

PRD:
https://www.notion.so/appsmith/PRD-Error-Handling-Framework-4ac9747057fd4105a9d52cb8b42f4452?pvs=4#008e9c79ff3c484abf0250a5416cf052

>TL;DR 

Fixes # 


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

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


## How Has This Been Tested?

- Manual
- Jest
- Cypress

### Test Plan


### Issues raised during DP testing


## Checklist:
### Dev activity
- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] PR is being merged under a feature flag


### QA activity:
- [ ] Test plan has been approved by relevant developers
- [ ] Test plan has been peer reviewed by QA
- [ ] Cypress test cases have been added and approved by either SDET or
manual QA
- [ ] Organized project review call with relevant stakeholders after
Round 1/2 of QA
- [ ] Added Test Plan Approved label after reveiwing all Cypress test

---------

Co-authored-by: subrata <subrata@appsmith.com>
2023-02-18 18:25:46 +05:30

518 lines
17 KiB
TypeScript

import React from "react";
import BaseWidget, { WidgetProps, WidgetState } from "../../BaseWidget";
import { WidgetType } from "constants/WidgetConstants";
import { EventType } from "constants/AppsmithActionConstants/ActionConstants";
import DropDownComponent from "../component";
import _ from "lodash";
import { DropdownOption } from "../constants";
import {
ValidationResponse,
ValidationTypes,
} from "constants/WidgetValidation";
import { EvaluationSubstitutionType } from "entities/DataTree/dataTreeFactory";
import { AutocompleteDataType } from "utils/autocomplete/CodemirrorTernService";
import { MinimumPopupRows, GRID_DENSITY_MIGRATION_V1 } from "widgets/constants";
import { LabelPosition } from "components/constants";
import { Alignment } from "@blueprintjs/core";
import { Stylesheet } from "entities/AppTheming";
function defaultOptionValueValidation(value: unknown): ValidationResponse {
if (typeof value === "string") return { isValid: true, parsed: value.trim() };
if (value === undefined || value === null)
return {
isValid: false,
parsed: "",
messages: [
{
name: "TypeError",
message: "This value does not evaluate to type: string",
},
],
};
return { isValid: true, parsed: value };
}
class DropdownWidget extends BaseWidget<DropdownWidgetProps, WidgetState> {
static getPropertyPaneConfig() {
return [
{
sectionName: "General",
children: [
{
helpText:
"Allows users to select a single option. 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: {
unique: ["value"],
children: {
type: ValidationTypes.OBJECT,
params: {
required: true,
allowedKeys: [
{
name: "label",
type: ValidationTypes.TEXT,
params: {
default: "",
required: true,
},
},
{
name: "value",
type: ValidationTypes.TEXT,
params: {
default: "",
},
},
],
},
},
},
},
evaluationSubstitutionType:
EvaluationSubstitutionType.SMART_SUBSTITUTE,
},
{
helpText: "Selects the option with value by default",
propertyName: "defaultOptionValue",
label: "Default Option",
controlType: "INPUT_TEXT",
placeholderText: "GREEN",
isBindProperty: true,
isTriggerProperty: false,
validation: {
type: ValidationTypes.FUNCTION,
params: {
fn: defaultOptionValueValidation,
expected: {
type: "value or Array of values",
example: `option1 | ['option1', 'option2']`,
autocompleteDataType: AutocompleteDataType.STRING,
},
},
},
dependencies: ["selectionType"],
},
{
helpText: "Sets a Placeholder Text",
propertyName: "placeholderText",
label: "Placeholder",
controlType: "INPUT_TEXT",
placeholderText: "Enter placeholder text",
isBindProperty: true,
isTriggerProperty: false,
validation: { type: ValidationTypes.TEXT },
},
{
propertyName: "isRequired",
label: "Required",
helpText: "Makes input to the widget mandatory",
controlType: "SWITCH",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: false,
validation: { type: ValidationTypes.BOOLEAN },
},
{
helpText: "Controls the visibility of the widget",
propertyName: "isVisible",
label: "Visible",
controlType: "SWITCH",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: false,
validation: { type: ValidationTypes.BOOLEAN },
},
{
propertyName: "isDisabled",
label: "Disabled",
helpText: "Disables input to this widget",
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 },
},
{
propertyName: "isFilterable",
label: "Filterable",
helpText: "Makes the dropdown list filterable",
controlType: "SWITCH",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: false,
validation: { type: ValidationTypes.BOOLEAN },
},
{
helpText: "Enables server side filtering of the data",
propertyName: "serverSideFiltering",
label: "Server Side Filtering",
controlType: "SWITCH",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: false,
validation: { type: ValidationTypes.BOOLEAN },
},
],
},
{
sectionName: "Label",
children: [
{
helpText: "Sets the label text of the widget",
propertyName: "labelText",
label: "Text",
controlType: "INPUT_TEXT",
placeholderText: "Enter label text",
isBindProperty: true,
isTriggerProperty: false,
validation: { type: ValidationTypes.TEXT },
},
{
helpText: "Sets the label position of the widget",
propertyName: "labelPosition",
label: "Position",
controlType: "DROP_DOWN",
options: [
{ label: "Left", value: LabelPosition.Left },
{ label: "Top", value: LabelPosition.Top },
{ label: "Auto", value: LabelPosition.Auto },
],
defaultValue: LabelPosition.Top,
isBindProperty: false,
isTriggerProperty: false,
validation: { type: ValidationTypes.TEXT },
},
{
helpText: "Sets the label alignment of the widget",
propertyName: "labelAlignment",
label: "Alignment",
controlType: "LABEL_ALIGNMENT_OPTIONS",
options: [
{
icon: "LEFT_ALIGN",
value: Alignment.LEFT,
},
{
icon: "RIGHT_ALIGN",
value: Alignment.RIGHT,
},
],
isBindProperty: false,
isTriggerProperty: false,
validation: { type: ValidationTypes.TEXT },
hidden: (props: DropdownWidgetProps) =>
props.labelPosition !== LabelPosition.Left,
dependencies: ["labelPosition"],
},
{
helpText:
"Sets the label width of the widget as the number of columns",
propertyName: "labelWidth",
label: "Width (in columns)",
controlType: "NUMERIC_INPUT",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: false,
min: 0,
validation: {
type: ValidationTypes.NUMBER,
params: {
natural: true,
},
},
hidden: (props: DropdownWidgetProps) =>
props.labelPosition !== LabelPosition.Left,
dependencies: ["labelPosition"],
},
],
},
{
sectionName: "Styles",
children: [
{
propertyName: "labelTextColor",
label: "Label Text Color",
controlType: "COLOR_PICKER",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: false,
validation: { type: ValidationTypes.TEXT },
},
{
propertyName: "labelTextSize",
label: "Label Text Size",
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: "Label Font Style",
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: "Actions",
children: [
{
helpText: "Triggers an action when a user selects an option",
propertyName: "onOptionChange",
label: "onOptionChange",
controlType: "ACTION_SELECTOR",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: true,
},
{
helpText: "Trigger an action on change of filterText",
hidden: (props: DropdownWidgetProps) => !props.serverSideFiltering,
dependencies: ["serverSideFiltering"],
propertyName: "onFilterUpdate",
label: "onFilterUpdate",
controlType: "ACTION_SELECTOR",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: true,
},
],
},
];
}
static getDerivedPropertiesMap() {
return {
isValid: `{{this.isRequired ? !!this.selectedOptionValue || this.selectedOptionValue === 0 : true}}`,
selectedOptionLabel: `{{(()=>{const index = _.findIndex(this.options, { value: this.value }); return this.options[index]?.label; })()}}`,
selectedOptionValue: `{{(()=>{const index = _.findIndex(this.options, { value: this.value }); return this.options[index]?.value; })()}}`,
};
}
static getDefaultPropertiesMap(): Record<string, string> {
return {
defaultValue: "defaultOptionValue",
value: "defaultOptionValue",
};
}
static getMetaPropertiesMap(): Record<string, any> {
return {
defaultValue: undefined,
value: undefined,
};
}
static getStylesheetConfig(): Stylesheet {
return {
accentColor: "{{appsmith.theme.colors.primaryColor}}",
borderRadius: "{{appsmith.theme.borderRadius.appBorderRadius}}",
boxShadow: "none",
};
}
componentDidMount() {
this.changeSelectedOption();
}
componentDidUpdate(prevProps: DropdownWidgetProps): void {
// removing selectedOptionValue if defaultValueChanges
if (
prevProps.defaultOptionValue !== this.props.defaultOptionValue ||
prevProps.option !== this.props.option
) {
this.changeSelectedOption();
}
}
getPageView() {
const options = _.isArray(this.props.options) ? this.props.options : [];
const isInvalid =
"isValid" in this.props && !this.props.isValid && !!this.props.isDirty;
const dropDownWidth = MinimumPopupRows * this.props.parentColumnSpace;
const selectedIndex = _.findIndex(this.props.options, {
value: this.props.selectedOptionValue,
});
const { componentHeight, componentWidth } = this.getComponentDimensions();
return (
<DropDownComponent
accentColor={this.props.accentColor}
backgroundColor={this.props.backgroundColor}
borderRadius={this.props.borderRadius}
boxShadow={this.props.boxShadow}
compactMode={
!(
(this.props.bottomRow - this.props.topRow) /
GRID_DENSITY_MIGRATION_V1 >
1
)
}
disabled={this.props.isDisabled}
dropDownWidth={dropDownWidth}
hasError={isInvalid}
height={componentHeight}
isFilterable={this.props.isFilterable}
isLoading={this.props.isLoading}
isValid={this.props.isValid}
labelAlignment={this.props.labelAlignment}
labelPosition={this.props.labelPosition}
labelStyle={this.props.labelStyle}
labelText={this.props.labelText}
labelTextColor={this.props.labelTextColor}
labelTextSize={this.props.labelTextSize}
labelWidth={this.getLabelWidth()}
onFilterChange={this.onFilterChange}
onOptionSelected={this.onOptionSelected}
options={options}
placeholder={this.props.placeholderText}
selectedIndex={selectedIndex > -1 ? selectedIndex : undefined}
serverSideFiltering={this.props.serverSideFiltering}
widgetId={this.props.widgetId}
width={componentWidth}
/>
);
}
onOptionSelected = (selectedOption: DropdownOption) => {
let isChanged = true;
if (!this.props.isDirty) {
this.props.updateWidgetMetaProperty("isDirty", true);
}
// Check if the value has changed. If no option
// selected till now, there is a change
if (this.props.selectedOptionValue) {
isChanged = !(this.props.selectedOptionValue === selectedOption.value);
}
if (isChanged) {
this.props.updateWidgetMetaProperty("value", selectedOption.value, {
triggerPropertyName: "onOptionChange",
dynamicString: this.props.onOptionChange as string,
event: {
type: EventType.ON_OPTION_CHANGE,
},
});
}
};
changeSelectedOption = () => {
const index = _.findIndex(this.props.options, {
value: this.props.selectedOptionValue ?? this.props.defaultOptionValue,
});
const value = this.props.options?.[index]?.value;
this.props.updateWidgetMetaProperty("value", value);
};
onFilterChange = (value: string) => {
this.props.updateWidgetMetaProperty("filterText", value);
super.executeAction({
triggerPropertyName: "onFilterUpdate",
dynamicString: this.props.onFilterUpdate,
event: {
type: EventType.ON_FILTER_UPDATE,
},
});
};
static getWidgetType(): WidgetType {
return "DROP_DOWN_WIDGET";
}
}
export interface DropdownWidgetProps extends WidgetProps {
placeholderText?: string;
labelText: string;
labelPosition?: LabelPosition;
labelAlignment?: Alignment;
labelWidth?: number;
selectedIndex?: number;
selectedOption: DropdownOption;
options?: DropdownOption[];
onOptionChange?: string;
defaultOptionValue?: string;
value?: string;
isRequired: boolean;
isFilterable: boolean;
defaultValue: string;
selectedOptionLabel: string;
serverSideFiltering: boolean;
onFilterUpdate: string;
backgroundColor: string;
borderRadius: string;
boxShadow?: string;
accentColor: string;
fontFamily?: string;
isDirty?: boolean;
}
export default DropdownWidget;