通用评论

index.js 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import React, { Component } from "react";
  2. import PropTypes from "prop-types";
  3. import { IMAGE_SPLIT } from "../../constant";
  4. import Comment from "../../Comment";
  5. class CommentInput extends Component {
  6. constructor(props) {
  7. super(props);
  8. this.state = {};
  9. this.handleSubmit = this.handleSubmit.bind(this);
  10. }
  11. /**
  12. * 提交评论
  13. * @param {object} { text<string>, files<array> } 需要提交的评论的文本和图片
  14. * @param {function} cb 提交成功后的回掉
  15. */
  16. handleSubmit({ text, files }, cb) {
  17. let value = text;
  18. if (files && files.length) {
  19. value += IMAGE_SPLIT;
  20. files.forEach(file => {
  21. value += `${file},`;
  22. });
  23. }
  24. if (value.substr(-1) === ",") {
  25. value = value.slice(0, -1);
  26. }
  27. const { action, commentId, replyId, userId, callback } = this.props;
  28. if (action === "comment") {
  29. this.props.app.sCreateComment(
  30. {
  31. content: value
  32. },
  33. cb
  34. );
  35. } else if (action === "reply") {
  36. console.log("[[[", callback);
  37. // this.props.app.sCreateReply(
  38. // {
  39. // comment_id: commentId,
  40. // content: value,
  41. // business_user_id: userId
  42. // },
  43. // (data) => callback && callback(data)
  44. // );
  45. } else if (action === "replyToReply") {
  46. this.props.app.sCreateReply(
  47. {
  48. comment_id: commentId,
  49. content: value,
  50. reply_id: replyId,
  51. business_user_id: userId
  52. },
  53. data => callback && callback(data)
  54. );
  55. }
  56. }
  57. render() {
  58. const childrenWithProps = React.Children.map(this.props.content, child => {
  59. return React.cloneElement(child, {
  60. // 编辑器本身不提交值,但 CommentInput 会提交
  61. // CommentInput 主要是负责评论的业务逻辑,提交评论和回复
  62. // 默认使用 CommentInput 的 onSubmit 来提交评论
  63. // 但也可以使用 Editor 的 props 来覆盖 onSubmit
  64. onSubmit: this.handleSubmit,
  65. ...child.props,
  66. // 如果当前的编辑器不是“评论”,则编辑器高度减小一些
  67. rows:
  68. this.props.action === "comment"
  69. ? child.props.rows
  70. : child.props.rows - 1
  71. });
  72. });
  73. return <div>{childrenWithProps}</div>;
  74. }
  75. }
  76. CommentInput.propTypes = {
  77. // comment 评论
  78. // reply 评论的回复
  79. // replyToReply 回复的回复
  80. action: PropTypes.oneOf(["comment", "reply", "replyToReply"])
  81. };
  82. CommentInput.defaultProps = {
  83. action: "comment"
  84. };
  85. export default Comment(CommentInput);