index.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import React, { PureComponent } from 'react';
  2. import cls from 'classnames';
  3. import PropTypes from 'prop-types';
  4. import { FileItem } from '../../upload/interface';
  5. import type { InputBoxProps, InputBoxState } from '../interface';
  6. import { BaseComponent, Button, Upload, Tooltip, TextArea } from '../../index';
  7. import { IconDeleteStroked, IconChainStroked, IconArrowUp } from '@douyinfe/semi-icons';
  8. import { cssClasses, strings } from "@douyinfe/semi-foundation/chat/constants";
  9. import InputBoxFoundation, { InputBoxAdapter } from '@douyinfe/semi-foundation/chat/inputboxFoundation';
  10. import Attachment from '../attachment';
  11. const { PREFIX_INPUT_BOX } = cssClasses;
  12. const { SEND_HOT_KEY } = strings;
  13. const textAutoSize = { minRows: 1, maxRows: 5 };
  14. // 新增 allowSend props,控制输入和发送
  15. class InputBox extends BaseComponent<InputBoxProps, InputBoxState> {
  16. inputAreaRef: React.RefObject<any>;
  17. static propTypes = {
  18. uploadProps: PropTypes.object,
  19. };
  20. static defaultProps = {
  21. uploadProps: {}
  22. };
  23. constructor(props: InputBoxProps) {
  24. super(props);
  25. this.inputAreaRef = React.createRef();
  26. this.foundation = new InputBoxFoundation(this.adapter);
  27. this.state = {
  28. content: '',
  29. attachment: []
  30. };
  31. }
  32. get adapter(): InputBoxAdapter<InputBoxProps, InputBoxState> {
  33. return {
  34. ...super.adapter,
  35. notifyInputChange: (props: { inputValue: string; attachment: any[]}) => {
  36. const { onInputChange } = this.props;
  37. onInputChange && onInputChange(props);
  38. },
  39. setInputValue: (value) => {
  40. this.setState({
  41. content: value
  42. });
  43. },
  44. setAttachment: (attachment: any[]) => {
  45. this.setState({
  46. attachment: attachment
  47. });
  48. },
  49. notifySend: (content: string, attachment: FileItem[]) => {
  50. const { onSend } = this.props;
  51. onSend && onSend(content, attachment);
  52. }
  53. };
  54. }
  55. onClick = () => {
  56. this.inputAreaRef.current?.focus();
  57. }
  58. renderUploadButton = () => {
  59. const { uploadProps, uploadRef, uploadTipProps, clickUpload, allowSend = true } = this.props;
  60. if (!clickUpload) {
  61. return null;
  62. }
  63. const { attachment } = this.state;
  64. const { className, onChange, renderFileItem, children, ...rest } = uploadProps;
  65. const realUploadProps = {
  66. ...rest,
  67. className: cls(`${PREFIX_INPUT_BOX}-upload`, {
  68. [className]: className
  69. }),
  70. onChange: this.foundation.onAttachmentAdd,
  71. disabled: !allowSend,
  72. };
  73. const uploadNode = <Upload
  74. ref={uploadRef}
  75. fileList={attachment}
  76. {...realUploadProps}
  77. >
  78. {children ? children : <Button
  79. className={`${PREFIX_INPUT_BOX}-uploadButton`}
  80. icon={<IconChainStroked size="extra-large" />}
  81. theme='borderless'
  82. disabled={!allowSend}
  83. />}
  84. </Upload>;
  85. return (uploadTipProps ? <Tooltip {...uploadTipProps}><span>{uploadNode}</span></Tooltip> : uploadNode);
  86. }
  87. renderInputArea = () => {
  88. const { content, attachment } = this.state;
  89. const { placeholder, sendHotKey } = this.props;
  90. return (<div
  91. className={`${PREFIX_INPUT_BOX}-inputArea`}
  92. >
  93. <TextArea
  94. placeholder={placeholder}
  95. onEnterPress={this.foundation.onEnterPress}
  96. value={content}
  97. onChange={this.foundation.onInputAreaChange}
  98. ref={this.inputAreaRef}
  99. className={`${PREFIX_INPUT_BOX}-textarea`}
  100. autosize={textAutoSize}
  101. disabledEnterStartNewLine={sendHotKey === SEND_HOT_KEY.ENTER ? true : false}
  102. onPaste={this.foundation.onPaste as any}
  103. />
  104. <Attachment
  105. attachment={attachment as any}
  106. onClear={this.foundation.onAttachmentDelete}
  107. />
  108. </div>);
  109. }
  110. renderClearButton = () => {
  111. const { onClearContext } = this.props;
  112. return (
  113. <Button
  114. className={`${PREFIX_INPUT_BOX}-clearButton`}
  115. theme='borderless'
  116. icon={<IconDeleteStroked />}
  117. onClick={onClearContext}
  118. />
  119. );
  120. }
  121. renderSendButton = () => {
  122. const disabledSend = this.foundation.getDisableSend();
  123. const { allowSend = true } = this.props;
  124. return (
  125. <Button
  126. disabled={disabledSend || !allowSend}
  127. theme='solid'
  128. type='primary'
  129. className={`${PREFIX_INPUT_BOX}-sendButton`}
  130. // icon={<IconSend size="extra-large" className={`${PREFIX_INPUT_BOX}-sendButton-icon`} />}
  131. icon={<IconArrowUp size="large" className={`${PREFIX_INPUT_BOX}-sendButton-icon`} />}
  132. onClick={this.foundation.onSend}
  133. />
  134. );
  135. }
  136. render() {
  137. const { onClearContext, renderInputArea, onSend, style, className, showClearContext, allowSend = true } = this.props;
  138. const clearNode = this.renderClearButton();
  139. const uploadNode = this.renderUploadButton();
  140. const inputNode = this.renderInputArea();
  141. const sendNode = this.renderSendButton();
  142. const nodes = (
  143. <div className={cls(PREFIX_INPUT_BOX, { [className]: className })} style={style}>
  144. <div
  145. className={`${PREFIX_INPUT_BOX}-inner`}
  146. onClick={this.onClick}
  147. >
  148. {showClearContext && clearNode}
  149. <div className={`${PREFIX_INPUT_BOX}-container`}>
  150. {uploadNode}
  151. {inputNode}
  152. {sendNode}
  153. </div>
  154. </div>
  155. </div>
  156. );
  157. if (renderInputArea) {
  158. return renderInputArea({
  159. defaultNode: nodes,
  160. onClear: onClearContext,
  161. onSend: onSend,
  162. detailProps: {
  163. clearContextNode: clearNode,
  164. uploadNode: uploadNode,
  165. inputNode: inputNode,
  166. sendNode: sendNode,
  167. onClick: this.onClick,
  168. }
  169. });
  170. }
  171. return nodes;
  172. }
  173. }
  174. export default InputBox;