2019-09-12 08:11:25 +00:00
|
|
|
import React from "react";
|
2019-09-13 10:45:49 +00:00
|
|
|
import BaseWidget, { WidgetProps, WidgetState } from "./BaseWidget";
|
2020-08-19 04:37:20 +00:00
|
|
|
import { WidgetType } from "constants/WidgetConstants";
|
2021-03-30 05:29:03 +00:00
|
|
|
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";
|
2020-08-28 17:23:07 +00:00
|
|
|
import * as Sentry from "@sentry/react";
|
2020-10-06 09:01:51 +00:00
|
|
|
import withMeta, { WithMeta } from "./MetaHOC";
|
2021-02-22 06:15:02 +00:00
|
|
|
import { IconName } from "@blueprintjs/icons";
|
2019-09-12 08:11:25 +00:00
|
|
|
|
2019-09-13 10:45:49 +00:00
|
|
|
class DropdownWidget extends BaseWidget<DropdownWidgetProps, WidgetState> {
|
2021-02-16 10:29:08 +00:00
|
|
|
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,
|
2021-04-21 14:34:25 +00:00
|
|
|
validation: VALIDATION_TYPES.OPTIONS_DATA,
|
2021-02-16 10:29:08 +00:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
helpText: "Selects the option with value by default",
|
|
|
|
|
propertyName: "defaultOptionValue",
|
|
|
|
|
label: "Default Option",
|
|
|
|
|
controlType: "INPUT_TEXT",
|
|
|
|
|
placeholderText: "Enter option value",
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
2021-04-21 14:34:25 +00:00
|
|
|
validation: VALIDATION_TYPES.DEFAULT_OPTION_VALUE,
|
2021-02-16 10:29:08 +00:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
propertyName: "isRequired",
|
|
|
|
|
label: "Required",
|
|
|
|
|
helpText: "Makes input to the widget mandatory",
|
|
|
|
|
controlType: "SWITCH",
|
|
|
|
|
isJSConvertible: true,
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
2021-04-21 14:34:25 +00:00
|
|
|
validation: VALIDATION_TYPES.BOOLEAN,
|
2021-02-16 10:29:08 +00:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
helpText: "Controls the visibility of the widget",
|
|
|
|
|
propertyName: "isVisible",
|
|
|
|
|
label: "Visible",
|
|
|
|
|
controlType: "SWITCH",
|
|
|
|
|
isJSConvertible: true,
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
2021-04-21 14:34:25 +00:00
|
|
|
validation: VALIDATION_TYPES.BOOLEAN,
|
2021-02-16 10:29:08 +00:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
propertyName: "isDisabled",
|
|
|
|
|
label: "Disabled",
|
|
|
|
|
helpText: "Disables input to this widget",
|
|
|
|
|
controlType: "SWITCH",
|
|
|
|
|
isJSConvertible: true,
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
2021-04-21 14:34:25 +00:00
|
|
|
validation: VALIDATION_TYPES.BOOLEAN,
|
2021-02-16 10:29:08 +00:00
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
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-06-09 12:04:38 +00:00
|
|
|
|
2020-01-17 09:28:26 +00:00
|
|
|
static getDerivedPropertiesMap() {
|
|
|
|
|
return {
|
2020-04-02 19:41:04 +00:00
|
|
|
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 })) }}`,
|
2020-06-09 12:04:38 +00:00
|
|
|
value: `{{ this.selectionType === 'SINGLE_SELECT' ? this.selectedOptionValue : this.selectedOptionValueArr }}`,
|
2020-09-10 07:21:44 +00:00
|
|
|
selectedOptionValues: `{{ this.selectedOptionValueArr }}`,
|
2021-02-26 06:24:25 +00:00
|
|
|
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,
|
2020-04-21 07:54:23 +00:00
|
|
|
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
|
|
|
|
2020-12-10 05:55:12 +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
|
|
|
});
|
2020-12-10 05:55:12 +00:00
|
|
|
const computedSelectedIndexArr = this.getSelectedOptionValueArr()
|
|
|
|
|
.map((opt: string) =>
|
|
|
|
|
_.findIndex(this.props.options, {
|
|
|
|
|
value: opt,
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
.filter((i: number) => i > -1);
|
2020-08-18 07:41:53 +00:00
|
|
|
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}
|
2020-08-18 07:41:53 +00:00
|
|
|
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}
|
2020-08-07 13:17:15 +00:00
|
|
|
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) => {
|
2020-10-03 09:52:59 +00:00
|
|
|
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);
|
|
|
|
|
}
|
2020-10-03 09:52:59 +00:00
|
|
|
if (isChanged) {
|
2020-10-06 09:01:51 +00:00
|
|
|
this.props.updateWidgetMetaProperty(
|
2020-10-03 09:52:59 +00:00
|
|
|
"selectedOptionValue",
|
|
|
|
|
selectedOption.value,
|
2020-10-06 16:47:16 +00:00
|
|
|
{
|
|
|
|
|
dynamicString: this.props.onOptionChange,
|
|
|
|
|
event: {
|
|
|
|
|
type: EventType.ON_OPTION_CHANGE,
|
|
|
|
|
},
|
|
|
|
|
},
|
2020-10-03 09:52:59 +00:00
|
|
|
);
|
|
|
|
|
}
|
2019-10-31 05:28:11 +00:00
|
|
|
} else if (this.props.selectionType === "MULTI_SELECT") {
|
2020-12-10 05:55:12 +00:00
|
|
|
const selectedOptionValueArr = this.getSelectedOptionValueArr();
|
|
|
|
|
const isAlreadySelected = selectedOptionValueArr.includes(
|
2020-04-17 16:15:09 +00:00
|
|
|
selectedOption.value,
|
|
|
|
|
);
|
|
|
|
|
|
2020-12-10 05:55:12 +00:00
|
|
|
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) => {
|
2020-12-10 05:55:12 +00:00
|
|
|
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";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-17 10:09:00 +00:00
|
|
|
export type SelectionType = "SINGLE_SELECT" | "MULTI_SELECT";
|
2019-09-12 08:11:25 +00:00
|
|
|
export interface DropdownOption {
|
2019-09-13 10:45:49 +00:00
|
|
|
label: string;
|
|
|
|
|
value: string;
|
2021-02-22 06:15:02 +00:00
|
|
|
icon?: IconName;
|
2021-02-16 10:29:08 +00:00
|
|
|
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 {
|
2019-09-19 11:29:24 +00:00
|
|
|
placeholderText?: string;
|
2019-09-13 10:45:49 +00:00
|
|
|
label?: string;
|
2019-10-31 05:28:11 +00:00
|
|
|
selectedIndex?: number;
|
|
|
|
|
selectedIndexArr?: number[];
|
2019-09-17 10:09:00 +00:00
|
|
|
selectionType: SelectionType;
|
2020-04-17 16:15:09 +00:00
|
|
|
selectedOption: DropdownOption;
|
2019-09-13 10:45:49 +00:00
|
|
|
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[];
|
2021-02-26 06:24:25 +00:00
|
|
|
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),
|
|
|
|
|
);
|