TermModal.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { useEffect, useState } from "react";
  2. import { Modal, Space } from "antd";
  3. import TermEdit from "./TermEdit";
  4. import type { ITermDataResponse } from "../../api/Term";
  5. import { Link } from "react-router";
  6. interface IWidget {
  7. trigger?: React.ReactNode;
  8. open?: boolean;
  9. id?: string;
  10. word?: string;
  11. tags?: string[];
  12. studioName?: string;
  13. channelId?: string;
  14. parentChannelId?: string;
  15. parentStudioId?: string;
  16. community?: boolean;
  17. onUpdate?: Function;
  18. onClose?: Function;
  19. }
  20. const TermModalWidget = ({
  21. trigger,
  22. open = false,
  23. id,
  24. word,
  25. tags,
  26. studioName,
  27. channelId,
  28. parentChannelId,
  29. parentStudioId,
  30. community = false,
  31. onUpdate,
  32. onClose,
  33. }: IWidget) => {
  34. const [isModalOpen, setIsModalOpen] = useState(open);
  35. useEffect(() => {
  36. if (open) {
  37. showModal();
  38. } else {
  39. modalClose();
  40. }
  41. }, [open]);
  42. const modalClose = () => {
  43. setIsModalOpen(false);
  44. if (document.getElementsByTagName("body")[0].hasAttribute("style")) {
  45. document.getElementsByTagName("body")[0].removeAttribute("style");
  46. }
  47. };
  48. const showModal = () => {
  49. setIsModalOpen(true);
  50. };
  51. const handleOk = () => {
  52. modalClose();
  53. if (typeof onClose !== "undefined") {
  54. onClose();
  55. }
  56. };
  57. const handleCancel = () => {
  58. modalClose();
  59. if (typeof onClose !== "undefined") {
  60. onClose();
  61. }
  62. };
  63. return (
  64. <>
  65. <span onClick={showModal}>{trigger}</span>
  66. <Modal
  67. style={{ top: 20 }}
  68. width={760}
  69. title={
  70. <Space>
  71. {"术语"}
  72. <Link
  73. to={`/studio/${studioName}/term/list`}
  74. target="_blank"
  75. style={{ display: "none" }}
  76. >
  77. 在studio中打开
  78. </Link>
  79. </Space>
  80. }
  81. footer={false}
  82. destroyOnHidden={true}
  83. open={isModalOpen}
  84. onOk={handleOk}
  85. onCancel={handleCancel}
  86. >
  87. <TermEdit
  88. id={id}
  89. word={word}
  90. tags={tags}
  91. studioName={studioName}
  92. channelId={channelId}
  93. parentChannelId={parentChannelId}
  94. parentStudioId={parentStudioId}
  95. community={community}
  96. onUpdate={(value: ITermDataResponse) => {
  97. modalClose();
  98. if (typeof onUpdate !== "undefined") {
  99. onUpdate(value);
  100. }
  101. }}
  102. />
  103. </Modal>
  104. </>
  105. );
  106. };
  107. export default TermModalWidget;