通用评论 vedio

index.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import React, { Component } from "react";
  2. import PropTypes from "prop-types";
  3. import { Icon } from "antd";
  4. import intl from "react-intl-universal";
  5. import Comment from "../../Comment";
  6. import ContentItem from "./../ContentItem";
  7. import "./index.css";
  8. class CommentBox extends Component {
  9. constructor(props) {
  10. super(props);
  11. this.state = {
  12. showReply: true,
  13. page: 1
  14. };
  15. this.handleToggleReply = this.handleToggleReply.bind(this);
  16. this.handleGetMoreReply = this.handleGetMoreReply.bind(this);
  17. this.renderReplies = this.renderReplies.bind(this);
  18. }
  19. /**
  20. * 切换是否显示回复列表
  21. */
  22. handleToggleReply() {
  23. this.setState({ showReply: !this.state.showReply });
  24. }
  25. /**
  26. * 获取更多评论
  27. * @param {string} commentId comment id
  28. */
  29. handleGetMoreReply(commentId) {
  30. // 从第一页开始获取评论
  31. const { page } = this.state;
  32. this.props.app.sGetReply({ commentId, page });
  33. this.setState({ page: page + 1 });
  34. }
  35. /**
  36. * 渲染回复 DOM
  37. * @param {array} replies 回复列表
  38. * @param {number} replies 回复的数量
  39. * @param {boolean} isNoMoreReply 是否没有更多回复
  40. */
  41. renderReplies(replies, replyCount, isNoMoreReply) {
  42. const { commentId } = this.props;
  43. const { showReply } = this.state;
  44. if (showReply && replies && replies.length) {
  45. const len = replies.length;
  46. return (
  47. <div style={{ marginLeft: 50 }}>
  48. {replies.map((item, index) => {
  49. if (index === len - 1) {
  50. return [
  51. <ContentItem
  52. commentId={commentId}
  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. {intl.get("reply.moreReply")}
  65. </span>
  66. )}
  67. <a
  68. style={{ float: "right" }}
  69. onClick={this.handleToggleReply}
  70. >
  71. <Icon type="up" /> {intl.get("reply.collapse")}
  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. action="replyToReply" // 评论的回复
  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="reply" // 评论的回复
  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);