EditableTreeNode.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { Button, message, Space, Typography } from "antd";
  2. import { useState } from "react";
  3. import { PlusOutlined, EditOutlined } from "@ant-design/icons";
  4. import { TreeNodeData } from "./EditableTree";
  5. const { Text } = Typography;
  6. interface IWidget {
  7. node: TreeNodeData;
  8. onAdd?: Function;
  9. onEdit?: Function;
  10. onTitleClick?: Function;
  11. }
  12. const EditableTreeNodeWidget = ({
  13. node,
  14. onAdd,
  15. onEdit,
  16. onTitleClick,
  17. }: IWidget) => {
  18. const [showNodeMenu, setShowNodeMenu] = useState(false);
  19. const [loading, setLoading] = useState(false);
  20. const title = node.deletedAt ? (
  21. <Text delete disabled>
  22. {node.title_text ? node.title_text : node.title}
  23. </Text>
  24. ) : (
  25. <Text
  26. onClick={(e: React.MouseEvent<HTMLElement, MouseEvent>) => {
  27. if (typeof onTitleClick !== "undefined") {
  28. onTitleClick(e);
  29. }
  30. }}
  31. >
  32. {node.title_text ? node.title_text : node.title}
  33. </Text>
  34. );
  35. const menu = (
  36. <Space style={{ visibility: showNodeMenu ? "visible" : "hidden" }}>
  37. <Button
  38. size="middle"
  39. icon={<EditOutlined />}
  40. type="text"
  41. onClick={async () => {
  42. if (typeof onEdit !== "undefined") {
  43. onEdit();
  44. }
  45. }}
  46. />
  47. <Button
  48. loading={loading}
  49. size="middle"
  50. icon={<PlusOutlined />}
  51. type="text"
  52. onClick={async () => {
  53. if (typeof onAdd !== "undefined") {
  54. setLoading(true);
  55. const ok = await onAdd();
  56. setLoading(false);
  57. if (!ok) {
  58. message.error("error");
  59. }
  60. }
  61. }}
  62. />
  63. </Space>
  64. );
  65. return (
  66. <Space
  67. onMouseEnter={() => setShowNodeMenu(true)}
  68. onMouseLeave={() => setShowNodeMenu(false)}
  69. >
  70. {title}
  71. {menu}
  72. </Space>
  73. );
  74. };
  75. export default EditableTreeNodeWidget;