import React, { Component } from "react"; import PropTypes from "prop-types"; import { Icon, AutoComplete } from "antd"; // import intl from "react-intl-universal"; import Comment from "../../Comment"; import ContentItem from "./../ContentItem"; import ReplyItem from "./../ReplyItem"; import "./index.css"; class CommentBox extends Component { constructor(props) { super(props); this.state = { showReply: true, page: 1 }; this.handleToggleReply = this.handleToggleReply.bind(this); this.handleGetMoreReply = this.handleGetMoreReply.bind(this); this.renderReplies = this.renderReplies.bind(this); } /** * 切换是否显示回复列表 */ handleToggleReply() { this.setState({ showReply: !this.state.showReply }); } /** * 获取更多评论 * @param {string} commentId comment id */ handleGetMoreReply(commentId) { // 从第一页开始获取评论 const { page } = this.state; this.props.app.sGetReply({ commentId, page }); this.setState({ page: page + 1 }); } /** * 渲染回复 DOM * @param {array} replies 回复列表 * @param {number} replies 回复的数量 * @param {boolean} isNoMoreReply 是否没有更多回复 */ // renderReplies(replies, replyCount, isNoMoreReply) { // console.log('replies', replies); // console.log('replyCount', replyCount); // console.log('isNoMoreReply', isNoMoreReply); // const { commentId } = this.props; // const { showReply } = this.state; // if (showReply && replies && replies.length) { // const len = replies.length; // return ( //
// {replies.map((item, index) => { // if (index === len - 1) { // return [ // , //
// {!isNoMoreReply && replyCount !== len && ( // this.handleGetMoreReply(commentId)} // > // {intl.get("reply.moreReply")} // // )} // // {intl.get("reply.collapse")} // //
// ]; // } // return ( // // ); // })} //
// ); // } // return null; // } /** * 渲染回复 DOM * @param {array} replies 回复列表 * @param {number} replyCount 回复的数量 * @param {boolean} isNoMoreReply 是否没有更多回复 */ renderReplies(replies, replyCount, isNoMoreReply) { const { commentId } = this.props; const { showReply } = this.state; if (showReply && replies && replies.length) { const len = replies.length; return (
{replies.map((item, index) => { if (index === len - 1) { return [ ,
{!isNoMoreReply && replyCount !== len && ( this.handleGetMoreReply(commentId)} > 展开更多评论 )} 收起
]; } return ( ); })}
); } } render() { const { content } = this.props; const { showReply } = this.state; return (
{this.renderReplies( content.replies, content.reply_count, content.isNoMoreReply )}
); } } CommentBox.propTypes = { commentId: PropTypes.string.isRequired }; CommentBox.defaultProps = {}; export default Comment(CommentBox);