通用评论

index.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 {boolean} isNoMoreReply 是否没有更多回复
  38. */
  39. renderReplies(replies, isNoMoreReply) {
  40. const { commentId } = this.props;
  41. const { showReply } = this.state;
  42. if (showReply && replies && replies.length) {
  43. const len = replies.length;
  44. return (
  45. <div style={{ marginLeft: 50 }}>
  46. {replies.map((item, index) => {
  47. if (index === len - 1) {
  48. return [
  49. <ContentItem
  50. commentId={commentId}
  51. replyId={item.id}
  52. key={item.id}
  53. content={item}
  54. type="reply"
  55. />,
  56. <div className="moreBox" key="show_more_button">
  57. {!isNoMoreReply && (
  58. <span
  59. className="showMore"
  60. onClick={() => this.handleGetMoreReply(commentId)}
  61. >
  62. 查看更多回复
  63. </span>
  64. )}
  65. <a
  66. style={{ float: "right" }}
  67. onClick={this.handleToggleReply}
  68. >
  69. <Icon type="up" /> 收起回复
  70. </a>
  71. </div>
  72. ];
  73. }
  74. return (
  75. <ContentItem
  76. commentId={commentId}
  77. replyId={item.id}
  78. key={item.id}
  79. content={item}
  80. type="reply"
  81. />
  82. );
  83. })}
  84. </div>
  85. );
  86. }
  87. return null;
  88. }
  89. render() {
  90. const { content } = this.props;
  91. const { showReply } = this.state;
  92. return (
  93. <div>
  94. <ContentItem
  95. content={content}
  96. onShowReply={this.handleToggleReply}
  97. showReply={showReply}
  98. commentId={content.id}
  99. type="comment"
  100. />
  101. {this.renderReplies(content.replies, content.isNoMoreReply)}
  102. </div>
  103. );
  104. }
  105. }
  106. CommentBox.propTypes = {
  107. commentId: PropTypes.string.isRequired
  108. };
  109. CommentBox.defaultProps = {};
  110. export default Comment(CommentBox);