通用评论 vedio

index.js 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import React, { Component } from "react";
  2. import PropTypes from "prop-types";
  3. import { Icon, AutoComplete } from "antd";
  4. // import intl from "react-intl-universal";
  5. import Comment from "../../Comment";
  6. import ContentItem from "./../ContentItem";
  7. import ReplyItem from "./../ReplyItem";
  8. import "./index.css";
  9. class CommentBox extends Component {
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. showReply: true,
  14. page: 1
  15. };
  16. this.handleToggleReply = this.handleToggleReply.bind(this);
  17. this.handleGetMoreReply = this.handleGetMoreReply.bind(this);
  18. this.renderReplies = this.renderReplies.bind(this);
  19. }
  20. /**
  21. * 切换是否显示回复列表
  22. */
  23. handleToggleReply() {
  24. this.setState({ showReply: !this.state.showReply });
  25. }
  26. /**
  27. * 获取更多评论
  28. * @param {string} commentId comment id
  29. */
  30. handleGetMoreReply(commentId) {
  31. // 从第一页开始获取评论
  32. const { page } = this.state;
  33. this.props.app.sGetReply({ commentId, page });
  34. this.setState({ page: page + 1 });
  35. }
  36. /**
  37. * 渲染回复 DOM
  38. * @param {array} replies 回复列表
  39. * @param {number} replies 回复的数量
  40. * @param {boolean} isNoMoreReply 是否没有更多回复
  41. */
  42. // renderReplies(replies, replyCount, isNoMoreReply) {
  43. // console.log('replies', replies);
  44. // console.log('replyCount', replyCount);
  45. // console.log('isNoMoreReply', isNoMoreReply);
  46. // const { commentId } = this.props;
  47. // const { showReply } = this.state;
  48. // if (showReply && replies && replies.length) {
  49. // const len = replies.length;
  50. // return (
  51. // <div style={{ marginLeft: 50 }}>
  52. // {replies.map((item, index) => {
  53. // if (index === len - 1) {
  54. // return [
  55. // <ContentItem
  56. // commentId={commentId}
  57. // replyId={item.id}
  58. // key={item.id}
  59. // content={item}
  60. // action="replyToReply" // 回复的回复
  61. // />,
  62. // <div className="comment-more-box" key="show_more_button">
  63. // {!isNoMoreReply && replyCount !== len && (
  64. // <span
  65. // className="comment-show-more"
  66. // onClick={() => this.handleGetMoreReply(commentId)}
  67. // >
  68. // {intl.get("reply.moreReply")}
  69. // </span>
  70. // )}
  71. // <a
  72. // style={{ float: "right" }}
  73. // onClick={this.handleToggleReply}
  74. // >
  75. // <Icon type="up" /> {intl.get("reply.collapse")}
  76. // </a>
  77. // </div>
  78. // ];
  79. // }
  80. // return (
  81. // <ContentItem
  82. // commentId={commentId}
  83. // replyId={item.id}
  84. // key={item.id}
  85. // content={item}
  86. // action="replyToReply" // 评论的回复
  87. // />
  88. // );
  89. // })}
  90. // </div>
  91. // );
  92. // }
  93. // return null;
  94. // }
  95. /**
  96. * 渲染回复 DOM
  97. * @param {array} replies 回复列表
  98. * @param {number} replyCount 回复的数量
  99. * @param {boolean} isNoMoreReply 是否没有更多回复
  100. */
  101. renderReplies(replies, replyCount, isNoMoreReply) {
  102. const { commentId } = this.props;
  103. const { showReply } = this.state;
  104. if (showReply && replies && replies.length) {
  105. const len = replies.length;
  106. return (
  107. <div
  108. style={{
  109. marginLeft: 50,
  110. borderTop: "1px solid #929292",
  111. paddingTop: 15,
  112. marginTop: 10,
  113. overflow: "auto"
  114. }}
  115. >
  116. {replies.map((item, index) => {
  117. if (index === len - 1) {
  118. return [
  119. <ReplyItem
  120. replyItem={item}
  121. key={item.id}
  122. commentId={commentId}
  123. action="replyToReply"
  124. />,
  125. <div key="show_more_button">
  126. {!isNoMoreReply && replyCount !== len && (
  127. <span
  128. className="comment-show-more"
  129. onClick={() => this.handleGetMoreReply(commentId)}
  130. >
  131. 展开更多评论
  132. <Icon type="down" />
  133. </span>
  134. )}
  135. <a
  136. style={{ float: "right", color: "#d5d5d5" }}
  137. onClick={this.handleToggleReply}
  138. >
  139. <Icon type="up" /> 收起
  140. </a>
  141. </div>
  142. ];
  143. }
  144. return (
  145. <ReplyItem
  146. replyItem={item}
  147. key={item.id}
  148. commentId={commentId}
  149. action="replyToReply"
  150. />
  151. );
  152. })}
  153. </div>
  154. );
  155. }
  156. }
  157. render() {
  158. const { content } = this.props;
  159. const { showReply } = this.state;
  160. return (
  161. <div>
  162. <ContentItem
  163. content={content}
  164. onShowReply={this.handleToggleReply}
  165. showReply={showReply}
  166. commentId={content.id}
  167. action="reply" // 评论的回复
  168. />
  169. {this.renderReplies(
  170. content.replies,
  171. content.reply_count,
  172. content.isNoMoreReply
  173. )}
  174. </div>
  175. );
  176. }
  177. }
  178. CommentBox.propTypes = {
  179. commentId: PropTypes.string.isRequired
  180. };
  181. CommentBox.defaultProps = {};
  182. export default Comment(CommentBox);