通用评论

index.js 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 的 onSubmit 来提交评论
  40. // 但也可以使用 Editor 的 props 来覆盖 onSubmit
  41. onSubmit: this.handleSubmit,
  42. ...child.props
  43. });
  44. });
  45. return (
  46. <div>
  47. {/* {type === "normal" ? (
  48. <div>
  49. <span
  50. style={{
  51. border: "1px solid #CECECE",
  52. color: "#666",
  53. padding: "2px 3px",
  54. }}
  55. >
  56. 回复
  57. </span>
  58. <span style={{ marginLeft: "20px", color: "#5198EB" }}>
  59. 口碑
  60. <span style={{ marginLeft: "20px", color: "#666666" }}>
  61. (全站挑出毛病或提出合理建议,奖励10到100元红包)
  62. </span>
  63. </span>
  64. </div>
  65. ) : null} */}
  66. <div style={{ marginTop: 40 }}>{childrenWithProps}</div>
  67. </div>
  68. );
  69. }
  70. }
  71. CommentInput.propTypes = {
  72. // comment 评论
  73. // reply 评论的回复
  74. // replyToReply 回复的回复
  75. action: PropTypes.oneOf(["comment", "reply", "replyToReply"])
  76. };
  77. CommentInput.defaultProps = {
  78. action: "comment"
  79. };
  80. export default Comment(CommentInput);