通用评论 vedio

index.js 6.4KB

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