fleet/frontend/components/forms/fields/SelectTargetsDropdown/TargetOption/TargetOption.jsx

80 lines
2 KiB
React
Raw Normal View History

import React, { Component } from "react";
import PropTypes from "prop-types";
import classnames from "classnames";
import targetInterface from "interfaces/target";
import TargetIcon from "./TargetIcon";
const baseClass = "target-option";
class TargetOption extends Component {
static propTypes = {
onMoreInfoClick: PropTypes.func,
onSelect: PropTypes.func,
target: targetInterface.isRequired,
};
handleSelect = (evt) => {
const { onSelect, target } = this.props;
return onSelect(target, evt);
};
renderTargetDetail = () => {
const { target } = this.props;
2017-01-20 17:02:13 +00:00
const {
count,
primary_ip: hostIpAddress,
2017-01-20 17:02:13 +00:00
target_type: targetType,
} = target;
if (targetType === "hosts") {
2017-02-24 19:11:33 +00:00
if (!hostIpAddress) {
return false;
}
2016-11-23 21:10:32 +00:00
return (
<span>
2017-01-20 17:02:13 +00:00
<span className={`${baseClass}__ip`}>{hostIpAddress}</span>
2016-11-23 21:10:32 +00:00
</span>
);
}
return <span className={`${baseClass}__count`}>{count} hosts</span>;
};
render() {
const { onMoreInfoClick, target } = this.props;
const { display_text: displayText, target_type: targetType } = target;
const { handleSelect, renderTargetDetail } = this;
const wrapperClassName = classnames(`${baseClass}__wrapper`, {
"is-team": targetType === "teams",
"is-label": targetType === "labels",
"is-host": targetType === "hosts",
});
return (
<div className={wrapperClassName}>
<button
className={`button button--unstyled ${baseClass}__target-content`}
onClick={onMoreInfoClick(target)}
>
<div>
<TargetIcon target={target} />
<span className={`${baseClass}__label-label`}>
{displayText !== "All Hosts" ? displayText : "All hosts"}
</span>
</div>
{renderTargetDetail()}
</button>
<button
className={`button button--unstyled ${baseClass}__add-btn`}
onClick={handleSelect}
/>
</div>
);
}
}
export default TargetOption;