2021-04-12 13:32:25 +00:00
|
|
|
|
import React, { Component } from "react";
|
|
|
|
|
|
import PropTypes from "prop-types";
|
|
|
|
|
|
import classnames from "classnames";
|
|
|
|
|
|
import { noop, pick } from "lodash";
|
|
|
|
|
|
import Select from "react-select";
|
2016-10-19 20:22:18 +00:00
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
|
import dropdownOptionInterface from "interfaces/dropdownOption";
|
|
|
|
|
|
import FormField from "components/forms/FormField";
|
2023-08-07 13:25:32 +00:00
|
|
|
|
import Icon from "components/Icon";
|
2024-04-18 21:34:20 +00:00
|
|
|
|
import DropdownOptionTooltipWrapper from "./DropdownOptionTooltipWrapper";
|
2016-12-16 15:54:49 +00:00
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
|
const baseClass = "dropdown";
|
2016-10-03 17:54:22 +00:00
|
|
|
|
|
|
|
|
|
|
class Dropdown extends Component {
|
|
|
|
|
|
static propTypes = {
|
2016-11-03 19:40:54 +00:00
|
|
|
|
className: PropTypes.string,
|
2016-12-21 17:25:54 +00:00
|
|
|
|
clearable: PropTypes.bool,
|
2021-03-24 13:18:56 +00:00
|
|
|
|
searchable: PropTypes.bool,
|
2017-01-13 23:27:58 +00:00
|
|
|
|
disabled: PropTypes.bool,
|
2016-12-16 15:54:49 +00:00
|
|
|
|
error: PropTypes.string,
|
2016-12-21 17:25:54 +00:00
|
|
|
|
label: PropTypes.oneOfType([PropTypes.array, PropTypes.string]),
|
|
|
|
|
|
labelClassName: PropTypes.string,
|
2017-01-24 23:52:48 +00:00
|
|
|
|
multi: PropTypes.bool,
|
2016-12-21 17:25:54 +00:00
|
|
|
|
name: PropTypes.string,
|
|
|
|
|
|
onChange: PropTypes.func,
|
2021-10-26 14:24:16 +00:00
|
|
|
|
onOpen: PropTypes.func,
|
|
|
|
|
|
onClose: PropTypes.func,
|
2016-12-21 17:25:54 +00:00
|
|
|
|
options: PropTypes.arrayOf(dropdownOptionInterface).isRequired,
|
|
|
|
|
|
placeholder: PropTypes.oneOfType([PropTypes.array, PropTypes.string]),
|
2024-09-03 22:35:33 +00:00
|
|
|
|
/**
|
|
|
|
|
|
value must correspond to the value of a dropdown option to render
|
|
|
|
|
|
e.g. with options:
|
|
|
|
|
|
|
|
|
|
|
|
[
|
|
|
|
|
|
{
|
|
|
|
|
|
label: "Display name",
|
|
|
|
|
|
value: 1, <– the id of the thing
|
|
|
|
|
|
}
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
set value to 1, not "Display name"
|
|
|
|
|
|
*/
|
2021-05-12 13:06:39 +00:00
|
|
|
|
value: PropTypes.oneOfType([
|
|
|
|
|
|
PropTypes.array,
|
|
|
|
|
|
PropTypes.string,
|
|
|
|
|
|
PropTypes.number,
|
|
|
|
|
|
]),
|
2016-12-21 17:25:54 +00:00
|
|
|
|
wrapperClassName: PropTypes.string,
|
2022-01-21 17:06:58 +00:00
|
|
|
|
parseTarget: PropTypes.bool,
|
2022-02-28 21:25:06 +00:00
|
|
|
|
tooltip: PropTypes.string,
|
2023-01-06 14:25:00 +00:00
|
|
|
|
autoFocus: PropTypes.bool,
|
2024-09-17 17:10:34 +00:00
|
|
|
|
/** Includes styled icon */
|
|
|
|
|
|
iconName: PropTypes.string,
|
2024-01-18 15:48:44 +00:00
|
|
|
|
helpText: PropTypes.oneOfType([
|
|
|
|
|
|
PropTypes.string,
|
|
|
|
|
|
PropTypes.arrayOf(PropTypes.string),
|
|
|
|
|
|
PropTypes.object,
|
|
|
|
|
|
]),
|
2016-10-03 17:54:22 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static defaultProps = {
|
2016-12-16 15:54:49 +00:00
|
|
|
|
onChange: noop,
|
2021-10-26 14:24:16 +00:00
|
|
|
|
onOpen: noop,
|
|
|
|
|
|
onClose: noop,
|
2016-11-09 14:26:15 +00:00
|
|
|
|
clearable: false,
|
2021-03-24 13:18:56 +00:00
|
|
|
|
searchable: true,
|
2017-01-13 23:27:58 +00:00
|
|
|
|
disabled: false,
|
2017-01-24 23:52:48 +00:00
|
|
|
|
multi: false,
|
2021-04-12 13:32:25 +00:00
|
|
|
|
name: "targets",
|
2022-10-14 20:21:30 +00:00
|
|
|
|
placeholder: "Select one...", // if value undefined
|
2022-01-21 17:06:58 +00:00
|
|
|
|
parseTarget: false,
|
2022-02-28 21:25:06 +00:00
|
|
|
|
tooltip: "",
|
2023-01-06 14:25:00 +00:00
|
|
|
|
autoFocus: false,
|
2024-09-17 17:10:34 +00:00
|
|
|
|
iconName: "",
|
2016-10-03 17:54:22 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
2021-10-26 14:24:16 +00:00
|
|
|
|
onMenuOpen = () => {
|
|
|
|
|
|
const { onOpen } = this.props;
|
|
|
|
|
|
onOpen();
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
onMenuClose = () => {
|
|
|
|
|
|
const { onClose } = this.props;
|
|
|
|
|
|
onClose();
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2017-01-24 23:52:48 +00:00
|
|
|
|
handleChange = (selected) => {
|
2022-01-21 17:06:58 +00:00
|
|
|
|
const { multi, onChange, clearable, name, parseTarget } = this.props;
|
|
|
|
|
|
|
|
|
|
|
|
if (parseTarget) {
|
2024-09-03 22:35:33 +00:00
|
|
|
|
// Returns both name of the Dropdown and value of the selected option
|
2022-01-21 17:06:58 +00:00
|
|
|
|
return onChange({ value: selected.value, name });
|
|
|
|
|
|
}
|
2016-12-16 15:54:49 +00:00
|
|
|
|
|
2021-05-13 14:30:42 +00:00
|
|
|
|
if (clearable && selected === null) {
|
2022-03-10 15:10:44 +00:00
|
|
|
|
return onChange(null);
|
2022-01-21 17:06:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (multi) {
|
2022-03-10 15:10:44 +00:00
|
|
|
|
return onChange(selected.map((obj) => obj.value).join(","));
|
2017-01-24 23:52:48 +00:00
|
|
|
|
}
|
2022-01-21 17:06:58 +00:00
|
|
|
|
|
2022-03-10 15:10:44 +00:00
|
|
|
|
return onChange(selected.value);
|
2016-12-16 15:54:49 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
2016-12-21 17:25:54 +00:00
|
|
|
|
renderLabel = () => {
|
|
|
|
|
|
const { error, label, labelClassName, name } = this.props;
|
|
|
|
|
|
const labelWrapperClasses = classnames(
|
|
|
|
|
|
`${baseClass}__label`,
|
|
|
|
|
|
labelClassName,
|
2021-04-12 13:32:25 +00:00
|
|
|
|
{ [`${baseClass}__label--error`]: error }
|
2016-12-21 17:25:54 +00:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
if (!label) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
2021-04-12 13:32:25 +00:00
|
|
|
|
<label className={labelWrapperClasses} htmlFor={name}>
|
2016-12-21 17:25:54 +00:00
|
|
|
|
{error || label}
|
|
|
|
|
|
</label>
|
|
|
|
|
|
);
|
2021-04-12 13:32:25 +00:00
|
|
|
|
};
|
2016-12-21 17:25:54 +00:00
|
|
|
|
|
2020-12-21 23:47:35 +00:00
|
|
|
|
renderOption = (option) => {
|
2024-04-18 21:34:20 +00:00
|
|
|
|
if (option.tooltipContent) {
|
2024-04-12 13:10:19 +00:00
|
|
|
|
return (
|
2024-04-18 21:34:20 +00:00
|
|
|
|
<DropdownOptionTooltipWrapper tipContent={option.tooltipContent}>
|
2024-04-12 13:10:19 +00:00
|
|
|
|
<div className={`${baseClass}__option`}>
|
|
|
|
|
|
{option.label}
|
|
|
|
|
|
{option.helpText && (
|
|
|
|
|
|
<span className={`${baseClass}__help-text`}>
|
|
|
|
|
|
{option.helpText}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
2024-04-18 21:34:20 +00:00
|
|
|
|
</DropdownOptionTooltipWrapper>
|
2024-04-12 13:10:19 +00:00
|
|
|
|
);
|
|
|
|
|
|
}
|
2020-12-21 23:47:35 +00:00
|
|
|
|
return (
|
|
|
|
|
|
<div className={`${baseClass}__option`}>
|
|
|
|
|
|
{option.label}
|
2021-04-12 13:32:25 +00:00
|
|
|
|
{option.helpText && (
|
|
|
|
|
|
<span className={`${baseClass}__help-text`}>{option.helpText}</span>
|
|
|
|
|
|
)}
|
2020-12-21 23:47:35 +00:00
|
|
|
|
</div>
|
|
|
|
|
|
);
|
2021-04-12 13:32:25 +00:00
|
|
|
|
};
|
2020-12-21 23:47:35 +00:00
|
|
|
|
|
2023-08-07 13:25:32 +00:00
|
|
|
|
renderCustomDropdownArrow = () => {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div className={`${baseClass}__custom-arrow`}>
|
2023-10-31 16:06:38 +00:00
|
|
|
|
<Icon name="chevron-down" className={`${baseClass}__icon`} />
|
2023-08-07 13:25:32 +00:00
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2024-09-17 17:10:34 +00:00
|
|
|
|
// Adds styled icon to dropdown
|
|
|
|
|
|
renderWithIcon = () => {
|
|
|
|
|
|
const { options, value, iconName } = this.props;
|
2023-08-09 17:35:28 +00:00
|
|
|
|
const customLabel = options
|
|
|
|
|
|
.filter((option) => option.value === value)
|
|
|
|
|
|
.map((option) => option.label);
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div className={`${baseClass}__custom-value`}>
|
2024-09-17 17:10:34 +00:00
|
|
|
|
<Icon name={iconName} className={`${baseClass}__icon`} />
|
2023-08-09 17:35:28 +00:00
|
|
|
|
<div className={`${baseClass}__custom-value-label`}>{customLabel}</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
|
render() {
|
2023-08-07 13:25:32 +00:00
|
|
|
|
const {
|
|
|
|
|
|
handleChange,
|
|
|
|
|
|
renderOption,
|
|
|
|
|
|
onMenuOpen,
|
|
|
|
|
|
onMenuClose,
|
|
|
|
|
|
renderCustomDropdownArrow,
|
2024-09-17 17:10:34 +00:00
|
|
|
|
renderWithIcon,
|
2023-08-07 13:25:32 +00:00
|
|
|
|
} = this;
|
2021-04-12 13:32:25 +00:00
|
|
|
|
const {
|
|
|
|
|
|
error,
|
|
|
|
|
|
className,
|
|
|
|
|
|
clearable,
|
|
|
|
|
|
disabled,
|
|
|
|
|
|
multi,
|
|
|
|
|
|
name,
|
|
|
|
|
|
options,
|
|
|
|
|
|
placeholder,
|
|
|
|
|
|
value,
|
|
|
|
|
|
wrapperClassName,
|
2021-04-14 16:52:15 +00:00
|
|
|
|
searchable,
|
2023-01-06 14:25:00 +00:00
|
|
|
|
autoFocus,
|
2024-09-17 17:10:34 +00:00
|
|
|
|
iconName,
|
2021-04-12 13:32:25 +00:00
|
|
|
|
} = this.props;
|
2016-12-16 15:54:49 +00:00
|
|
|
|
|
2022-02-28 21:25:06 +00:00
|
|
|
|
const formFieldProps = pick(this.props, [
|
2024-01-18 15:48:44 +00:00
|
|
|
|
"helpText",
|
2022-02-28 21:25:06 +00:00
|
|
|
|
"label",
|
|
|
|
|
|
"error",
|
|
|
|
|
|
"name",
|
|
|
|
|
|
"tooltip",
|
2024-08-20 13:41:49 +00:00
|
|
|
|
"disabled",
|
2022-02-28 21:25:06 +00:00
|
|
|
|
]);
|
2017-01-06 15:36:51 +00:00
|
|
|
|
const selectClasses = classnames(className, `${baseClass}__select`, {
|
|
|
|
|
|
[`${baseClass}__select--error`]: error,
|
2024-08-20 13:41:49 +00:00
|
|
|
|
[`${baseClass}__select--disabled`]: disabled,
|
2017-01-06 15:36:51 +00:00
|
|
|
|
});
|
2016-10-03 17:54:22 +00:00
|
|
|
|
|
|
|
|
|
|
return (
|
2021-04-12 13:32:25 +00:00
|
|
|
|
<FormField
|
|
|
|
|
|
{...formFieldProps}
|
|
|
|
|
|
type="dropdown"
|
|
|
|
|
|
className={wrapperClassName}
|
|
|
|
|
|
>
|
2016-12-16 15:54:49 +00:00
|
|
|
|
<Select
|
2017-01-06 15:36:51 +00:00
|
|
|
|
className={selectClasses}
|
2016-12-21 17:25:54 +00:00
|
|
|
|
clearable={clearable}
|
2017-01-13 23:27:58 +00:00
|
|
|
|
disabled={disabled}
|
2017-01-24 23:52:48 +00:00
|
|
|
|
multi={multi}
|
2021-03-24 13:18:56 +00:00
|
|
|
|
searchable={searchable}
|
2016-12-23 18:40:16 +00:00
|
|
|
|
name={`${name}-select`}
|
2016-12-16 15:54:49 +00:00
|
|
|
|
onChange={handleChange}
|
2016-12-21 17:25:54 +00:00
|
|
|
|
options={options}
|
2020-12-21 23:47:35 +00:00
|
|
|
|
optionRenderer={renderOption}
|
2016-12-16 15:54:49 +00:00
|
|
|
|
placeholder={placeholder}
|
|
|
|
|
|
value={value}
|
2021-10-26 14:24:16 +00:00
|
|
|
|
onOpen={onMenuOpen}
|
|
|
|
|
|
onClose={onMenuClose}
|
2023-01-06 14:25:00 +00:00
|
|
|
|
autoFocus={autoFocus}
|
2023-08-07 13:25:32 +00:00
|
|
|
|
arrowRenderer={renderCustomDropdownArrow}
|
2024-09-17 17:10:34 +00:00
|
|
|
|
valueComponent={iconName ? renderWithIcon : undefined}
|
2016-12-16 15:54:49 +00:00
|
|
|
|
/>
|
|
|
|
|
|
</FormField>
|
2016-10-03 17:54:22 +00:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-11-03 19:40:54 +00:00
|
|
|
|
export default Dropdown;
|