通用评论

index.js 2.3KB

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