import React from "react"; import BaseWidget, { WidgetProps, WidgetState } from "../../BaseWidget"; import { WidgetType } from "constants/WidgetConstants"; import { EventType } from "constants/AppsmithActionConstants/ActionConstants"; import DropDownComponent from "../component"; import _ from "lodash"; import { DropdownOption } from "../constants"; import { ValidationResponse, ValidationTypes, } from "constants/WidgetValidation"; import { EvaluationSubstitutionType } from "entities/DataTree/dataTreeFactory"; import { AutocompleteDataType } from "utils/autocomplete/CodemirrorTernService"; import { MinimumPopupRows, GRID_DENSITY_MIGRATION_V1 } from "widgets/constants"; import { LabelPosition } from "components/constants"; import { Alignment } from "@blueprintjs/core"; import { Stylesheet } from "entities/AppTheming"; function defaultOptionValueValidation(value: unknown): ValidationResponse { if (typeof value === "string") return { isValid: true, parsed: value.trim() }; if (value === undefined || value === null) return { isValid: false, parsed: "", messages: ["This value does not evaluate to type: string"], }; return { isValid: true, parsed: value }; } class DropdownWidget extends BaseWidget { static getPropertyPaneConfig() { return [ { sectionName: "General", children: [ { helpText: "Allows users to select a single option. Values must be unique", propertyName: "options", label: "Options", controlType: "INPUT_TEXT", placeholderText: '[{ "label": "Option1", "value": "Option2" }]', isBindProperty: true, isTriggerProperty: 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 Option", controlType: "INPUT_TEXT", placeholderText: "GREEN", isBindProperty: true, isTriggerProperty: false, validation: { type: ValidationTypes.FUNCTION, params: { fn: defaultOptionValueValidation, expected: { type: "value or Array of values", example: `option1 | ['option1', 'option2']`, autocompleteDataType: AutocompleteDataType.STRING, }, }, }, dependencies: ["selectionType"], }, { helpText: "Sets a Placeholder Text", propertyName: "placeholderText", label: "Placeholder", controlType: "INPUT_TEXT", placeholderText: "Enter placeholder text", 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 }, }, { propertyName: "isFilterable", label: "Filterable", helpText: "Makes the dropdown list filterable", controlType: "SWITCH", 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 }, }, ], }, { 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: DropdownWidgetProps) => 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: DropdownWidgetProps) => props.labelPosition !== LabelPosition.Left, dependencies: ["labelPosition"], }, ], }, { 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: "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_TABS", 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: "Triggers an action 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: DropdownWidgetProps) => !props.serverSideFiltering, dependencies: ["serverSideFiltering"], propertyName: "onFilterUpdate", label: "onFilterUpdate", controlType: "ACTION_SELECTOR", isJSConvertible: true, isBindProperty: true, isTriggerProperty: true, }, ], }, ]; } static getDerivedPropertiesMap() { return { isValid: `{{this.isRequired ? !!this.selectedOptionValue || this.selectedOptionValue === 0 : true}}`, selectedOptionLabel: `{{(()=>{const index = _.findIndex(this.options, { value: this.value }); return this.options[index]?.label; })()}}`, selectedOptionValue: `{{(()=>{const index = _.findIndex(this.options, { value: this.value }); return this.options[index]?.value; })()}}`, }; } static getDefaultPropertiesMap(): Record { return { defaultValue: "defaultOptionValue", value: "defaultOptionValue", }; } static getMetaPropertiesMap(): Record { return { defaultValue: undefined, value: undefined, }; } static getStylesheetConfig(): Stylesheet { return { accentColor: "{{appsmith.theme.colors.primaryColor}}", borderRadius: "{{appsmith.theme.borderRadius.appBorderRadius}}", boxShadow: "none", }; } componentDidMount() { this.changeSelectedOption(); } componentDidUpdate(prevProps: DropdownWidgetProps): void { // removing selectedOptionValue if defaultValueChanges if ( prevProps.defaultOptionValue !== this.props.defaultOptionValue || prevProps.option !== this.props.option ) { this.changeSelectedOption(); } } getPageView() { const options = _.isArray(this.props.options) ? this.props.options : []; const isInvalid = "isValid" in this.props && !this.props.isValid && !!this.props.isDirty; const dropDownWidth = MinimumPopupRows * this.props.parentColumnSpace; const selectedIndex = _.findIndex(this.props.options, { value: this.props.selectedOptionValue, }); const { componentHeight, componentWidth } = this.getComponentDimensions(); return ( 1 ) } disabled={this.props.isDisabled} dropDownWidth={dropDownWidth} hasError={isInvalid} height={componentHeight} isFilterable={this.props.isFilterable} isLoading={this.props.isLoading} 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()} onFilterChange={this.onFilterChange} onOptionSelected={this.onOptionSelected} options={options} placeholder={this.props.placeholderText} selectedIndex={selectedIndex > -1 ? selectedIndex : undefined} serverSideFiltering={this.props.serverSideFiltering} widgetId={this.props.widgetId} width={componentWidth} /> ); } onOptionSelected = (selectedOption: DropdownOption) => { let isChanged = true; if (!this.props.isDirty) { this.props.updateWidgetMetaProperty("isDirty", true); } // Check if the value has changed. If no option // selected till now, there is a change if (this.props.selectedOptionValue) { isChanged = !(this.props.selectedOptionValue === selectedOption.value); } if (isChanged) { this.props.updateWidgetMetaProperty("value", selectedOption.value, { triggerPropertyName: "onOptionChange", dynamicString: this.props.onOptionChange as string, event: { type: EventType.ON_OPTION_CHANGE, }, }); } }; changeSelectedOption = () => { const index = _.findIndex(this.props.options, { value: this.props.selectedOptionValue ?? this.props.defaultOptionValue, }); const value = this.props.options?.[index]?.value; this.props.updateWidgetMetaProperty("value", value); }; onFilterChange = (value: string) => { this.props.updateWidgetMetaProperty("filterText", value); super.executeAction({ triggerPropertyName: "onFilterUpdate", dynamicString: this.props.onFilterUpdate, event: { type: EventType.ON_FILTER_UPDATE, }, }); }; static getWidgetType(): WidgetType { return "DROP_DOWN_WIDGET"; } } export interface DropdownWidgetProps extends WidgetProps { placeholderText?: string; labelText: string; labelPosition?: LabelPosition; labelAlignment?: Alignment; labelWidth?: number; selectedIndex?: number; selectedOption: DropdownOption; options?: DropdownOption[]; onOptionChange?: string; defaultOptionValue?: string; value?: string; isRequired: boolean; isFilterable: boolean; defaultValue: string; selectedOptionLabel: string; serverSideFiltering: boolean; onFilterUpdate: string; backgroundColor: string; borderRadius: string; boxShadow?: string; accentColor: string; fontFamily?: string; isDirty?: boolean; } export default DropdownWidget;