通用评论

index.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 } = 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. replyId={item.id}
  53. key={item.id}
  54. content={item}
  55. type="replyToReply" // 回复的回复
  56. />,
  57. <div className="moreBox" key="show_more_button">
  58. {!isNoMoreReply &&
  59. replyCount !== len && (
  60. <span
  61. className="showMore"
  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. commentId={commentId}
  79. replyId={item.id}
  80. key={item.id}
  81. content={item}
  82. type="reply" // 评论的回复
  83. />
  84. );
  85. })}
  86. </div>
  87. );
  88. }
  89. return null;
  90. }
  91. render() {
  92. const { content } = this.props;
  93. const { showReply } = this.state;
  94. return (
  95. <div>
  96. <ContentItem
  97. content={content}
  98. onShowReply={this.handleToggleReply}
  99. showReply={showReply}
  100. commentId={content.id}
  101. action="comment" // 回复
  102. />
  103. {this.renderReplies(
  104. content.replies,
  105. content.reply_count,
  106. content.isNoMoreReply
  107. )}
  108. </div>
  109. );
  110. }
  111. }
  112. CommentBox.propTypes = {
  113. commentId: PropTypes.string.isRequired
  114. };
  115. CommentBox.defaultProps = {};
  116. export default Comment(CommentBox);