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

276 lines
9.4 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";
2019-11-22 13:12:39 +00:00
import { VALIDATION_TYPES } 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";
2019-09-12 08:11:25 +00:00
class DropdownWidget extends BaseWidget<DropdownWidgetProps, WidgetState> {
static getPropertyPaneConfig() {
return [
{
sectionName: "General",
children: [
{
helpText:
"Allows users to select either a single option or multiple options",
propertyName: "selectionType",
label: "Selection Type",
controlType: "DROP_DOWN",
options: [
{
label: "Single Select",
value: "SINGLE_SELECT",
},
{
label: "Multi Select",
value: "MULTI_SELECT",
},
],
isBindProperty: false,
isTriggerProperty: false,
},
{
helpText:
"Allows users to select either a single option or multiple options. Values must be unique",
propertyName: "options",
label: "Options",
controlType: "INPUT_TEXT",
placeholderText: 'Enter [{label: "label1", value: "value2"}]',
isBindProperty: true,
isTriggerProperty: false,
validation: VALIDATION_TYPES.OPTIONS_DATA,
},
{
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: VALIDATION_TYPES.DEFAULT_OPTION_VALUE,
},
{
propertyName: "isRequired",
label: "Required",
helpText: "Makes input to the widget mandatory",
controlType: "SWITCH",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: false,
validation: VALIDATION_TYPES.BOOLEAN,
},
{
helpText: "Controls the visibility of the widget",
propertyName: "isVisible",
label: "Visible",
controlType: "SWITCH",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: false,
validation: VALIDATION_TYPES.BOOLEAN,
},
{
propertyName: "isDisabled",
label: "Disabled",
helpText: "Disables input to this widget",
controlType: "SWITCH",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: false,
validation: VALIDATION_TYPES.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.selectionType === 'SINGLE_SELECT' ? !!this.selectedOption : !!this.selectedIndexArr && this.selectedIndexArr.length > 0 : true}}`,
2020-04-17 16:15:09 +00:00
selectedOption: `{{ this.selectionType === 'SINGLE_SELECT' ? _.find(this.options, { value: this.selectedOptionValue }) : undefined}}`,
selectedOptionArr: `{{this.selectionType === "MULTI_SELECT" ? this.options.filter(opt => _.includes(this.selectedOptionValueArr, opt.value)) : undefined}}`,
selectedIndex: `{{ _.findIndex(this.options, { value: this.selectedOption.value } ) }}`,
selectedIndexArr: `{{ this.selectedOptionValueArr.map(o => _.findIndex(this.options, { value: o })) }}`,
value: `{{ this.selectionType === 'SINGLE_SELECT' ? this.selectedOptionValue : this.selectedOptionValueArr }}`,
2020-09-10 07:21:44 +00:00
selectedOptionValues: `{{ this.selectedOptionValueArr }}`,
selectedOptionLabels: `{{ this.selectionType === "MULTI_SELECT" ? this.selectedOptionValueArr.map(o => { const index = _.findIndex(this.options, { value: o }); return this.options[index]?.label; }) : [] }}`,
selectedOptionLabel: `{{(()=>{const index = _.findIndex(this.options, { value: this.selectedOptionValue }); return this.selectionType === "SINGLE_SELECT" ? this.options[index]?.label : ""; })()}}`,
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 {
selectedOptionValue: "defaultOptionValue",
selectedOptionValueArr: "defaultOptionValue",
};
2020-03-13 07:24:03 +00:00
}
2020-04-17 16:15:09 +00:00
static getMetaPropertiesMap(): Record<string, any> {
return {
selectedOptionValue: undefined,
selectedOptionValueArr: 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
getSelectedOptionValueArr(): string[] {
return Array.isArray(this.props.selectedOptionValueArr)
? this.props.selectedOptionValueArr
: [];
}
2019-09-12 08:11:25 +00:00
getPageView() {
2020-02-11 11:03:10 +00:00
const options = this.props.options || [];
2020-04-17 16:15:09 +00:00
const selectedIndex = _.findIndex(this.props.options, {
value: this.props.selectedOptionValue,
2020-02-11 11:03:10 +00:00
});
const computedSelectedIndexArr = this.getSelectedOptionValueArr()
.map((opt: string) =>
_.findIndex(this.props.options, {
value: opt,
}),
)
.filter((i: number) => i > -1);
const { componentWidth, componentHeight } = this.getComponentDimensions();
2019-10-31 05:28:11 +00:00
return (
<DropDownComponent
onOptionSelected={this.onOptionSelected}
onOptionRemoved={this.onOptionRemoved}
widgetId={this.props.widgetId}
placeholder={this.props.placeholderText}
2020-02-11 11:03:10 +00:00
options={options}
height={componentHeight}
width={componentWidth}
2019-10-31 05:28:11 +00:00
selectionType={this.props.selectionType}
2020-04-17 16:15:09 +00:00
selectedIndex={selectedIndex > -1 ? selectedIndex : undefined}
2020-02-11 11:03:10 +00:00
selectedIndexArr={computedSelectedIndexArr}
2020-05-25 09:32:01 +00:00
label={`${this.props.label}`}
2019-12-03 04:41:10 +00:00
isLoading={this.props.isLoading}
disabled={this.props.isDisabled}
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;
2019-10-31 05:28:11 +00:00
if (this.props.selectionType === "SINGLE_SELECT") {
2020-10-21 19:45:02 +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(
"selectedOptionValue",
selectedOption.value,
2020-10-06 16:47:16 +00:00
{
dynamicString: this.props.onOptionChange,
event: {
type: EventType.ON_OPTION_CHANGE,
},
},
);
}
2019-10-31 05:28:11 +00:00
} else if (this.props.selectionType === "MULTI_SELECT") {
const selectedOptionValueArr = this.getSelectedOptionValueArr();
const isAlreadySelected = selectedOptionValueArr.includes(
2020-04-17 16:15:09 +00:00
selectedOption.value,
);
let newSelectedValue = [...selectedOptionValueArr];
2019-10-31 05:28:11 +00:00
if (isAlreadySelected) {
2020-04-17 16:15:09 +00:00
newSelectedValue = newSelectedValue.filter(
2020-12-24 04:32:25 +00:00
(v) => v !== selectedOption.value,
2020-04-17 16:15:09 +00:00
);
2019-10-31 05:28:11 +00:00
} else {
2020-04-17 16:15:09 +00:00
newSelectedValue.push(selectedOption.value);
2019-10-31 05:28:11 +00:00
}
2020-10-06 09:01:51 +00:00
this.props.updateWidgetMetaProperty(
"selectedOptionValueArr",
newSelectedValue,
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
};
onOptionRemoved = (removedIndex: number) => {
const newSelectedValue = this.getSelectedOptionValueArr().filter(
2020-04-17 16:15:09 +00:00
(v: string) =>
_.findIndex(this.props.options, { value: v }) !== removedIndex,
);
2020-10-06 09:01:51 +00:00
this.props.updateWidgetMetaProperty(
"selectedOptionValueArr",
newSelectedValue,
2020-10-06 16:47:16 +00:00
{
2020-02-18 10:41:52 +00:00
dynamicString: this.props.onOptionChange,
event: {
type: EventType.ON_OPTION_CHANGE,
},
2020-10-06 16:47:16 +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 type SelectionType = "SINGLE_SELECT" | "MULTI_SELECT";
2019-09-12 08:11:25 +00:00
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;
selectedIndexArr?: number[];
selectionType: SelectionType;
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;
2020-10-06 09:01:51 +00:00
selectedOptionValue: string;
selectedOptionValueArr: string[];
selectedOptionLabels: 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),
);