index.tsx 6.3 KB

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