2021-04-12 13:32:25 +00:00
|
|
|
import React, { Component } from "react";
|
|
|
|
|
import PropTypes from "prop-types";
|
|
|
|
|
import { difference } from "lodash";
|
|
|
|
|
import Select from "react-select";
|
|
|
|
|
import "react-select/dist/react-select.css";
|
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),
|
|
|
|
|
};
|
|
|
|
|
|
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,
|
|
|
|
|
selectedTargets,
|
|
|
|
|
targets,
|
|
|
|
|
} = this.props;
|
|
|
|
|
|
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"
|
|
|
|
|
options={targets}
|
|
|
|
|
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}
|
2016-10-27 16:14:30 +00:00
|
|
|
placeholder="Label Name, Host Name, IP Address, etc."
|
|
|
|
|
resetValue={[]}
|
2016-11-23 21:10:32 +00:00
|
|
|
scrollMenuIntoView={false}
|
|
|
|
|
tabSelectsValue={false}
|
2016-10-27 16:14:30 +00:00
|
|
|
value={selectedTargets}
|
2021-04-19 22:14:43 +00:00
|
|
|
valueKey="id"
|
2016-10-27 16:14:30 +00:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default SelectTargetsInput;
|