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

202 lines
6.5 KiB
TypeScript
Raw Normal View History

2019-09-12 08:11:25 +00:00
import React from "react";
import BaseWidget, { WidgetProps, WidgetState } from "./BaseWidget";
2019-11-25 05:07:27 +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/ValidationFactory";
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-03-31 03:21:35 +00:00
import { VALIDATORS } from "utils/Validators";
2020-04-20 05:42:46 +00:00
import { DataTree } from "entities/DataTree/dataTreeFactory";
2020-04-22 09:15:24 +00:00
import { Intent as BlueprintIntent } from "@blueprintjs/core";
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,
2020-03-31 10:40:52 +00:00
onOptionChange: VALIDATION_TYPES.ACTION_SELECTOR,
2020-04-17 16:15:09 +00:00
selectedOptionValueArr: VALIDATION_TYPES.ARRAY,
2020-04-20 05:42:46 +00:00
defaultOptionValue: (
value: string | string[],
props: WidgetProps,
dataTree?: DataTree,
) => {
2020-03-31 03:21:35 +00:00
let values = value;
if (props) {
if (props.selectionType === "SINGLE_SELECT") {
2020-04-20 05:42:46 +00:00
return VALIDATORS[VALIDATION_TYPES.TEXT](value, props, dataTree);
2020-03-31 03:21:35 +00:00
} else if (props.selectionType === "MULTI_SELECT") {
if (typeof value === "string") {
try {
values = JSON.parse(value);
if (!Array.isArray(values)) {
throw new Error();
}
} catch {
2020-04-17 16:15:09 +00:00
values = value.length ? value.split(",") : [];
2020-03-31 03:21:35 +00:00
if (values.length > 0) {
values = values.map(value => value.trim());
}
}
}
}
}
if (Array.isArray(values)) {
values = _.uniq(values);
}
2020-03-31 03:21:35 +00:00
return {
isValid: true,
parsed: values,
};
},
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 })) }}`,
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
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
});
2020-04-17 16:15:09 +00:00
const computedSelectedIndexArr = this.props.selectedOptionValueArr
.map((opt: string) =>
_.findIndex(this.props.options, {
value: opt,
}),
)
.filter((i: number) => i > -1);
2020-02-11 11:03:10 +00:00
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}
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-03-06 09:45:21 +00:00
label={`${this.props.label}${this.props.isRequired ? " *" : ""}`}
2019-12-03 04:41:10 +00:00
isLoading={this.props.isLoading}
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) => {
if (this.props.selectionType === "SINGLE_SELECT") {
2020-04-17 16:15:09 +00:00
this.updateWidgetMetaProperty(
"selectedOptionValue",
selectedOption.value,
);
2019-10-31 05:28:11 +00:00
} else if (this.props.selectionType === "MULTI_SELECT") {
2020-04-17 16:15:09 +00:00
console.log(this.props, selectedOption);
const isAlreadySelected = this.props.selectedOptionValueArr.includes(
selectedOption.value,
);
let newSelectedValue = [...this.props.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-04-17 16:15:09 +00:00
this.updateWidgetMetaProperty("selectedOptionValueArr", newSelectedValue);
2019-10-31 05:28:11 +00:00
}
2020-02-18 10:41:52 +00:00
if (this.props.onOptionChange) {
super.executeAction({
dynamicString: this.props.onOptionChange,
event: {
type: EventType.ON_OPTION_CHANGE,
},
});
}
2019-10-31 05:28:11 +00:00
};
onOptionRemoved = (removedIndex: number) => {
2020-04-17 16:15:09 +00:00
const newSelectedValue = this.props.selectedOptionValueArr.filter(
(v: string) =>
_.findIndex(this.props.options, { value: v }) !== removedIndex,
);
this.updateWidgetMetaProperty("selectedOptionValueArr", newSelectedValue);
2020-02-18 10:41:52 +00:00
if (this.props.onOptionChange) {
super.executeAction({
dynamicString: this.props.onOptionChange,
event: {
type: EventType.ON_OPTION_CHANGE,
},
});
}
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
}
export interface DropdownWidgetProps extends WidgetProps {
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;
2019-09-12 08:11:25 +00:00
}
export default DropdownWidget;