通用评论

index.js 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. const { action, commentId, replyId, callback } = this.props;
  12. if (action === "comment") {
  13. this.props.app.sCreateComment({
  14. content: value
  15. });
  16. } else if (action === "reply") {
  17. this.props.app.sCreateReply(
  18. {
  19. comment_id: commentId,
  20. content: value
  21. },
  22. () => callback && callback()
  23. );
  24. } else if (action === "replyToReply") {
  25. this.props.app.sCreateReply(
  26. {
  27. comment_id: commentId,
  28. content: value,
  29. reply_id: replyId
  30. },
  31. () => callback && callback()
  32. );
  33. }
  34. }
  35. render() {
  36. const childrenWithProps = React.Children.map(this.props.content, child => {
  37. return React.cloneElement(child, {
  38. // 编辑器本身不提交值,但 CommentInput 会提交
  39. // CommentInput 主要是负责评论的业务逻辑,提交评论和回复
  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);