通用评论

index.js 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 {string} value 需要提交的评论的值
  13. * @param {function} cb 提交成功后的回掉
  14. */
  15. handleSubmit(value, cb) {
  16. const { action, commentId, replyId, callback } = this.props;
  17. if (action === "comment") {
  18. this.props.app.sCreateComment(
  19. {
  20. content: value
  21. },
  22. cb
  23. );
  24. } else if (action === "reply") {
  25. this.props.app.sCreateReply(
  26. {
  27. comment_id: commentId,
  28. content: value
  29. },
  30. () => callback && callback()
  31. );
  32. } else if (action === "replyToReply") {
  33. this.props.app.sCreateReply(
  34. {
  35. comment_id: commentId,
  36. content: value,
  37. reply_id: replyId
  38. },
  39. () => callback && callback()
  40. );
  41. }
  42. }
  43. render() {
  44. const childrenWithProps = React.Children.map(this.props.content, child => {
  45. return React.cloneElement(child, {
  46. // 编辑器本身不提交值,但 CommentInput 会提交
  47. // CommentInput 主要是负责评论的业务逻辑,提交评论和回复
  48. // 默认使用 CommentInput 的 onSubmit 来提交评论
  49. // 但也可以使用 Editor 的 props 来覆盖 onSubmit
  50. onSubmit: this.handleSubmit,
  51. ...child.props,
  52. // 如果当前的编辑器不是“评论”,则编辑器高度减小一些
  53. rows:
  54. this.props.action === "comment"
  55. ? child.props.rows
  56. : child.props.rows - 1
  57. });
  58. });
  59. return <div>{childrenWithProps}</div>;
  60. }
  61. }
  62. CommentInput.propTypes = {
  63. // comment 评论
  64. // reply 评论的回复
  65. // replyToReply 回复的回复
  66. action: PropTypes.oneOf(["comment", "reply", "replyToReply"])
  67. };
  68. CommentInput.defaultProps = {
  69. action: "comment"
  70. };
  71. export default Comment(CommentInput);