2019-01-07 01:25:33 +00:00
|
|
|
import React from 'react';
|
|
|
|
|
import PropTypes from 'prop-types';
|
2016-11-14 17:32:13 +00:00
|
|
|
import classNames from 'classnames';
|
|
|
|
|
import { filter, includes, isEqual, noop } from 'lodash';
|
|
|
|
|
|
|
|
|
|
import targetInterface from 'interfaces/target';
|
2016-11-21 15:38:23 +00:00
|
|
|
import TargetDetails from '../TargetDetails';
|
2016-12-05 22:48:46 +00:00
|
|
|
import { targetFilter } from './helpers';
|
2016-11-21 15:38:23 +00:00
|
|
|
import TargetOption from '../TargetOption';
|
2016-11-14 17:32:13 +00:00
|
|
|
|
2016-11-23 21:10:32 +00:00
|
|
|
const baseClass = 'target-list';
|
2016-11-14 17:32:13 +00:00
|
|
|
|
2016-11-23 21:10:32 +00:00
|
|
|
const SelectTargetsMenuWrapper = (onMoreInfoClick, moreInfoTarget, handleBackToResults) => {
|
2016-11-14 17:32:13 +00:00
|
|
|
const SelectTargetsMenu = ({
|
|
|
|
|
focusedOption,
|
|
|
|
|
instancePrefix,
|
|
|
|
|
onFocus,
|
|
|
|
|
onSelect,
|
|
|
|
|
optionClassName,
|
|
|
|
|
optionComponent,
|
|
|
|
|
options,
|
|
|
|
|
valueArray = [],
|
|
|
|
|
valueKey,
|
|
|
|
|
onOptionRef,
|
|
|
|
|
}) => {
|
|
|
|
|
const Option = optionComponent;
|
2016-11-23 21:10:32 +00:00
|
|
|
|
2016-11-14 17:32:13 +00:00
|
|
|
const renderTargets = (targetType) => {
|
2016-12-05 22:48:46 +00:00
|
|
|
const targets = filter(options, targetFilter(targetType));
|
2017-03-02 16:52:31 +00:00
|
|
|
const targetsOutput = [];
|
|
|
|
|
const targetTitle = targetType === 'all' ? 'all hosts' : targetType;
|
|
|
|
|
|
|
|
|
|
targetsOutput.push(<p className={`${baseClass}__type`} key={`type-${targetType}-key`}>{targetTitle}</p>);
|
2016-11-14 17:32:13 +00:00
|
|
|
|
2016-11-23 21:10:32 +00:00
|
|
|
if (targets.length === 0) {
|
2017-03-02 16:52:31 +00:00
|
|
|
if (targetType === 'all') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
targetsOutput.push(<span className={`${baseClass}__not-found`} key={`${targetType}-notfound`}>Unable to find any matching {targetType}.</span>);
|
|
|
|
|
|
|
|
|
|
return targetsOutput;
|
2016-11-23 21:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
2017-03-02 16:52:31 +00:00
|
|
|
targetsOutput.push(targets.map((target, index) => {
|
2016-11-14 17:32:13 +00:00
|
|
|
const { disabled: isDisabled } = target;
|
|
|
|
|
const isSelected = includes(valueArray, target);
|
|
|
|
|
const isFocused = isEqual(focusedOption, target);
|
|
|
|
|
const className = classNames(optionClassName, {
|
|
|
|
|
'Select-option': true,
|
|
|
|
|
'is-selected': isSelected,
|
|
|
|
|
'is-focused': isFocused,
|
|
|
|
|
'is-disabled': true,
|
|
|
|
|
});
|
|
|
|
|
const setRef = (ref) => { onOptionRef(ref, isFocused); };
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Option
|
|
|
|
|
className={className}
|
|
|
|
|
instancePrefix={instancePrefix}
|
|
|
|
|
isDisabled={isDisabled}
|
|
|
|
|
isFocused={isFocused}
|
|
|
|
|
isSelected={isSelected}
|
|
|
|
|
key={`option-${index}-${target[valueKey]}`}
|
|
|
|
|
onFocus={onFocus}
|
|
|
|
|
onSelect={noop}
|
|
|
|
|
option={target}
|
|
|
|
|
optionIndex={index}
|
|
|
|
|
ref={setRef}
|
|
|
|
|
>
|
2016-11-21 15:38:23 +00:00
|
|
|
<TargetOption
|
2016-11-14 17:32:13 +00:00
|
|
|
target={target}
|
|
|
|
|
onSelect={onSelect}
|
|
|
|
|
onMoreInfoClick={onMoreInfoClick}
|
|
|
|
|
/>
|
|
|
|
|
</Option>
|
|
|
|
|
);
|
2017-03-02 16:52:31 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
return targetsOutput;
|
2016-11-14 17:32:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2016-11-23 21:10:32 +00:00
|
|
|
<div className={baseClass}>
|
|
|
|
|
<div className={`${baseClass}__options`}>
|
2016-12-05 22:48:46 +00:00
|
|
|
{renderTargets('all')}
|
2016-11-14 17:32:13 +00:00
|
|
|
{renderTargets('labels')}
|
2016-11-23 21:10:32 +00:00
|
|
|
{renderTargets('hosts')}
|
2016-11-14 17:32:13 +00:00
|
|
|
</div>
|
2016-11-23 21:10:32 +00:00
|
|
|
<TargetDetails target={moreInfoTarget} className={`${baseClass}__spotlight`} handleBackToResults={handleBackToResults} />
|
2016-11-14 17:32:13 +00:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SelectTargetsMenu.propTypes = {
|
|
|
|
|
focusedOption: targetInterface,
|
|
|
|
|
instancePrefix: PropTypes.string,
|
|
|
|
|
onFocus: PropTypes.func,
|
|
|
|
|
onSelect: PropTypes.func,
|
|
|
|
|
optionClassName: PropTypes.string,
|
|
|
|
|
optionComponent: PropTypes.node,
|
|
|
|
|
options: PropTypes.arrayOf(targetInterface),
|
|
|
|
|
valueArray: PropTypes.arrayOf(targetInterface),
|
|
|
|
|
valueKey: PropTypes.string,
|
|
|
|
|
onOptionRef: PropTypes.func,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return SelectTargetsMenu;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default SelectTargetsMenuWrapper;
|