通用评论

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _icon = require("antd/es/icon");
  6. var _icon2 = _interopRequireDefault(_icon);
  7. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  8. require("antd/es/icon/style/css");
  9. var _react = require("react");
  10. var _react2 = _interopRequireDefault(_react);
  11. var _propTypes = require("prop-types");
  12. var _propTypes2 = _interopRequireDefault(_propTypes);
  13. var _Comment = require("../../Comment");
  14. var _Comment2 = _interopRequireDefault(_Comment);
  15. var _ContentItem = require("./../ContentItem");
  16. var _ContentItem2 = _interopRequireDefault(_ContentItem);
  17. require("./index.css");
  18. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  19. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  20. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  21. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  22. var CommentBox = function (_Component) {
  23. _inherits(CommentBox, _Component);
  24. function CommentBox(props) {
  25. _classCallCheck(this, CommentBox);
  26. var _this = _possibleConstructorReturn(this, (CommentBox.__proto__ || Object.getPrototypeOf(CommentBox)).call(this, props));
  27. _this.state = {
  28. showReply: true,
  29. page: 1
  30. };
  31. _this.handleToggleReply = _this.handleToggleReply.bind(_this);
  32. _this.handleGetMoreReply = _this.handleGetMoreReply.bind(_this);
  33. _this.renderReplies = _this.renderReplies.bind(_this);
  34. return _this;
  35. }
  36. /**
  37. * 切换是否显示回复列表
  38. */
  39. _createClass(CommentBox, [{
  40. key: "handleToggleReply",
  41. value: function handleToggleReply() {
  42. this.setState({ showReply: !this.state.showReply });
  43. }
  44. /**
  45. * 获取更多评论
  46. * @param {string} commentId comment id
  47. */
  48. }, {
  49. key: "handleGetMoreReply",
  50. value: function handleGetMoreReply(commentId) {
  51. // 从第一页开始获取评论
  52. var page = this.state.page;
  53. this.props.app.sGetReply({ commentId: commentId, page: page });
  54. this.setState({ page: page + 1 });
  55. }
  56. /**
  57. * 渲染回复 DOM
  58. * @param {array} replies 回复列表
  59. * @param {boolean} isNoMoreReply 是否没有更多回复
  60. */
  61. }, {
  62. key: "renderReplies",
  63. value: function renderReplies(replies, isNoMoreReply) {
  64. var _this2 = this;
  65. var commentId = this.props.commentId;
  66. var showReply = this.state.showReply;
  67. if (showReply && replies && replies.length) {
  68. var len = replies.length;
  69. return _react2.default.createElement(
  70. "div",
  71. { style: { marginLeft: 50 } },
  72. replies.map(function (item, index) {
  73. if (index === len - 1) {
  74. return [_react2.default.createElement(_ContentItem2.default, {
  75. commentId: commentId,
  76. replyId: item.id,
  77. key: item.id,
  78. content: item,
  79. type: "reply"
  80. }), _react2.default.createElement(
  81. "div",
  82. { className: "moreBox", key: "show_more_button" },
  83. !isNoMoreReply && _react2.default.createElement(
  84. "span",
  85. {
  86. className: "showMore",
  87. onClick: function onClick() {
  88. return _this2.handleGetMoreReply(commentId);
  89. }
  90. },
  91. "\u67E5\u770B\u66F4\u591A\u56DE\u590D"
  92. ),
  93. _react2.default.createElement(
  94. "a",
  95. {
  96. style: { float: "right" },
  97. onClick: _this2.handleToggleReply
  98. },
  99. _react2.default.createElement(_icon2.default, { type: "up" }),
  100. " \u6536\u8D77\u56DE\u590D"
  101. )
  102. )];
  103. }
  104. return _react2.default.createElement(_ContentItem2.default, {
  105. commentId: commentId,
  106. replyId: item.id,
  107. key: item.id,
  108. content: item,
  109. type: "reply"
  110. });
  111. })
  112. );
  113. }
  114. return null;
  115. }
  116. }, {
  117. key: "render",
  118. value: function render() {
  119. var content = this.props.content;
  120. var showReply = this.state.showReply;
  121. return _react2.default.createElement(
  122. "div",
  123. null,
  124. _react2.default.createElement(_ContentItem2.default, {
  125. content: content,
  126. onShowReply: this.handleToggleReply,
  127. showReply: showReply,
  128. commentId: content.id,
  129. type: "comment"
  130. }),
  131. this.renderReplies(content.replies, content.isNoMoreReply)
  132. );
  133. }
  134. }]);
  135. return CommentBox;
  136. }(_react.Component);
  137. CommentBox.propTypes = {
  138. commentId: _propTypes2.default.string.isRequired
  139. };
  140. CommentBox.defaultProps = {};
  141. exports.default = (0, _Comment2.default)(CommentBox);
  142. //# sourceMappingURL=index.js.map