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";
|
2020-04-28 06:52:53 +00:00
|
|
|
|
|
|
|
|
const DropdownSelect = styled.div`
|
|
|
|
|
font-size: 14px;
|
|
|
|
|
width: 50vh;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
class DropDownControl extends BaseControl<DropDownControlProps> {
|
|
|
|
|
render() {
|
2021-11-10 13:45:47 +00:00
|
|
|
let width = "50vh";
|
2021-12-27 12:04:45 +00:00
|
|
|
if (this.props.customStyles && this.props?.customStyles?.width) {
|
|
|
|
|
width = this.props?.customStyles?.width;
|
2021-11-10 13:45:47 +00:00
|
|
|
}
|
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}
|
|
|
|
|
options={this.props.options}
|
|
|
|
|
props={{ ...this.props, width }}
|
|
|
|
|
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;
|
|
|
|
|
props: DropDownControlProps & { width?: string };
|
|
|
|
|
options: { label: string; value: string }[];
|
|
|
|
|
}): JSX.Element {
|
|
|
|
|
let selectedValue = props.input?.value;
|
|
|
|
|
if (_.isUndefined(props.input?.value)) {
|
|
|
|
|
selectedValue = props?.props?.initialValue;
|
|
|
|
|
}
|
|
|
|
|
const selectedOption =
|
|
|
|
|
props?.options.find(
|
|
|
|
|
(option: DropdownOption) => option.value === selectedValue,
|
|
|
|
|
) || {};
|
|
|
|
|
return (
|
|
|
|
|
<Dropdown
|
|
|
|
|
boundary="window"
|
|
|
|
|
dontUsePortal={false}
|
|
|
|
|
dropdownMaxHeight="250px"
|
|
|
|
|
errorMsg={props.props?.errorText}
|
|
|
|
|
helperText={props.props?.info}
|
|
|
|
|
isMultiSelect={props?.props?.isMultiSelect}
|
|
|
|
|
onSelect={props.input?.onChange}
|
|
|
|
|
optionWidth="50vh"
|
|
|
|
|
options={props.options}
|
|
|
|
|
placeholder={props.props?.placeholderText}
|
|
|
|
|
selected={selectedOption}
|
|
|
|
|
showLabelOnly
|
|
|
|
|
width={props?.props?.width ? props?.props?.width : "50vh"}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
isDisabled?: boolean;
|
|
|
|
|
isSearchable?: boolean;
|
2020-04-28 06:52:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default DropDownControl;
|