通用评论

index.js 1.9KB

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