PromucFlow_constructor/app/client/src/widgets/DropdownWidget.tsx

200 lines
6.6 KiB
TypeScript
Raw Normal View History

2019-09-12 08:11:25 +00:00
import React from "react";
import BaseWidget, { WidgetProps, WidgetState } from "./BaseWidget";
2020-08-19 04:37:20 +00:00
import { WidgetType } from "constants/WidgetConstants";
2020-02-18 10:41:52 +00:00
import { EventType } from "constants/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";
2020-03-16 07:59:07 +00:00
import {
WidgetPropertyValidationType,
BASE_WIDGET_VALIDATION,
} from "utils/WidgetValidation";
2019-11-22 13:12:39 +00:00
import { VALIDATION_TYPES } from "constants/WidgetValidation";
2020-02-18 10:41:52 +00:00
import { TriggerPropertiesMap } from "utils/WidgetFactory";
2020-04-22 09:15:24 +00:00
import { Intent as BlueprintIntent } from "@blueprintjs/core";
import * as Sentry from "@sentry/react";
2020-10-06 09:01:51 +00:00
import withMeta, { WithMeta } from "./MetaHOC";
2019-09-12 08:11:25 +00:00
class DropdownWidget extends BaseWidget<DropdownWidgetProps, WidgetState> {
2019-11-22 13:12:39 +00:00
static getPropertyValidationMap(): WidgetPropertyValidationType {
return {
2020-03-16 07:59:07 +00:00
...BASE_WIDGET_VALIDATION,
2019-11-22 13:12:39 +00:00
placeholderText: VALIDATION_TYPES.TEXT,
label: VALIDATION_TYPES.TEXT,
2020-02-03 11:49:20 +00:00
options: VALIDATION_TYPES.OPTIONS_DATA,
2019-11-22 13:12:39 +00:00
selectionType: VALIDATION_TYPES.TEXT,
2020-03-06 09:45:21 +00:00
isRequired: VALIDATION_TYPES.BOOLEAN,
// onOptionChange: VALIDATION_TYPES.ACTION_SELECTOR,
2020-09-10 07:21:44 +00:00
selectedOptionValues: VALIDATION_TYPES.ARRAY,
defaultOptionValue: VALIDATION_TYPES.DEFAULT_OPTION_VALUE,
2019-11-22 13:12:39 +00:00
};
}
2020-01-17 09:28:26 +00:00
static getDerivedPropertiesMap() {
return {
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 })) }}`,
value: `{{ this.selectionType === 'SINGLE_SELECT' ? this.selectedOptionValue : this.selectedOptionValueArr }}`,
2020-09-10 07:21:44 +00:00
selectedOptionValues: `{{ this.selectedOptionValueArr }}`,
2020-01-17 09:28:26 +00:00
};
}
2020-02-11 11:03:10 +00:00
2020-02-18 10:41:52 +00:00
static getTriggerPropertyMap(): TriggerPropertiesMap {
return {
onOptionChange: true,
};
}
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,
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
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
});
const computedSelectedIndexArr = this.getSelectedOptionValueArr()
.map((opt: string) =>
_.findIndex(this.props.options, {
value: opt,
}),
)
.filter((i: number) => i > -1);
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}
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}
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) => {
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);
}
if (isChanged) {
2020-10-06 09:01:51 +00:00
this.props.updateWidgetMetaProperty(
"selectedOptionValue",
selectedOption.value,
2020-10-06 16:47:16 +00:00
{
dynamicString: this.props.onOptionChange,
event: {
type: EventType.ON_OPTION_CHANGE,
},
},
);
}
2019-10-31 05:28:11 +00:00
} else if (this.props.selectionType === "MULTI_SELECT") {
const selectedOptionValueArr = this.getSelectedOptionValueArr();
const isAlreadySelected = selectedOptionValueArr.includes(
2020-04-17 16:15:09 +00:00
selectedOption.value,
);
let newSelectedValue = [...selectedOptionValueArr];
2019-10-31 05:28:11 +00:00
if (isAlreadySelected) {
2020-04-17 16:15:09 +00:00
newSelectedValue = newSelectedValue.filter(
v => v !== selectedOption.value,
);
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) => {
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";
}
}
export type SelectionType = "SINGLE_SELECT" | "MULTI_SELECT";
2019-09-12 08:11:25 +00:00
export interface DropdownOption {
label: string;
value: 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 {
placeholderText?: string;
label?: string;
2019-10-31 05:28:11 +00:00
selectedIndex?: number;
selectedIndexArr?: number[];
selectionType: SelectionType;
2020-04-17 16:15:09 +00:00
selectedOption: DropdownOption;
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[];
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),
);