2019-09-12 08:11:25 +00:00
|
|
|
import React from "react";
|
2019-09-13 10:45:49 +00:00
|
|
|
import BaseWidget, { WidgetProps, WidgetState } from "./BaseWidget";
|
2019-11-25 05:07:27 +00:00
|
|
|
import { WidgetType } from "constants/WidgetConstants";
|
|
|
|
|
import { ActionPayload } from "constants/ActionConstants";
|
|
|
|
|
import DropDownComponent from "components/designSystems/blueprint/DropdownComponent";
|
2019-10-31 05:28:11 +00:00
|
|
|
import _ from "lodash";
|
2019-11-22 13:12:39 +00:00
|
|
|
import { WidgetPropertyValidationType } from "utils/ValidationFactory";
|
|
|
|
|
import { VALIDATION_TYPES } from "constants/WidgetValidation";
|
2019-09-12 08:11:25 +00:00
|
|
|
|
2020-01-17 09:28:26 +00:00
|
|
|
export interface DropDownDerivedProps {
|
|
|
|
|
selectedOption?: DropdownOption;
|
|
|
|
|
selectedOptionArr?: DropdownOption[];
|
|
|
|
|
}
|
2019-09-13 10:45:49 +00:00
|
|
|
class DropdownWidget extends BaseWidget<DropdownWidgetProps, WidgetState> {
|
2019-11-22 13:12:39 +00:00
|
|
|
static getPropertyValidationMap(): WidgetPropertyValidationType {
|
|
|
|
|
return {
|
|
|
|
|
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,
|
|
|
|
|
selectedIndex: VALIDATION_TYPES.NUMBER,
|
|
|
|
|
selectedIndexArr: VALIDATION_TYPES.ARRAY,
|
|
|
|
|
};
|
|
|
|
|
}
|
2020-01-17 09:28:26 +00:00
|
|
|
static getDerivedPropertiesMap() {
|
|
|
|
|
return {
|
|
|
|
|
selectedOption: `{{
|
|
|
|
|
this.selectionType === 'SINGLE_SELECT'
|
|
|
|
|
? this.options[this.selectedIndex]
|
|
|
|
|
: undefined
|
|
|
|
|
}}`,
|
|
|
|
|
selectedOptionArr: `{{
|
|
|
|
|
const options = this.options || [];
|
|
|
|
|
this.selectionType === "MULTI_SELECT"
|
|
|
|
|
? options.filter((opt, index) =>
|
|
|
|
|
_.includes(this.selectedIndexArr, index),
|
|
|
|
|
)
|
|
|
|
|
: undefined
|
|
|
|
|
}}`,
|
|
|
|
|
};
|
|
|
|
|
}
|
2020-02-11 11:03:10 +00:00
|
|
|
|
2020-02-14 07:48:33 +00:00
|
|
|
componentDidUpdate(prevProps: DropdownWidgetProps) {
|
|
|
|
|
super.componentDidUpdate(prevProps);
|
2020-02-11 11:03:10 +00:00
|
|
|
if (
|
2020-02-14 07:48:33 +00:00
|
|
|
JSON.stringify(prevProps.options) !== JSON.stringify(this.props.options)
|
2020-02-11 11:03:10 +00:00
|
|
|
) {
|
|
|
|
|
this.updateWidgetMetaProperty("selectedIndex", undefined);
|
|
|
|
|
this.updateWidgetMetaProperty("selectedIndexArr", []);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-12 08:11:25 +00:00
|
|
|
getPageView() {
|
2020-02-11 11:03:10 +00:00
|
|
|
const options = this.props.options || [];
|
|
|
|
|
let selectedIndex: number | undefined = undefined;
|
|
|
|
|
if (
|
|
|
|
|
this.props.selectedIndex !== undefined &&
|
|
|
|
|
this.props.selectedIndex < options.length &&
|
|
|
|
|
this.props.selectedIndex >= 0
|
|
|
|
|
) {
|
|
|
|
|
selectedIndex = this.props.selectedIndex;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const selectedIndexArr = this.props.selectedIndexArr || [];
|
|
|
|
|
let computedSelectedIndexArr = selectedIndexArr.slice();
|
|
|
|
|
selectedIndexArr.forEach((selectedIndex, index) => {
|
|
|
|
|
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-02-11 11:03:10 +00:00
|
|
|
selectedIndex={selectedIndex}
|
|
|
|
|
selectedIndexArr={computedSelectedIndexArr}
|
2019-10-31 05:28:11 +00:00
|
|
|
label={this.props.label}
|
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
|
|
|
}
|
|
|
|
|
}
|
2019-11-06 12:12:41 +00:00
|
|
|
super.executeAction(this.props.onOptionChange);
|
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);
|
2019-11-06 12:12:41 +00:00
|
|
|
super.executeAction(this.props.onOptionChange);
|
2019-10-31 05:28:11 +00:00
|
|
|
};
|
|
|
|
|
|
2019-09-12 08:11:25 +00:00
|
|
|
getWidgetType(): WidgetType {
|
|
|
|
|
return "DROP_DOWN_WIDGET";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-17 10:09:00 +00:00
|
|
|
export type SelectionType = "SINGLE_SELECT" | "MULTI_SELECT";
|
2019-09-12 08:11:25 +00:00
|
|
|
export interface DropdownOption {
|
2019-09-13 10:45:49 +00:00
|
|
|
label: string;
|
|
|
|
|
value: string;
|
2019-09-12 08:11:25 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-13 10:45:49 +00:00
|
|
|
export interface DropdownWidgetProps extends WidgetProps {
|
2019-09-19 11:29:24 +00:00
|
|
|
placeholderText?: string;
|
2019-09-13 10:45:49 +00:00
|
|
|
label?: string;
|
2019-10-31 05:28:11 +00:00
|
|
|
selectedIndex?: number;
|
|
|
|
|
selectedIndexArr?: number[];
|
2019-09-17 10:09:00 +00:00
|
|
|
selectionType: SelectionType;
|
2019-09-13 10:45:49 +00:00
|
|
|
options?: DropdownOption[];
|
2019-11-06 12:12:41 +00:00
|
|
|
onOptionChange?: ActionPayload[];
|
2019-09-12 08:11:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default DropdownWidget;
|