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

254 lines
8.2 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";
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,
selectedIndexArr: VALIDATION_TYPES.ARRAY,
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-03-31 03:21:35 +00:00
defaultOptionValue: (value: string | string[], props?: WidgetProps) => {
let values = value;
if (props) {
if (props.selectionType === "SINGLE_SELECT") {
return VALIDATORS[VALIDATION_TYPES.TEXT](value);
} else if (props.selectionType === "MULTI_SELECT") {
if (typeof value === "string") {
try {
values = JSON.parse(value);
if (!Array.isArray(values)) {
throw new Error();
}
} catch {
values = value.split(",");
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-01-17 09:28:26 +00:00
selectedOption: `{{
this.selectionType === 'SINGLE_SELECT'
? this.options[this.selectedIndex]
: undefined
}}`,
selectedOptionArr: `{{
this.selectionType === "MULTI_SELECT"
2020-02-18 10:41:52 +00:00
? this.options.filter((opt, index) =>
2020-01-17 09:28:26 +00:00
_.includes(this.selectedIndexArr, index),
)
: undefined
}}`,
};
}
2020-02-11 11:03:10 +00:00
2020-02-18 10:41:52 +00:00
static getTriggerPropertyMap(): TriggerPropertiesMap {
return {
onOptionChange: true,
};
}
2020-03-13 07:24:03 +00:00
componentDidMount() {
super.componentDidMount();
if (this.props.defaultOptionValue) {
2020-03-31 03:21:35 +00:00
const selectionOptions: number[] = [];
if (this.props.selectionType === "SINGLE_SELECT") {
const selectedIndex = _.findIndex(this.props.options, option => {
return (
option.value.toString() ===
(this.props.defaultOptionValue as string).toString()
);
});
if (selectedIndex > -1) {
selectionOptions.push(selectedIndex);
}
} else if (this.props.selectionType === "MULTI_SELECT") {
2020-03-31 03:21:35 +00:00
(this.props.defaultOptionValue as string[]).forEach(optionValue => {
const selectedIndex = _.findIndex(this.props.options, option => {
return option.value.toString() === optionValue.toString();
});
if (selectedIndex > -1) {
selectionOptions.push(selectedIndex);
}
});
}
if (selectionOptions.length > 0) {
this.updateWidgetMetaProperty("selectedIndex", selectionOptions[0]);
this.updateWidgetMetaProperty("selectedIndexArr", selectionOptions);
2020-03-31 03:21:35 +00:00
}
2020-03-13 07:24:03 +00:00
}
}
2020-02-14 07:48:33 +00:00
componentDidUpdate(prevProps: DropdownWidgetProps) {
super.componentDidUpdate(prevProps);
if (this.props.defaultOptionValue) {
2020-03-17 13:39:06 +00:00
if (
this.props.defaultOptionValue.toString() !==
prevProps.defaultOptionValue?.toString()
2020-03-17 13:39:06 +00:00
) {
2020-03-31 03:21:35 +00:00
const selectionOptions: number[] = [];
if (this.props.selectionType === "SINGLE_SELECT") {
const selectedIndex = _.findIndex(this.props.options, option => {
return (
option.value.toString() ===
(this.props.defaultOptionValue as string).toString()
);
});
if (selectedIndex > -1) {
selectionOptions.push(selectedIndex);
}
} else {
(this.props.defaultOptionValue as string[]).forEach(optionValue => {
const selectedIndex = _.findIndex(this.props.options, option => {
return option.value.toString() === optionValue.toString();
});
if (selectedIndex > -1) {
selectionOptions.push(selectedIndex);
}
});
}
if (selectionOptions.length > 0) {
this.updateWidgetMetaProperty("selectedIndexArr", selectionOptions);
this.updateWidgetMetaProperty("selectedIndex", selectionOptions[0]);
2020-03-17 13:39:06 +00:00
} else {
this.updateWidgetMetaProperty("selectedIndex", undefined);
2020-03-31 03:21:35 +00:00
this.updateWidgetMetaProperty("selectedIndexArr", []);
2020-03-17 13:39:06 +00:00
}
2020-03-13 07:24:03 +00:00
}
2020-02-11 11:03:10 +00:00
}
}
2019-09-12 08:11:25 +00:00
getPageView() {
2020-02-11 11:03:10 +00:00
const options = this.props.options || [];
const selectedIndexArr = this.props.selectedIndexArr || [];
let computedSelectedIndexArr = selectedIndexArr.slice();
2020-03-13 07:24:03 +00:00
selectedIndexArr.forEach(selectedIndex => {
2020-02-11 11:03:10 +00:00
if (options[selectedIndex] === undefined) {
computedSelectedIndexArr = [];
}
});
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-03-13 07:24:03 +00:00
selectedIndex={this.props.selectedIndex}
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) => {
const selectedIndex = _.findIndex(this.props.options, option => {
return option.value === selectedOption.value;
});
if (this.props.selectionType === "SINGLE_SELECT") {
2020-02-11 11:03:10 +00:00
this.updateWidgetMetaProperty("selectedIndex", selectedIndex);
2019-10-31 05:28:11 +00:00
} else if (this.props.selectionType === "MULTI_SELECT") {
const selectedIndexArr = this.props.selectedIndexArr || [];
const isAlreadySelected =
_.find(selectedIndexArr, index => {
return index === selectedIndex;
}) !== undefined;
if (isAlreadySelected) {
this.onOptionRemoved(selectedIndex);
} else {
selectedIndexArr.push(selectedIndex);
2020-02-11 11:03:10 +00:00
this.updateWidgetMetaProperty("selectedIndexArr", selectedIndexArr);
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) => {
const updateIndexArr = this.props.selectedIndexArr
? this.props.selectedIndexArr.filter(index => {
return removedIndex !== index;
})
: [];
2020-02-11 11:03:10 +00:00
this.updateWidgetMetaProperty("selectedIndexArr", updateIndexArr);
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[];
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;
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;