通用评论

index.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import React, { Component } from "react";
  2. import PropTypes from "prop-types";
  3. import { Icon } from "antd";
  4. import Comment from "../../Comment";
  5. import ContentItem from "./../ContentItem";
  6. import "./index.css";
  7. class CommentBox extends Component {
  8. constructor(props) {
  9. super(props);
  10. this.state = {
  11. showReply: true,
  12. page: 1
  13. };
  14. this.handleToggleReply = this.handleToggleReply.bind(this);
  15. this.handleGetMoreReply = this.handleGetMoreReply.bind(this);
  16. this.renderReplies = this.renderReplies.bind(this);
  17. }
  18. /**
  19. * 切换是否显示回复列表
  20. */
  21. handleToggleReply() {
  22. this.setState({ showReply: !this.state.showReply });
  23. }
  24. /**
  25. * 获取更多评论
  26. * @param {string} commentId comment id
  27. */
  28. handleGetMoreReply(commentId) {
  29. // 从第一页开始获取评论
  30. const { page } = this.state;
  31. this.props.app.sGetReply({ commentId, page });
  32. this.setState({ page: page + 1 });
  33. }
  34. /**
  35. * 渲染回复 DOM
  36. * @param {array} replies 回复列表
  37. * @param {number} replies 回复的数量
  38. * @param {boolean} isNoMoreReply 是否没有更多回复
  39. */
  40. renderReplies(replies, replyCount, isNoMoreReply) {
  41. const { commentId, isMobile } = this.props;
  42. const { showReply } = this.state;
  43. if (showReply && replies && replies.length) {
  44. const len = replies.length;
  45. return (
  46. <div style={{ marginLeft: 50 }}>
  47. {replies.map((item, index) => {
  48. if (index === len - 1) {
  49. return [
  50. <ContentItem
  51. commentId={commentId}
  52. isMobile={isMobile}
  53. replyId={item.id}
  54. key={item.id}
  55. content={item}
  56. action="replyToReply" // 回复的回复
  57. />,
  58. <div className="comment-more-box" key="show_more_button">
  59. {!isNoMoreReply && replyCount !== len && (
  60. <span
  61. className="comment-show-more"
  62. onClick={() => this.handleGetMoreReply(commentId)}
  63. >
  64. 查看更多回复
  65. </span>
  66. )}
  67. <a
  68. style={{ float: "right" }}
  69. onClick={this.handleToggleReply}
  70. >
  71. <Icon type="up" /> 收起回复
  72. </a>
  73. </div>
  74. ];
  75. }
  76. return (
  77. <ContentItem
  78. isMobile={isMobile}
  79. commentId={commentId}
  80. replyId={item.id}
  81. key={item.id}
  82. content={item}
  83. action="replyToReply" // 评论的回复
  84. />
  85. );
  86. })}
  87. </div>
  88. );
  89. }
  90. return null;
  91. }
  92. render() {
  93. const { content, isMobile } = this.props;
  94. const { showReply } = this.state;
  95. return (
  96. <div>
  97. <ContentItem
  98. isMobile={isMobile}
  99. content={content}
  100. onShowReply={this.handleToggleReply}
  101. showReply={showReply}
  102. commentId={content.id}
  103. action="reply" // 评论的回复
  104. />
  105. {this.renderReplies(
  106. content.replies,
  107. content.reply_count,
  108. content.isNoMoreReply
  109. )}
  110. </div>
  111. );
  112. }
  113. }
  114. CommentBox.propTypes = {
  115. commentId: PropTypes.string.isRequired
  116. };
  117. CommentBox.defaultProps = {};
  118. export default Comment(CommentBox);