通用评论

index.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import React, { Component } from "react";
  2. import PropTypes from "prop-types";
  3. import { OSS_LINK } from "../../constant";
  4. import Comment from "../../Comment";
  5. class CommentInput extends Component {
  6. constructor(props) {
  7. super(props);
  8. this.state = {
  9. value: "",
  10. fileList: [], // 图片列表
  11. fileMap: {} // 已经上传的图片路径和 uid 的映射 { uid: path }
  12. };
  13. this.handleChange = this.handleChange.bind(this);
  14. this.handleSubmit = this.handleSubmit.bind(this);
  15. this.handleChangeFileList = this.handleChangeFileList.bind(this);
  16. this.handleChangeEmoji = this.handleChangeEmoji.bind(this);
  17. this.handleUpload = this.handleUpload.bind(this);
  18. }
  19. handleChange(value) {
  20. this.setState({ value });
  21. }
  22. handleChangeFileList(fileList) {
  23. this.setState({ fileList });
  24. }
  25. handleChangeEmoji(emojiId) {
  26. let { value } = this.state;
  27. value += `[${emojiId}]`;
  28. this.setState({ value });
  29. React.Children.forEach(this.props.content, child => {
  30. // 如果 Editor 的父组件传入了 onChange 事件,则需要将改变之后的值传递给父组件
  31. if (child.props.onChange) {
  32. child.props.onChange(value);
  33. }
  34. });
  35. }
  36. handleUpload({ uid, path }) {
  37. const { fileMap } = this.state;
  38. fileMap[uid] = path;
  39. this.setState({ fileMap });
  40. }
  41. handleSubmit() {
  42. let { value, fileMap, fileList } = this.state;
  43. if (fileList.length) {
  44. value += "<br/>";
  45. fileList.forEach(item => {
  46. value += `[${OSS_LINK}${fileMap[item.uid]}]`;
  47. });
  48. }
  49. const { type, commentId, replyId, handleToggleInput } = this.props;
  50. if (type === "normal") {
  51. this.props.app.sCreateComment({
  52. content: value
  53. });
  54. } else if (type === "comment") {
  55. this.props.app.sCreateReply(
  56. {
  57. comment_id: commentId,
  58. content: value
  59. },
  60. () => handleToggleInput()
  61. );
  62. } else if (type === "reply") {
  63. this.props.app.sCreateReply(
  64. {
  65. comment_id: commentId,
  66. content: value,
  67. reply_id: replyId
  68. },
  69. () => handleToggleInput()
  70. );
  71. }
  72. React.Children.forEach(this.props.content, child => {
  73. // 如果 Editor 的父组件传入了 onSubmit 事件,则需要将改变之后的值传递给父组件
  74. if (child.props.onSubmit) {
  75. child.props.onSubmit(value);
  76. }
  77. });
  78. }
  79. render() {
  80. const { type } = this.props;
  81. const { value, fileList } = this.state;
  82. const childrenWithProps = React.Children.map(this.props.content, child => {
  83. return React.cloneElement(child, {
  84. value: value,
  85. fileList: fileList,
  86. onChange: this.handleChange,
  87. onSubmit: this.handleSubmit,
  88. onChangeFileList: this.handleChangeFileList,
  89. onChangeEmoji: this.handleChangeEmoji,
  90. onUpload: this.handleUpload,
  91. loading: this.props.app.loading.sCreateComment,
  92. ...child.props
  93. });
  94. });
  95. return (
  96. <div>
  97. {type === "normal" ? (
  98. <div>
  99. <span
  100. style={{
  101. border: "1px solid #CECECE",
  102. color: "#666",
  103. padding: "2px 3px"
  104. }}
  105. >
  106. 回复
  107. </span>
  108. <span style={{ marginLeft: "20px", color: "#5198EB" }}>
  109. 口碑
  110. <span style={{ marginLeft: "20px", color: "#666666" }}>
  111. (全站挑出毛病或提出合理建议,奖励10到100元红包)
  112. </span>
  113. </span>
  114. </div>
  115. ) : null}
  116. <div style={{ marginTop: 40 }}>{childrenWithProps}</div>
  117. </div>
  118. );
  119. }
  120. }
  121. CommentInput.propTypes = {
  122. // normal 有切换回复/口碑的 header ; comment 评论输入框 / reply 回复输入框
  123. type: PropTypes.oneOf(["normal", "comment", "reply"])
  124. };
  125. CommentInput.defaultProps = {
  126. type: "normal"
  127. };
  128. export default Comment(CommentInput);