import { EventType } from "constants/AppsmithActionConstants/ActionConstants"; import type { WidgetType } from "constants/WidgetConstants"; import type { ValidationResponse } from "constants/WidgetValidation"; import { ValidationTypes } from "constants/WidgetValidation"; import { isArray } from "lodash"; import React from "react"; import type { WidgetProps, WidgetState } from "widgets/BaseWidget"; import BaseWidget from "widgets/BaseWidget"; import { Alignment } from "@blueprintjs/core"; import { LabelPosition } from "components/constants"; import { Layers } from "constants/Layers"; import type { Stylesheet } from "entities/AppTheming"; import { EvaluationSubstitutionType } from "entities/DataTree/dataTreeFactory"; import type { DraftValueType } from "rc-select/lib/Select"; import { AutocompleteDataType } from "utils/autocomplete/CodemirrorTernService"; import { GRID_DENSITY_MIGRATION_V1, MinimumPopupRows } from "widgets/constants"; import MultiSelectComponent from "../component"; import { DefaultAutocompleteDefinitions } from "widgets/WidgetUtils"; import type { AutocompletionDefinitions } from "widgets/constants"; function defaultOptionValueValidation(value: unknown): ValidationResponse { let values: string[] = []; if (typeof value === "string") { try { values = JSON.parse(value); if (!Array.isArray(values)) { throw new Error(); } } catch { values = value.length ? value.split(",") : []; if (values.length > 0) { values = values.map((_v: string) => _v.trim()); } } } if (Array.isArray(value)) { values = Array.from(new Set(value)); } return { isValid: true, parsed: values, }; } class MultiSelectWidget extends BaseWidget< MultiSelectWidgetProps, WidgetState > { static getAutocompleteDefinitions(): AutocompletionDefinitions { return { "!doc": "MultiSelect is used to capture user input/s from a specified list of permitted inputs. A MultiSelect captures multiple choices from a list of options", "!url": "https://docs.appsmith.com/widget-reference/dropdown", isVisible: DefaultAutocompleteDefinitions.isVisible, filterText: { "!type": "string", "!doc": "The filter text for Server side filtering", }, selectedOptionValues: { "!type": "[string]", "!doc": "The array of values selected in a multi select dropdown", "!url": "https://docs.appsmith.com/widget-reference/dropdown", }, selectedOptionLabels: { "!type": "[string]", "!doc": "The array of selected option labels in a multi select dropdown", "!url": "https://docs.appsmith.com/widget-reference/dropdown", }, isDisabled: "bool", options: "[$__dropdownOption__$]", }; } static getPropertyPaneConfig() { return [ { sectionName: "General", children: [ { helpText: "Allows users to select multiple options. Values must be unique", propertyName: "options", label: "Options", controlType: "INPUT_TEXT", placeholderText: '[{ "label": "Option1", "value": "Option2" }]', isBindProperty: true, isTriggerProperty: false, isJSConvertible: false, validation: { type: ValidationTypes.ARRAY, params: { unique: ["value"], children: { type: ValidationTypes.OBJECT, params: { required: true, allowedKeys: [ { name: "label", type: ValidationTypes.TEXT, params: { default: "", required: true, }, }, { name: "value", type: ValidationTypes.TEXT, params: { default: "", }, }, ], }, }, }, }, evaluationSubstitutionType: EvaluationSubstitutionType.SMART_SUBSTITUTE, }, { helpText: "Selects the option with value by default", propertyName: "defaultOptionValue", label: "Default Value", controlType: "INPUT_TEXT", placeholderText: "[GREEN]", isBindProperty: true, isTriggerProperty: false, validation: { type: ValidationTypes.FUNCTION, params: { fn: defaultOptionValueValidation, expected: { type: "Array of values", example: `['option1', 'option2']`, autocompleteDataType: AutocompleteDataType.ARRAY, }, }, }, }, { helpText: "Sets a Placeholder Text", propertyName: "placeholderText", label: "Placeholder", controlType: "INPUT_TEXT", placeholderText: "Search", isBindProperty: true, isTriggerProperty: false, validation: { type: ValidationTypes.TEXT }, }, { 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 }, }, { propertyName: "animateLoading", label: "Animate Loading", controlType: "SWITCH", helpText: "Controls the loading of the widget", defaultValue: true, isJSConvertible: true, isBindProperty: true, isTriggerProperty: false, validation: { type: ValidationTypes.BOOLEAN }, }, { 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 }, }, { helpText: "Controls the visibility of select all option in dropdown.", propertyName: "allowSelectAll", label: "Allow Select All", controlType: "SWITCH", isJSConvertible: true, isBindProperty: true, isTriggerProperty: false, validation: { type: ValidationTypes.BOOLEAN }, }, ], }, { sectionName: "Label", children: [ { helpText: "Sets the label text of the widget", propertyName: "labelText", label: "Text", controlType: "INPUT_TEXT", placeholderText: "Enter label text", isBindProperty: true, isTriggerProperty: false, validation: { type: ValidationTypes.TEXT }, }, { helpText: "Sets the label position of the widget", propertyName: "labelPosition", label: "Position", controlType: "DROP_DOWN", options: [ { label: "Left", value: LabelPosition.Left }, { label: "Top", value: LabelPosition.Top }, { label: "Auto", value: LabelPosition.Auto }, ], defaultValue: LabelPosition.Top, isBindProperty: false, isTriggerProperty: false, validation: { type: ValidationTypes.TEXT }, }, { helpText: "Sets the label alignment of the widget", propertyName: "labelAlignment", label: "Alignment", controlType: "LABEL_ALIGNMENT_OPTIONS", options: [ { icon: "LEFT_ALIGN", value: Alignment.LEFT, }, { icon: "RIGHT_ALIGN", value: Alignment.RIGHT, }, ], isBindProperty: false, isTriggerProperty: false, validation: { type: ValidationTypes.TEXT }, hidden: (props: MultiSelectWidgetProps) => props.labelPosition !== LabelPosition.Left, dependencies: ["labelPosition"], }, { helpText: "Sets the label width of the widget as the number of columns", propertyName: "labelWidth", label: "Width (in columns)", controlType: "NUMERIC_INPUT", isJSConvertible: true, isBindProperty: true, isTriggerProperty: false, min: 0, validation: { type: ValidationTypes.NUMBER, params: { natural: true, }, }, hidden: (props: MultiSelectWidgetProps) => props.labelPosition !== LabelPosition.Left, dependencies: ["labelPosition"], }, ], }, { sectionName: "Styles", children: [ { propertyName: "accentColor", label: "Accent Color", controlType: "COLOR_PICKER", isJSConvertible: true, isBindProperty: true, isTriggerProperty: false, validation: { type: ValidationTypes.TEXT }, invisible: true, }, { 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: "0.875rem", options: [ { label: "S", value: "0.875rem", subText: "0.875rem", }, { label: "M", value: "1rem", subText: "1rem", }, { label: "L", value: "1.25rem", subText: "1.25rem", }, { label: "XL", value: "1.875rem", subText: "1.875rem", }, { label: "XXL", value: "3rem", subText: "3rem", }, { label: "3XL", value: "3.75rem", subText: "3.75rem", }, ], isJSConvertible: true, isBindProperty: true, isTriggerProperty: false, validation: { type: ValidationTypes.TEXT }, }, { propertyName: "labelStyle", label: "Label Font Style", controlType: "BUTTON_GROUP", options: [ { icon: "BOLD_FONT", value: "BOLD", }, { icon: "ITALICS_FONT", value: "ITALIC", }, ], isJSConvertible: true, isBindProperty: true, isTriggerProperty: false, validation: { type: ValidationTypes.TEXT }, }, ], }, { sectionName: "Actions", children: [ { helpText: "when a user selects an option", propertyName: "onOptionChange", label: "onOptionChange", controlType: "ACTION_SELECTOR", isJSConvertible: true, isBindProperty: true, isTriggerProperty: true, }, { helpText: "Trigger an action on change of filterText", hidden: (props: MultiSelectWidgetProps) => !props.serverSideFiltering, dependencies: ["serverSideFiltering"], propertyName: "onFilterUpdate", label: "onFilterUpdate", controlType: "ACTION_SELECTOR", isJSConvertible: true, isBindProperty: true, isTriggerProperty: true, }, ], }, { sectionName: "Styles", children: [ { propertyName: "backgroundColor", helpText: "Sets the background color of the widget", label: "Background color", controlType: "COLOR_PICKER", isBindProperty: false, isTriggerProperty: false, }, ], }, ]; } static getDerivedPropertiesMap() { return { selectedIndexArr: `{{ this.selectedOptionValues.map(o => _.findIndex(this.options, { value: o })) }}`, selectedOptionLabels: `{{ this.selectedOptionValueArr.map((o) => { const index = _.findIndex(this.options, { value: o }); return this.options[index]?.label ?? this.options[index]?.value; }) }}`, selectedOptionValues: `{{ this.selectedOptionValueArr.filter((o) => { const index = _.findIndex(this.options, { value: o }); return index > -1; }) }}`, isValid: `{{this.isRequired ? !!this.selectedIndexArr && this.selectedIndexArr.length > 0 : true}}`, }; } static getDefaultPropertiesMap(): Record { return { selectedOptionValueArr: "defaultOptionValue", filterText: "", }; } static getMetaPropertiesMap(): Record { return { selectedOptionValueArr: undefined, filterText: "", }; } static getStylesheetConfig(): Stylesheet { return { accentColor: "{{appsmith.theme.colors.primaryColor}}", borderRadius: "{{appsmith.theme.borderRadius.appBorderRadius}}", boxShadow: "none", }; } getPageView() { const options = isArray(this.props.options) ? this.props.options : []; const values: string[] = isArray(this.props.selectedOptionValues) ? this.props.selectedOptionValues : []; const dropDownWidth = MinimumPopupRows * this.props.parentColumnSpace; const { componentWidth } = this.getComponentDimensions(); return ( 1 ) } disabled={this.props.isDisabled ?? false} dropDownWidth={dropDownWidth} dropdownStyle={{ zIndex: Layers.dropdownModalWidget, }} isValid={this.props.isValid} labelAlignment={this.props.labelAlignment} labelPosition={this.props.labelPosition} labelStyle={this.props.labelStyle} labelText={this.props.labelText} labelTextColor={this.props.labelTextColor} labelTextSize={this.props.labelTextSize} labelWidth={this.getLabelWidth()} loading={this.props.isLoading} onChange={this.onOptionChange} onFilterChange={this.onFilterChange} options={options} placeholder={this.props.placeholderText as string} serverSideFiltering={this.props.serverSideFiltering} value={values} widgetId={this.props.widgetId} width={componentWidth} /> ); } onOptionChange = (value: DraftValueType) => { this.props.updateWidgetMetaProperty("selectedOptionValueArr", value, { triggerPropertyName: "onOptionChange", dynamicString: this.props.onOptionChange, event: { type: EventType.ON_OPTION_CHANGE, }, }); // Empty filter after Selection this.onFilterChange(""); }; onFilterChange = (value: string) => { this.props.updateWidgetMetaProperty("filterText", value); if (this.props.onFilterUpdate) { super.executeAction({ triggerPropertyName: "onFilterUpdate", dynamicString: this.props.onFilterUpdate, event: { type: EventType.ON_FILTER_UPDATE, }, }); } }; static getWidgetType(): WidgetType { return "MULTI_SELECT_WIDGET"; } } export interface DropdownOption { label: string; value: string; disabled?: boolean; } export interface MultiSelectWidgetProps extends WidgetProps { placeholderText?: string; selectedIndex?: number; selectedIndexArr?: number[]; selectedOption: DropdownOption; options?: DropdownOption[]; onOptionChange: string; onFilterChange: string; defaultOptionValue: string | string[]; isRequired: boolean; isLoading: boolean; selectedOptionValueArr: string[]; filterText: string; selectedOptionValues: string[]; selectedOptionLabels: string[]; serverSideFiltering: boolean; onFilterUpdate: string; backgroundColor: string; borderRadius: string; boxShadow?: string; accentColor: string; allowSelectAll?: boolean; labelText: string; labelPosition?: LabelPosition; labelAlignment?: Alignment; labelWidth?: number; } export default MultiSelectWidget;