CommentBox.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { useState } from "react";
  2. import { Drawer } from "antd";
  3. import CommentTopic from "./CommentTopic";
  4. import CommentListCard from "./CommentListCard";
  5. import { IComment } from "./CommentItem";
  6. export interface IAnswerCount {
  7. id: string;
  8. count: number;
  9. }
  10. interface IWidget {
  11. trigger?: JSX.Element;
  12. resId?: string;
  13. resType?: string;
  14. onCommentCountChange?: Function;
  15. }
  16. const Widget = ({ trigger, resId, resType, onCommentCountChange }: IWidget) => {
  17. const [open, setOpen] = useState(false);
  18. const [childrenDrawer, setChildrenDrawer] = useState(false);
  19. const [topicComment, setTopicComment] = useState<IComment>();
  20. const [answerCount, setAnswerCount] = useState<IAnswerCount>();
  21. console.log(resId, resType);
  22. const showDrawer = () => {
  23. setOpen(true);
  24. };
  25. const onClose = () => {
  26. setOpen(false);
  27. };
  28. const showChildrenDrawer = (
  29. e: React.MouseEvent<HTMLSpanElement, MouseEvent>,
  30. comment: IComment
  31. ) => {
  32. setChildrenDrawer(true);
  33. setTopicComment(comment);
  34. };
  35. const onChildrenDrawerClose = () => {
  36. setChildrenDrawer(false);
  37. };
  38. return (
  39. <>
  40. <span onClick={showDrawer}>{trigger}</span>
  41. <Drawer
  42. title="Discussion"
  43. width={520}
  44. closable={false}
  45. onClose={onClose}
  46. open={open}
  47. >
  48. <CommentListCard
  49. resId={resId}
  50. resType={resType}
  51. onSelect={showChildrenDrawer}
  52. changedAnswerCount={answerCount}
  53. onItemCountChange={(count: number) => {
  54. if (typeof onCommentCountChange !== "undefined") {
  55. onCommentCountChange(count);
  56. }
  57. }}
  58. />
  59. <Drawer
  60. title="回答"
  61. width={480}
  62. closable={false}
  63. onClose={onChildrenDrawerClose}
  64. open={childrenDrawer}
  65. >
  66. {resId && resType ? (
  67. <CommentTopic
  68. comment={topicComment}
  69. resId={resId}
  70. resType={resType}
  71. onItemCountChange={(count: number, parent: string) => {
  72. setAnswerCount({ id: parent, count: count });
  73. }}
  74. />
  75. ) : (
  76. <></>
  77. )}
  78. </Drawer>
  79. </Drawer>
  80. </>
  81. );
  82. };
  83. export default Widget;