PromucFlow_constructor/app/client/src/widgets/DropdownWidget.tsx

259 lines
8.3 KiB
TypeScript
Raw Normal View History

2019-09-12 08:11:25 +00:00
import React from "react";
import BaseWidget, { WidgetProps, WidgetState } from "./BaseWidget";
2020-08-19 04:37:20 +00:00
import { WidgetType } from "constants/WidgetConstants";
import { EventType } from "constants/AppsmithActionConstants/ActionConstants";
2019-11-25 05:07:27 +00:00
import DropDownComponent from "components/designSystems/blueprint/DropdownComponent";
2019-10-31 05:28:11 +00:00
import _ from "lodash";
import {
ValidationResponse,
ValidationTypes,
} from "constants/WidgetValidation";
2020-04-22 09:15:24 +00:00
import { Intent as BlueprintIntent } from "@blueprintjs/core";
import * as Sentry from "@sentry/react";
2020-10-06 09:01:51 +00:00
import withMeta, { WithMeta } from "./MetaHOC";
import { IconName } from "@blueprintjs/icons";
import { EvaluationSubstitutionType } from "entities/DataTree/dataTreeFactory";
import { AutocompleteDataType } from "utils/autocomplete/TernServer";
2019-09-12 08:11:25 +00:00
function defaultOptionValueValidation(value: unknown): ValidationResponse {
if (typeof value === "string") return { isValid: true, parsed: value.trim() };
if (value === undefined || value === null)
return {
isValid: false,
parsed: "",
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: 'Enter [{"label": "label1", "value": "value2"}]',
isBindProperty: true,
isTriggerProperty: false,
validation: {
type: ValidationTypes.ARRAY,
params: {
unique: ["value"],
children: {
type: ValidationTypes.OBJECT,
params: {
allowedKeys: [
{
name: "label",
type: ValidationTypes.TEXT,
params: {
default: "",
required: true,
},
},
{
name: "value",
type: ValidationTypes.TEXT,
params: {
default: "",
required: true,
},
},
],
},
},
},
},
evaluationSubstitutionType:
EvaluationSubstitutionType.SMART_SUBSTITUTE,
},
{
helpText: "Selects the option with value by default",
propertyName: "defaultOptionValue",
label: "Default Option",
controlType: "INPUT_TEXT",
placeholderText: "Enter option value",
isBindProperty: true,
isTriggerProperty: false,
validation: {
type: ValidationTypes.FUNCTION,
params: {
fn: defaultOptionValueValidation,
expected: {
type: "value or Array of values",
example: `value1 | ['value1', 'value2']`,
autocompleteDataType: AutocompleteDataType.STRING,
},
},
},
dependencies: ["selectionType"],
},
{
propertyName: "isFilterable",
label: "Filterable",
helpText: "Makes the dropdown list filterable",
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 },
},
{
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 },
},
],
},
{
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,
},
],
},
];
}
2020-01-17 09:28:26 +00:00
static getDerivedPropertiesMap() {
return {
isValid: `{{this.isRequired ? !!this.selectedOption : true}}`,
selectedOption: `{{ _.find(this.options, { value: this.defaultValue }) }}`,
2020-04-17 16:15:09 +00:00
selectedIndex: `{{ _.findIndex(this.options, { value: this.selectedOption.value } ) }}`,
value: `{{ this.defaultValue }}`,
selectedOptionLabel: `{{(()=>{const index = _.findIndex(this.options, { value: this.defaultValue }); return this.options[index]?.label; })()}}`,
selectedOptionValue: `{{(()=>{const index = _.findIndex(this.options, { value: this.defaultValue }); return this.options[index]?.value; })()}}`,
2020-01-17 09:28:26 +00:00
};
}
2020-02-11 11:03:10 +00:00
2020-04-17 16:15:09 +00:00
static getDefaultPropertiesMap(): Record<string, string> {
return {
defaultValue: "defaultOptionValue",
2020-04-17 16:15:09 +00:00
};
2020-03-13 07:24:03 +00:00
}
2020-04-17 16:15:09 +00:00
static getMetaPropertiesMap(): Record<string, any> {
return {
defaultValue: undefined,
2020-04-17 16:15:09 +00:00
};
2020-02-11 11:03:10 +00:00
}
2020-04-17 16:15:09 +00:00
2019-09-12 08:11:25 +00:00
getPageView() {
2021-06-01 04:06:47 +00:00
const options = _.isArray(this.props.options) ? this.props.options : [];
2020-04-17 16:15:09 +00:00
const selectedIndex = _.findIndex(this.props.options, {
value: this.props.defaultValue,
2020-02-11 11:03:10 +00:00
});
const { componentHeight, componentWidth } = this.getComponentDimensions();
2019-10-31 05:28:11 +00:00
return (
<DropDownComponent
disabled={this.props.isDisabled}
height={componentHeight}
2021-05-06 18:04:14 +00:00
isFilterable={this.props.isFilterable}
isLoading={this.props.isLoading}
label={`${this.props.label}`}
onOptionSelected={this.onOptionSelected}
2020-02-11 11:03:10 +00:00
options={options}
placeholder={this.props.placeholderText}
2020-04-17 16:15:09 +00:00
selectedIndex={selectedIndex > -1 ? selectedIndex : undefined}
widgetId={this.props.widgetId}
width={componentWidth}
2019-10-31 05:28:11 +00:00
/>
);
2019-09-12 08:11:25 +00:00
}
2019-10-31 05:28:11 +00:00
onOptionSelected = (selectedOption: DropdownOption) => {
let isChanged = true;
2020-04-17 16:15:09 +00:00
// Check if the value has changed. If no option
// selected till now, there is a change
if (this.props.selectedOption) {
isChanged = !(this.props.selectedOption.value === selectedOption.value);
}
if (isChanged) {
2020-10-06 09:01:51 +00:00
this.props.updateWidgetMetaProperty(
"defaultValue",
selectedOption.value,
2020-10-06 16:47:16 +00:00
{
2021-04-23 13:50:55 +00:00
triggerPropertyName: "onOptionChange",
2020-10-06 16:47:16 +00:00
dynamicString: this.props.onOptionChange,
event: {
type: EventType.ON_OPTION_CHANGE,
},
2020-02-18 10:41:52 +00:00
},
2020-10-06 16:47:16 +00:00
);
2020-02-18 10:41:52 +00:00
}
2019-10-31 05:28:11 +00:00
};
2019-09-12 08:11:25 +00:00
getWidgetType(): WidgetType {
return "DROP_DOWN_WIDGET";
}
}
export interface DropdownOption {
label: string;
value: string;
icon?: IconName;
subText?: string;
2020-02-18 10:41:52 +00:00
id?: string;
onSelect?: (option: DropdownOption) => void;
children?: DropdownOption[];
2020-04-22 09:15:24 +00:00
intent?: BlueprintIntent;
2019-09-12 08:11:25 +00:00
}
2020-10-06 09:01:51 +00:00
export interface DropdownWidgetProps extends WidgetProps, WithMeta {
placeholderText?: string;
label?: string;
2019-10-31 05:28:11 +00:00
selectedIndex?: number;
2020-04-17 16:15:09 +00:00
selectedOption: DropdownOption;
options?: DropdownOption[];
2020-02-18 10:41:52 +00:00
onOptionChange?: string;
2020-03-31 03:21:35 +00:00
defaultOptionValue?: string | string[];
2020-03-06 09:45:21 +00:00
isRequired: boolean;
isFilterable: boolean;
defaultValue: string;
selectedOptionLabel: string;
2019-09-12 08:11:25 +00:00
}
export default DropdownWidget;
2020-10-06 09:01:51 +00:00
export const ProfiledDropDownWidget = Sentry.withProfiler(
withMeta(DropdownWidget),
);