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-02-26 17:11:38 +00:00
|
|
|
import { connect } from "react-redux";
|
|
|
|
|
import { AppState } from "reducers";
|
|
|
|
|
import { getDynamicFetchedValues } from "selectors/formSelectors";
|
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
|
|
|
|
|
|
|
|
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-02-26 17:11:38 +00:00
|
|
|
props={{ ...this.props, width }}
|
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";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-26 17:11:38 +00:00
|
|
|
function renderDropdown(
|
|
|
|
|
props: {
|
|
|
|
|
input?: WrappedFieldInputProps;
|
|
|
|
|
meta?: Partial<WrappedFieldMetaProps>;
|
|
|
|
|
width: string;
|
|
|
|
|
} & DropDownControlProps,
|
|
|
|
|
): JSX.Element {
|
2021-12-27 12:04:45 +00:00
|
|
|
let selectedValue = props.input?.value;
|
|
|
|
|
if (_.isUndefined(props.input?.value)) {
|
2022-02-26 17:11:38 +00:00
|
|
|
selectedValue = props?.initialValue;
|
|
|
|
|
}
|
|
|
|
|
let options: DropdownOption[] = [];
|
|
|
|
|
let selectedOption = {};
|
|
|
|
|
if (typeof props.options === "object" && Array.isArray(props.options)) {
|
|
|
|
|
options = props.options;
|
|
|
|
|
selectedOption =
|
|
|
|
|
options.find(
|
|
|
|
|
(option: DropdownOption) => option.value === selectedValue,
|
|
|
|
|
) || {};
|
2021-12-27 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
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"
|
2022-01-13 08:07:30 +00:00
|
|
|
isLoading={props.isLoading}
|
2022-02-26 17:11:38 +00:00
|
|
|
isMultiSelect={props?.isMultiSelect}
|
2021-12-27 12:04:45 +00:00
|
|
|
onSelect={props.input?.onChange}
|
2022-01-14 13:50:54 +00:00
|
|
|
optionWidth={props.width}
|
2022-02-26 17:11:38 +00:00
|
|
|
options={options}
|
|
|
|
|
placeholder={props?.placeholderText}
|
2021-12-27 12:04:45 +00:00
|
|
|
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-02-26 17:11:38 +00:00
|
|
|
isLoading: boolean;
|
2020-04-28 06:52:53 +00:00
|
|
|
}
|
2022-01-06 10:03:20 +00:00
|
|
|
|
2022-02-26 17:11:38 +00:00
|
|
|
const mapStateToProps = (
|
|
|
|
|
state: AppState,
|
|
|
|
|
ownProps: DropDownControlProps,
|
|
|
|
|
): { isLoading: boolean; options: DropdownOption[] } => {
|
|
|
|
|
// Added default options to prevent error when options is undefined
|
|
|
|
|
let isLoading = false;
|
|
|
|
|
let options: DropdownOption[] = ownProps.fetchOptionsCondtionally
|
|
|
|
|
? []
|
|
|
|
|
: ownProps.options;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (ownProps.fetchOptionsCondtionally) {
|
|
|
|
|
const dynamicFetchedValues = getDynamicFetchedValues(
|
|
|
|
|
state,
|
|
|
|
|
ownProps.configProperty,
|
|
|
|
|
);
|
|
|
|
|
isLoading = dynamicFetchedValues.isLoading;
|
|
|
|
|
options = dynamicFetchedValues.data;
|
|
|
|
|
}
|
|
|
|
|
return { isLoading, options };
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return {
|
|
|
|
|
isLoading,
|
|
|
|
|
options,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Connecting this componenet to the state to allow for dynamic fetching of options to be updated.
|
|
|
|
|
export default connect(mapStateToProps)(DropDownControl);
|