通用评论

index.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import React, { Component } from "react";
  2. import PropTypes from "prop-types";
  3. import { Avatar, Icon } from "antd";
  4. import dayjs from "dayjs";
  5. import Comment from "../../Comment";
  6. import CommentInput from "../CommentInput";
  7. // import Editor from "../Editor";
  8. import { renderContent } from "../../helper";
  9. import "./index.css";
  10. class CommentItem extends Component {
  11. constructor(props) {
  12. super(props);
  13. this.state = {
  14. isShowInput: false
  15. };
  16. this.handleToggleInput = this.handleToggleInput.bind(this);
  17. this.renderTextWithReply = this.renderTextWithReply.bind(this);
  18. }
  19. handleToggleInput() {
  20. this.setState({ isShowInput: !this.state.isShowInput });
  21. }
  22. renderTextWithReply(text, content) {
  23. let newText = text;
  24. const { reply } = content;
  25. if (reply) {
  26. newText = `${newText} //@<a href="/${reply.user_id}">${
  27. reply.user_name
  28. }</a> ${reply.content}`;
  29. if (reply.reply) {
  30. return this.renderTextWithReply(newText, reply);
  31. }
  32. }
  33. return newText;
  34. }
  35. render() {
  36. const {
  37. commentId,
  38. replyId,
  39. content,
  40. action,
  41. showReply,
  42. onShowReply,
  43. app
  44. } = this.props;
  45. const { isShowInput } = this.state;
  46. console.log("isShowInput ", isShowInput);
  47. const isComment = action === "comment";
  48. return (
  49. <div className="box">
  50. <div className="left">
  51. <Avatar src={content.user_avatar} size="large" />
  52. </div>
  53. <div className="right">
  54. <div className="name">
  55. <a href={`/${content.user_id}`}>
  56. {content.user_name || "暂无昵称"}
  57. </a>
  58. <span style={{ marginLeft: 10 }}>
  59. {dayjs(content.created * 1000).format("YYYY-MM-DD HH:mm:ss")}
  60. </span>
  61. </div>
  62. <div
  63. className="content"
  64. dangerouslySetInnerHTML={{
  65. __html: renderContent(
  66. this.renderTextWithReply(content.content, content)
  67. )
  68. }}
  69. />
  70. <div className="bottom">
  71. {isComment &&
  72. content.reply_count && (
  73. <div>
  74. <a
  75. className="itemLeft"
  76. onClick={onShowReply}
  77. style={{ userSelect: "none" }}
  78. >
  79. {content.reply_count} 条回复
  80. {showReply ? <Icon type="up" /> : <Icon type="down" />}
  81. </a>
  82. </div>
  83. )}
  84. <a onClick={this.handleToggleInput} className="itemRight">
  85. &nbsp; 回复
  86. </a>
  87. <div
  88. className="itemRight"
  89. style={{ cursor: "pointer" }}
  90. onClick={() => app.sCommentFavor(content.id, content.favored)}
  91. >
  92. <Icon
  93. type="like-o"
  94. className={content.favored ? "favored" : ""}
  95. />
  96. &nbsp;{content.favor_count}
  97. </div>
  98. </div>
  99. {/* {isShowInput && (
  100. <CommentInput
  101. action={action}
  102. replyId={replyId}
  103. commentId={commentId}
  104. callback={this.handleToggleInput}
  105. />
  106. )} */}
  107. {isShowInput && <CommentInput content={app.children} />}
  108. </div>
  109. </div>
  110. );
  111. }
  112. }
  113. CommentItem.propTypes = {
  114. content: PropTypes.object.isRequired,
  115. // comment 评论
  116. // reply 评论的回复
  117. // replyToReply 回复的回复
  118. action: PropTypes.oneOf(["comment", "reply", "replyToReply"]),
  119. onShowReply: PropTypes.func
  120. };
  121. CommentItem.defaultProps = {
  122. action: "comment",
  123. onShowReply: () => {}
  124. };
  125. export default Comment(CommentItem);