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";
|
2021-07-26 05:50:46 +00:00
|
|
|
import {
|
|
|
|
|
ValidationResponse,
|
|
|
|
|
ValidationTypes,
|
|
|
|
|
} 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";
|
2021-06-01 04:59:45 +00:00
|
|
|
import { EvaluationSubstitutionType } from "entities/DataTree/dataTreeFactory";
|
2021-08-04 05:34:44 +00:00
|
|
|
import { AutocompleteDataType } from "utils/autocomplete/TernServer";
|
2019-09-12 08:11:25 +00:00
|
|
|
|
2021-08-03 06:38:01 +00:00
|
|
|
function defaultOptionValueValidation(value: unknown): ValidationResponse {
|
|
|
|
|
if (typeof value === "string") return { isValid: true, parsed: value.trim() };
|
|
|
|
|
if (value === undefined || value === null)
|
2021-07-26 05:50:46 +00:00
|
|
|
return {
|
2021-08-03 06:38:01 +00:00
|
|
|
isValid: false,
|
|
|
|
|
parsed: "",
|
|
|
|
|
message: "This value does not evaluate to type: string",
|
2021-07-26 05:50:46 +00:00
|
|
|
};
|
2021-08-03 06:38:01 +00:00
|
|
|
return { isValid: true, parsed: value };
|
2021-07-26 05:50:46 +00:00
|
|
|
}
|
2021-08-03 06:38:01 +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:
|
2021-08-03 06:38:01 +00:00
|
|
|
"Allows users to select a single option. Values must be unique",
|
2021-02-16 10:29:08 +00:00
|
|
|
propertyName: "options",
|
|
|
|
|
label: "Options",
|
|
|
|
|
controlType: "INPUT_TEXT",
|
2021-09-09 08:18:16 +00:00
|
|
|
placeholderText: 'Enter [{"label": "label1", "value": "value2"}]',
|
2021-02-16 10:29:08 +00:00
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
2021-07-26 05:50:46 +00:00
|
|
|
validation: {
|
|
|
|
|
type: ValidationTypes.ARRAY,
|
|
|
|
|
params: {
|
2021-08-03 06:38:01 +00:00
|
|
|
unique: ["value"],
|
2021-07-26 05:50:46 +00:00
|
|
|
children: {
|
|
|
|
|
type: ValidationTypes.OBJECT,
|
|
|
|
|
params: {
|
2021-08-17 11:35:51 +00:00
|
|
|
required: true,
|
2021-07-26 05:50:46 +00:00
|
|
|
allowedKeys: [
|
|
|
|
|
{
|
|
|
|
|
name: "label",
|
|
|
|
|
type: ValidationTypes.TEXT,
|
2021-08-03 06:38:01 +00:00
|
|
|
params: {
|
|
|
|
|
default: "",
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
2021-07-26 05:50:46 +00:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "value",
|
|
|
|
|
type: ValidationTypes.TEXT,
|
2021-08-03 06:38:01 +00:00
|
|
|
params: {
|
|
|
|
|
default: "",
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
2021-07-26 05:50:46 +00:00
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2021-06-01 04:59:45 +00:00
|
|
|
evaluationSubstitutionType:
|
|
|
|
|
EvaluationSubstitutionType.SMART_SUBSTITUTE,
|
2021-02-16 10:29:08 +00:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
helpText: "Selects the option with value by default",
|
|
|
|
|
propertyName: "defaultOptionValue",
|
|
|
|
|
label: "Default Option",
|
|
|
|
|
controlType: "INPUT_TEXT",
|
2021-09-09 08:18:16 +00:00
|
|
|
placeholderText: "Enter option value",
|
2021-02-16 10:29:08 +00:00
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
2021-07-26 05:50:46 +00:00
|
|
|
validation: {
|
|
|
|
|
type: ValidationTypes.FUNCTION,
|
|
|
|
|
params: {
|
|
|
|
|
fn: defaultOptionValueValidation,
|
|
|
|
|
expected: {
|
|
|
|
|
type: "value or Array of values",
|
2021-09-09 08:18:16 +00:00
|
|
|
example: `value1 | ['value1', 'value2']`,
|
2021-08-04 05:34:44 +00:00
|
|
|
autocompleteDataType: AutocompleteDataType.STRING,
|
2021-07-26 05:50:46 +00:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2021-08-02 13:06:22 +00:00
|
|
|
dependencies: ["selectionType"],
|
2021-02-16 10:29:08 +00:00
|
|
|
},
|
2021-08-25 14:07:01 +00:00
|
|
|
{
|
|
|
|
|
propertyName: "placeholderText",
|
|
|
|
|
label: "Placeholder",
|
|
|
|
|
controlType: "INPUT_TEXT",
|
|
|
|
|
placeholderText: "Enter placeholder text",
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
|
|
|
|
validation: { type: ValidationTypes.TEXT },
|
|
|
|
|
},
|
2021-09-09 08:18:16 +00:00
|
|
|
{
|
|
|
|
|
propertyName: "isFilterable",
|
|
|
|
|
label: "Filterable",
|
|
|
|
|
helpText: "Makes the dropdown list filterable",
|
|
|
|
|
controlType: "SWITCH",
|
|
|
|
|
isJSConvertible: true,
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
|
|
|
|
validation: { type: ValidationTypes.BOOLEAN },
|
|
|
|
|
},
|
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-07-26 05:50:46 +00:00
|
|
|
validation: { type: ValidationTypes.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-07-26 05:50:46 +00:00
|
|
|
validation: { type: ValidationTypes.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-07-26 05:50:46 +00:00
|
|
|
validation: { type: ValidationTypes.BOOLEAN },
|
2021-02-16 10:29:08 +00:00
|
|
|
},
|
2021-08-16 06:56:09 +00:00
|
|
|
{
|
|
|
|
|
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 },
|
|
|
|
|
},
|
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,
|
|
|
|
|
},
|
2021-08-16 06:56:09 +00:00
|
|
|
{
|
|
|
|
|
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,
|
|
|
|
|
},
|
2021-02-16 10:29:08 +00:00
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
}
|
2020-06-09 12:04:38 +00:00
|
|
|
|
2020-01-17 09:28:26 +00:00
|
|
|
static getDerivedPropertiesMap() {
|
|
|
|
|
return {
|
2021-08-03 06:38:01 +00:00
|
|
|
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 } ) }}`,
|
2021-08-03 06:38:01 +00:00
|
|
|
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 {
|
2021-08-03 06:38:01 +00:00
|
|
|
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 {
|
2021-08-03 06:38:01 +00:00
|
|
|
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 : [];
|
2021-08-17 11:35:51 +00:00
|
|
|
|
2020-04-17 16:15:09 +00:00
|
|
|
const selectedIndex = _.findIndex(this.props.options, {
|
2021-08-03 06:38:01 +00:00
|
|
|
value: this.props.defaultValue,
|
2020-02-11 11:03:10 +00:00
|
|
|
});
|
2021-05-13 08:35:39 +00:00
|
|
|
const { componentHeight, componentWidth } = this.getComponentDimensions();
|
2019-10-31 05:28:11 +00:00
|
|
|
return (
|
|
|
|
|
<DropDownComponent
|
2021-04-28 10:28:39 +00:00
|
|
|
disabled={this.props.isDisabled}
|
|
|
|
|
height={componentHeight}
|
2021-05-06 18:04:14 +00:00
|
|
|
isFilterable={this.props.isFilterable}
|
2021-04-28 10:28:39 +00:00
|
|
|
isLoading={this.props.isLoading}
|
|
|
|
|
label={`${this.props.label}`}
|
2021-08-16 06:56:09 +00:00
|
|
|
onFilterChange={this.onFilterChange}
|
2021-04-28 10:28:39 +00:00
|
|
|
onOptionSelected={this.onOptionSelected}
|
2020-02-11 11:03:10 +00:00
|
|
|
options={options}
|
2021-04-28 10:28:39 +00:00
|
|
|
placeholder={this.props.placeholderText}
|
2020-04-17 16:15:09 +00:00
|
|
|
selectedIndex={selectedIndex > -1 ? selectedIndex : undefined}
|
2021-08-16 06:56:09 +00:00
|
|
|
serverSideFiltering={this.props.serverSideFiltering}
|
2021-04-28 10:28:39 +00:00
|
|
|
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) => {
|
2020-10-03 09:52:59 +00:00
|
|
|
let isChanged = true;
|
2020-04-17 16:15:09 +00:00
|
|
|
|
2021-08-03 06:38:01 +00:00
|
|
|
// Check if the value has changed. If no option
|
|
|
|
|
// selected till now, there is a change
|
2021-08-09 05:35:01 +00:00
|
|
|
if (this.props.selectedOptionValue) {
|
|
|
|
|
isChanged = !(this.props.selectedOptionValue === selectedOption.value);
|
2021-08-03 06:38:01 +00:00
|
|
|
}
|
2021-08-09 05:35:01 +00:00
|
|
|
|
2021-08-03 06:38:01 +00:00
|
|
|
if (isChanged) {
|
2020-10-06 09:01:51 +00:00
|
|
|
this.props.updateWidgetMetaProperty(
|
2021-08-03 06:38:01 +00:00
|
|
|
"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
|
|
|
};
|
|
|
|
|
|
2021-08-16 06:56:09 +00:00
|
|
|
onFilterChange = (value: string) => {
|
|
|
|
|
this.props.updateWidgetMetaProperty("filterText", value);
|
|
|
|
|
|
|
|
|
|
super.executeAction({
|
|
|
|
|
triggerPropertyName: "onFilterUpdate",
|
|
|
|
|
dynamicString: this.props.onFilterUpdate,
|
|
|
|
|
event: {
|
|
|
|
|
type: EventType.ON_FILTER_UPDATE,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2019-09-12 08:11:25 +00:00
|
|
|
getWidgetType(): WidgetType {
|
|
|
|
|
return "DROP_DOWN_WIDGET";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
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;
|
2021-03-30 16:40:23 +00:00
|
|
|
isFilterable: boolean;
|
2021-08-03 06:38:01 +00:00
|
|
|
defaultValue: string;
|
2021-02-26 06:24:25 +00:00
|
|
|
selectedOptionLabel: string;
|
2021-08-16 06:56:09 +00:00
|
|
|
serverSideFiltering: boolean;
|
|
|
|
|
onFilterUpdate: 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),
|
|
|
|
|
);
|