2020-04-28 06:52:53 +00:00
|
|
|
import React from "react";
|
|
|
|
|
import BaseControl, { ControlProps } from "./BaseControl";
|
|
|
|
|
import styled from "styled-components";
|
2021-12-27 12:04:45 +00:00
|
|
|
import Dropdown, { DropdownOption } from "components/ads/Dropdown";
|
2020-04-28 06:52:53 +00:00
|
|
|
import { ControlType } from "constants/PropertyControlConstants";
|
2021-12-27 12:04:45 +00:00
|
|
|
import _ from "lodash";
|
|
|
|
|
import {
|
|
|
|
|
Field,
|
|
|
|
|
WrappedFieldInputProps,
|
|
|
|
|
WrappedFieldMetaProps,
|
|
|
|
|
} from "redux-form";
|
2022-01-13 08:07:30 +00:00
|
|
|
import { DynamicValues } from "reducers/evaluationReducers/formEvaluationReducer";
|
2020-04-28 06:52:53 +00:00
|
|
|
|
|
|
|
|
const DropdownSelect = styled.div`
|
|
|
|
|
font-size: 14px;
|
2022-01-14 13:50:54 +00:00
|
|
|
width: 20vw;
|
2020-04-28 06:52:53 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
class DropDownControl extends BaseControl<DropDownControlProps> {
|
|
|
|
|
render() {
|
2022-01-14 13:50:54 +00:00
|
|
|
let width = "20vw";
|
|
|
|
|
if (
|
|
|
|
|
"customStyles" in this.props &&
|
|
|
|
|
!!this.props.customStyles &&
|
|
|
|
|
"width" in this.props.customStyles
|
|
|
|
|
) {
|
|
|
|
|
width = this.props.customStyles.width;
|
2021-11-10 13:45:47 +00:00
|
|
|
}
|
2020-04-28 06:52:53 +00:00
|
|
|
|
2022-01-13 08:07:30 +00:00
|
|
|
// Options will be set dynamically if the config has fetchOptionsConditionally set to true
|
|
|
|
|
let options = this.props.options;
|
|
|
|
|
let isLoading = false;
|
|
|
|
|
if (
|
|
|
|
|
this.props.fetchOptionsCondtionally &&
|
|
|
|
|
!!this.props.dynamicFetchedValues
|
|
|
|
|
) {
|
|
|
|
|
options = this.props.dynamicFetchedValues.data;
|
|
|
|
|
isLoading = this.props.dynamicFetchedValues.isLoading;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-28 06:52:53 +00:00
|
|
|
return (
|
2021-12-27 12:04:45 +00:00
|
|
|
<DropdownSelect data-cy={this.props.configProperty} style={{ width }}>
|
|
|
|
|
<Field
|
|
|
|
|
component={renderDropdown}
|
|
|
|
|
name={this.props.configProperty}
|
2022-01-13 08:07:30 +00:00
|
|
|
props={{ ...this.props, width, isLoading, options }} // Passing options and isLoading in props allows the component to get the updated values
|
2021-12-27 12:04:45 +00:00
|
|
|
type={this.props?.isMultiSelect ? "select-multiple" : undefined}
|
|
|
|
|
/>
|
|
|
|
|
</DropdownSelect>
|
2020-04-28 06:52:53 +00:00
|
|
|
);
|
2021-12-27 12:04:45 +00:00
|
|
|
}
|
2020-04-28 06:52:53 +00:00
|
|
|
|
|
|
|
|
getControlType(): ControlType {
|
|
|
|
|
return "DROP_DOWN";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-27 12:04:45 +00:00
|
|
|
function renderDropdown(props: {
|
|
|
|
|
input?: WrappedFieldInputProps;
|
|
|
|
|
meta?: WrappedFieldMetaProps;
|
2022-01-14 13:50:54 +00:00
|
|
|
props: DropDownControlProps;
|
|
|
|
|
width: string;
|
2022-01-06 10:03:20 +00:00
|
|
|
formName: string;
|
2022-01-13 08:07:30 +00:00
|
|
|
isLoading?: boolean;
|
|
|
|
|
options: DropdownOption[];
|
2022-01-06 14:39:21 +00:00
|
|
|
disabled?: boolean;
|
2021-12-27 12:04:45 +00:00
|
|
|
}): JSX.Element {
|
|
|
|
|
let selectedValue = props.input?.value;
|
|
|
|
|
if (_.isUndefined(props.input?.value)) {
|
|
|
|
|
selectedValue = props?.props?.initialValue;
|
|
|
|
|
}
|
2022-01-13 08:07:30 +00:00
|
|
|
|
2021-12-27 12:04:45 +00:00
|
|
|
const selectedOption =
|
2022-01-13 08:07:30 +00:00
|
|
|
props.options.find(
|
2021-12-27 12:04:45 +00:00
|
|
|
(option: DropdownOption) => option.value === selectedValue,
|
|
|
|
|
) || {};
|
|
|
|
|
return (
|
|
|
|
|
<Dropdown
|
|
|
|
|
boundary="window"
|
2022-01-06 14:39:21 +00:00
|
|
|
disabled={props.disabled}
|
2021-12-27 12:04:45 +00:00
|
|
|
dontUsePortal={false}
|
|
|
|
|
dropdownMaxHeight="250px"
|
|
|
|
|
errorMsg={props.props?.errorText}
|
|
|
|
|
helperText={props.props?.info}
|
2022-01-13 08:07:30 +00:00
|
|
|
isLoading={props.isLoading}
|
2021-12-27 12:04:45 +00:00
|
|
|
isMultiSelect={props?.props?.isMultiSelect}
|
|
|
|
|
onSelect={props.input?.onChange}
|
2022-01-14 13:50:54 +00:00
|
|
|
optionWidth={props.width}
|
2022-01-13 08:07:30 +00:00
|
|
|
options={props.options}
|
2021-12-27 12:04:45 +00:00
|
|
|
placeholder={props.props?.placeholderText}
|
|
|
|
|
selected={selectedOption}
|
|
|
|
|
showLabelOnly
|
2022-01-14 13:50:54 +00:00
|
|
|
width={props.width}
|
2021-12-27 12:04:45 +00:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-28 06:52:53 +00:00
|
|
|
export interface DropDownControlProps extends ControlProps {
|
|
|
|
|
options: DropdownOption[];
|
|
|
|
|
placeholderText: string;
|
|
|
|
|
propertyValue: string;
|
2021-02-26 06:58:47 +00:00
|
|
|
subtitle?: string;
|
2021-12-27 12:04:45 +00:00
|
|
|
isMultiSelect?: boolean;
|
2021-11-10 13:45:47 +00:00
|
|
|
isSearchable?: boolean;
|
2022-01-06 10:03:20 +00:00
|
|
|
fetchOptionsCondtionally?: boolean;
|
2022-01-13 08:07:30 +00:00
|
|
|
dynamicFetchedValues?: DynamicValues;
|
2020-04-28 06:52:53 +00:00
|
|
|
}
|
2022-01-06 10:03:20 +00:00
|
|
|
|
2022-01-13 08:07:30 +00:00
|
|
|
export default DropDownControl;
|