@netdata/charts
Version:
Netdata frontend SDK and chart utilities
103 lines • 4.86 kB
JavaScript
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
import React, { useState } from "react";
import Icon, { Button } from "../../../icon";
import { Flex, TextInput } from "@netdata/netdata-ui";
import xIcon from "@netdata/netdata-ui/dist/components/icon/assets/x.svg";
import checkIcon from "@netdata/netdata-ui/dist/components/icon/assets/check.svg";
import { useChart } from "../../../provider";
import ColorPicker from "./colorPicker";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
var AnnotationForm = function AnnotationForm(_ref) {
var _ref$initialText = _ref.initialText,
initialText = _ref$initialText === void 0 ? "" : _ref$initialText,
_ref$initialColor = _ref.initialColor,
initialColor = _ref$initialColor === void 0 ? "#0075F2" : _ref$initialColor,
_ref$initialPriority = _ref.initialPriority,
initialPriority = _ref$initialPriority === void 0 ? "info" : _ref$initialPriority,
_ref$placeholder = _ref.placeholder,
placeholder = _ref$placeholder === void 0 ? "Annotation text..." : _ref$placeholder,
onSave = _ref.onSave,
onCancel = _ref.onCancel,
_ref$autoFocus = _ref.autoFocus,
autoFocus = _ref$autoFocus === void 0 ? true : _ref$autoFocus;
var chart = useChart();
var _useState = useState(initialText),
_useState2 = _slicedToArray(_useState, 2),
text = _useState2[0],
setText = _useState2[1];
var _useState3 = useState(initialColor),
_useState4 = _slicedToArray(_useState3, 2),
color = _useState4[0],
setColor = _useState4[1];
var _useState5 = useState(initialPriority),
_useState6 = _slicedToArray(_useState5, 2),
priority = _useState6[0],
setPriority = _useState6[1];
var handleColorChange = function handleColorChange(newColor, newPriority) {
setColor(newColor);
setPriority(newPriority);
};
var handleSave = function handleSave() {
if (!text.trim()) return;
onSave({
text: text.trim(),
color: color,
priority: priority
});
};
var handleKeyDown = function handleKeyDown(e) {
if (e.key === "Enter") {
e.preventDefault();
handleSave();
} else if (e.key === "Escape") {
e.preventDefault();
onCancel();
}
};
return /*#__PURE__*/_jsxs(Flex, {
column: true,
gap: 1,
children: [/*#__PURE__*/_jsxs(Flex, {
gap: 1,
alignItems: "center",
children: [/*#__PURE__*/_jsx(TextInput, {
name: "annotation-text",
placeholder: placeholder,
value: text,
onChange: function onChange(e) {
return setText(e.target.value);
},
onKeyDown: handleKeyDown,
autoFocus: autoFocus,
size: "tiny"
}), /*#__PURE__*/_jsx(Button, {
icon: /*#__PURE__*/_jsx(Icon, {
svg: checkIcon,
size: "16px"
}),
onClick: handleSave,
disabled: !text.trim(),
size: "small",
"data-track": chart.track("annotation-save-".concat(initialText ? "edit" : "create"))
}), /*#__PURE__*/_jsx(Button, {
icon: /*#__PURE__*/_jsx(Icon, {
svg: xIcon,
size: "16px"
}),
onClick: onCancel,
size: "small",
variant: "secondary",
"data-track": chart.track("annotation-cancel")
})]
}), /*#__PURE__*/_jsx(ColorPicker, {
selectedColor: color,
onColorChange: handleColorChange
})]
});
};
export default AnnotationForm;