通用评论

index.js 2.0KB

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