2021-04-12 13:32:25 +00:00
|
|
|
import React, { Component } from "react";
|
|
|
|
|
import PropTypes from "prop-types";
|
2022-04-12 20:32:12 +00:00
|
|
|
import { difference, isEqual } from "lodash";
|
2021-04-12 13:32:25 +00:00
|
|
|
import Select from "react-select";
|
|
|
|
|
import "react-select/dist/react-select.css";
|
2022-03-18 20:01:29 +00:00
|
|
|
import { v4 as uuidv4 } from "uuid";
|
2016-10-27 16:14:30 +00:00
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
import debounce from "utilities/debounce";
|
|
|
|
|
import targetInterface from "interfaces/target";
|
2016-10-27 16:14:30 +00:00
|
|
|
|
|
|
|
|
class SelectTargetsInput extends Component {
|
|
|
|
|
static propTypes = {
|
2016-11-23 21:10:32 +00:00
|
|
|
className: PropTypes.string,
|
2016-12-21 17:25:54 +00:00
|
|
|
disabled: PropTypes.bool,
|
2016-10-27 16:14:30 +00:00
|
|
|
isLoading: PropTypes.bool,
|
|
|
|
|
menuRenderer: PropTypes.func,
|
2016-11-21 15:38:23 +00:00
|
|
|
onClose: PropTypes.func,
|
2017-02-24 22:52:42 +00:00
|
|
|
onOpen: PropTypes.func,
|
|
|
|
|
onFocus: PropTypes.func,
|
2016-10-27 16:14:30 +00:00
|
|
|
onTargetSelect: PropTypes.func,
|
|
|
|
|
onTargetSelectInputChange: PropTypes.func,
|
|
|
|
|
selectedTargets: PropTypes.arrayOf(targetInterface),
|
|
|
|
|
targets: PropTypes.arrayOf(targetInterface),
|
|
|
|
|
};
|
|
|
|
|
|
2022-04-12 20:32:12 +00:00
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
uuidTargets: props.targets,
|
|
|
|
|
uuidSelectedTargets: props.selectedTargets,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// disable the eslint rule because it's the best way we can
|
|
|
|
|
// fix #4905 without rewriting code that will be replaced
|
|
|
|
|
// by the newer SelectTargets component soon.
|
|
|
|
|
/* eslint-disable react/no-did-update-set-state */
|
|
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
|
const { targets, selectedTargets } = this.props;
|
|
|
|
|
|
|
|
|
|
if (!isEqual(prevProps.targets, targets)) {
|
|
|
|
|
// must have unique key to select correctly
|
|
|
|
|
const uuidTargets = targets.map((target) => ({
|
|
|
|
|
...target,
|
|
|
|
|
uuid: uuidv4(),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
this.setState({ uuidTargets });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isEqual(prevProps.selectedTargets, selectedTargets)) {
|
|
|
|
|
// must have unique key to deselect correctly
|
|
|
|
|
const uuidSelectedTargets = selectedTargets.map((target) => ({
|
|
|
|
|
...target,
|
|
|
|
|
uuid: uuidv4(),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
this.setState({ uuidSelectedTargets });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-09 18:08:00 +00:00
|
|
|
filterOptions = (options) => {
|
|
|
|
|
const { selectedTargets } = this.props;
|
|
|
|
|
|
|
|
|
|
return difference(options, selectedTargets);
|
2021-04-12 13:32:25 +00:00
|
|
|
};
|
2016-11-09 18:08:00 +00:00
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
handleInputChange = debounce(
|
|
|
|
|
(query) => {
|
|
|
|
|
const { onTargetSelectInputChange } = this.props;
|
|
|
|
|
onTargetSelectInputChange(query);
|
|
|
|
|
},
|
|
|
|
|
{ leading: false, trailing: true }
|
|
|
|
|
);
|
2020-05-21 18:11:02 +00:00
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
render() {
|
2016-10-27 16:14:30 +00:00
|
|
|
const {
|
2016-11-23 21:10:32 +00:00
|
|
|
className,
|
2016-12-21 17:25:54 +00:00
|
|
|
disabled,
|
2016-10-27 16:14:30 +00:00
|
|
|
isLoading,
|
|
|
|
|
menuRenderer,
|
2016-11-21 15:38:23 +00:00
|
|
|
onClose,
|
2017-02-24 22:52:42 +00:00
|
|
|
onOpen,
|
|
|
|
|
onFocus,
|
2016-10-27 16:14:30 +00:00
|
|
|
onTargetSelect,
|
|
|
|
|
} = this.props;
|
2022-04-12 20:32:12 +00:00
|
|
|
const { uuidTargets, uuidSelectedTargets } = this.state;
|
2020-05-21 18:11:02 +00:00
|
|
|
const { handleInputChange } = this;
|
|
|
|
|
|
2016-10-27 16:14:30 +00:00
|
|
|
return (
|
|
|
|
|
<Select
|
2016-11-23 21:10:32 +00:00
|
|
|
className={`${className} target-select`}
|
2016-12-21 17:25:54 +00:00
|
|
|
disabled={disabled}
|
2016-10-27 16:14:30 +00:00
|
|
|
isLoading={isLoading}
|
2016-11-09 18:08:00 +00:00
|
|
|
filterOptions={this.filterOptions}
|
|
|
|
|
labelKey="display_text"
|
2016-10-27 16:14:30 +00:00
|
|
|
menuRenderer={menuRenderer}
|
|
|
|
|
multi
|
|
|
|
|
name="targets"
|
2022-03-18 20:01:29 +00:00
|
|
|
options={uuidTargets}
|
2016-10-27 16:14:30 +00:00
|
|
|
onChange={onTargetSelect}
|
2016-11-21 15:38:23 +00:00
|
|
|
onClose={onClose}
|
2017-02-24 22:52:42 +00:00
|
|
|
onOpen={onOpen}
|
|
|
|
|
onFocus={onFocus}
|
2020-05-21 18:11:02 +00:00
|
|
|
onInputChange={handleInputChange}
|
2022-04-22 07:28:11 +00:00
|
|
|
placeholder="Label name, host name, private IP address, etc."
|
2016-10-27 16:14:30 +00:00
|
|
|
resetValue={[]}
|
2016-11-23 21:10:32 +00:00
|
|
|
scrollMenuIntoView={false}
|
|
|
|
|
tabSelectsValue={false}
|
2022-03-18 20:01:29 +00:00
|
|
|
value={uuidSelectedTargets}
|
|
|
|
|
valueKey="uuid" // must be unique, target ids are not unique
|
2016-10-27 16:14:30 +00:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default SelectTargetsInput;
|