2019-09-12 08:11:25 +00:00
|
|
|
import React from "react";
|
2021-09-09 15:10:22 +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";
|
2021-09-09 15:10:22 +00:00
|
|
|
import DropDownComponent from "../component";
|
2019-10-31 05:28:11 +00:00
|
|
|
import _ from "lodash";
|
2021-09-09 15:10:22 +00:00
|
|
|
import { DropdownOption } from "../constants";
|
2021-07-26 05:50:46 +00:00
|
|
|
import {
|
|
|
|
|
ValidationResponse,
|
|
|
|
|
ValidationTypes,
|
|
|
|
|
} from "constants/WidgetValidation";
|
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";
|
2021-11-16 09:27:38 +00:00
|
|
|
import { GRID_DENSITY_MIGRATION_V1 } from "widgets/constants";
|
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: "",
|
2021-09-29 12:03:11 +00:00
|
|
|
messages: ["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-20 10:43:44 +00:00
|
|
|
placeholderText: '[{ "label": "Option1", "value": "Option2" }]',
|
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-20 10:43:44 +00:00
|
|
|
placeholderText: "GREEN",
|
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-20 10:43:44 +00:00
|
|
|
example: `option1 | ['option1', 'option2']`,
|
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
|
|
|
{
|
2021-11-16 09:27:38 +00:00
|
|
|
helpText: "Sets a Label Text",
|
|
|
|
|
propertyName: "labelText",
|
|
|
|
|
label: "Label Text",
|
|
|
|
|
controlType: "INPUT_TEXT",
|
|
|
|
|
placeholderText: "Enter Label text",
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
|
|
|
|
validation: { type: ValidationTypes.TEXT },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
helpText: "Sets a Placeholder Text",
|
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-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-09-20 10:43:44 +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-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
|
|
|
],
|
|
|
|
|
},
|
2021-11-16 09:27:38 +00:00
|
|
|
{
|
|
|
|
|
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: "PARAGRAPH",
|
|
|
|
|
options: [
|
|
|
|
|
{
|
|
|
|
|
label: "Heading 1",
|
|
|
|
|
value: "HEADING1",
|
|
|
|
|
subText: "24px",
|
|
|
|
|
icon: "HEADING_ONE",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: "Heading 2",
|
|
|
|
|
value: "HEADING2",
|
|
|
|
|
subText: "18px",
|
|
|
|
|
icon: "HEADING_TWO",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: "Heading 3",
|
|
|
|
|
value: "HEADING3",
|
|
|
|
|
subText: "16px",
|
|
|
|
|
icon: "HEADING_THREE",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: "Paragraph",
|
|
|
|
|
value: "PARAGRAPH",
|
|
|
|
|
subText: "14px",
|
|
|
|
|
icon: "PARAGRAPH",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: "Paragraph 2",
|
|
|
|
|
value: "PARAGRAPH2",
|
|
|
|
|
subText: "12px",
|
|
|
|
|
icon: "PARAGRAPH_TWO",
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
isBindProperty: false,
|
|
|
|
|
isTriggerProperty: false,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
propertyName: "labelStyle",
|
|
|
|
|
label: "Label Font Style",
|
|
|
|
|
controlType: "BUTTON_TABS",
|
|
|
|
|
options: [
|
|
|
|
|
{
|
|
|
|
|
icon: "BOLD_FONT",
|
|
|
|
|
value: "BOLD",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
icon: "ITALICS_FONT",
|
|
|
|
|
value: "ITALIC",
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
isJSConvertible: true,
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
|
|
|
|
validation: { type: ValidationTypes.TEXT },
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
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-09-09 15:10:22 +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-11-16 09:27:38 +00:00
|
|
|
compactMode={
|
|
|
|
|
!(
|
|
|
|
|
(this.props.bottomRow - this.props.topRow) /
|
|
|
|
|
GRID_DENSITY_MIGRATION_V1 >
|
|
|
|
|
1
|
|
|
|
|
)
|
|
|
|
|
}
|
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}
|
2021-11-16 09:27:38 +00:00
|
|
|
isValid={this.props.isValid}
|
|
|
|
|
labelStyle={this.props.labelStyle}
|
|
|
|
|
labelText={this.props.labelText}
|
|
|
|
|
labelTextColor={this.props.labelTextColor}
|
|
|
|
|
labelTextSize={this.props.labelTextSize}
|
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,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2021-09-09 15:10:22 +00:00
|
|
|
static getWidgetType(): WidgetType {
|
2019-09-12 08:11:25 +00:00
|
|
|
return "DROP_DOWN_WIDGET";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-09 15:10:22 +00:00
|
|
|
export interface DropdownWidgetProps extends WidgetProps {
|
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;
|