您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ChatInput.js 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // @flow
  2. import React, { Component } from 'react';
  3. import TextareaAutosize from 'react-textarea-autosize';
  4. import type { Dispatch } from 'redux';
  5. import { isMobileBrowser } from '../../../base/environment/utils';
  6. import { translate } from '../../../base/i18n';
  7. import { Icon, IconPlane, IconSmile } from '../../../base/icons';
  8. import { connect } from '../../../base/redux';
  9. import SmileysPanel from './SmileysPanel';
  10. /**
  11. * The type of the React {@code Component} props of {@link ChatInput}.
  12. */
  13. type Props = {
  14. /**
  15. * Invoked to send chat messages.
  16. */
  17. dispatch: Dispatch<any>,
  18. /**
  19. * Optional callback to invoke when the chat textarea has auto-resized to
  20. * fit overflowing text.
  21. */
  22. onResize: ?Function,
  23. /**
  24. * Callback to invoke on message send.
  25. */
  26. onSend: Function,
  27. /**
  28. * Invoked to obtain translated strings.
  29. */
  30. t: Function
  31. };
  32. /**
  33. * The type of the React {@code Component} state of {@link ChatInput}.
  34. */
  35. type State = {
  36. /**
  37. * User provided nickname when the input text is provided in the view.
  38. */
  39. message: string,
  40. /**
  41. * Whether or not the smiley selector is visible.
  42. */
  43. showSmileysPanel: boolean
  44. };
  45. /**
  46. * Implements a React Component for drafting and submitting a chat message.
  47. *
  48. * @extends Component
  49. */
  50. class ChatInput extends Component<Props, State> {
  51. _textArea: ?HTMLTextAreaElement;
  52. state = {
  53. message: '',
  54. showSmileysPanel: false
  55. };
  56. /**
  57. * Initializes a new {@code ChatInput} instance.
  58. *
  59. * @param {Object} props - The read-only properties with which the new
  60. * instance is to be initialized.
  61. */
  62. constructor(props: Props) {
  63. super(props);
  64. this._textArea = null;
  65. // Bind event handlers so they are only bound once for every instance.
  66. this._onDetectSubmit = this._onDetectSubmit.bind(this);
  67. this._onMessageChange = this._onMessageChange.bind(this);
  68. this._onSmileySelect = this._onSmileySelect.bind(this);
  69. this._onSubmitMessage = this._onSubmitMessage.bind(this);
  70. this._onToggleSmileysPanel = this._onToggleSmileysPanel.bind(this);
  71. this._setTextAreaRef = this._setTextAreaRef.bind(this);
  72. }
  73. /**
  74. * Implements React's {@link Component#componentDidMount()}.
  75. *
  76. * @inheritdoc
  77. */
  78. componentDidMount() {
  79. if (isMobileBrowser()) {
  80. // Ensure textarea is not focused when opening chat on mobile browser.
  81. this._textArea && this._textArea.blur();
  82. }
  83. }
  84. /**
  85. * Implements React's {@link Component#render()}.
  86. *
  87. * @inheritdoc
  88. * @returns {ReactElement}
  89. */
  90. render() {
  91. const smileysPanelClassName = `${this.state.showSmileysPanel
  92. ? 'show-smileys' : 'hide-smileys'} smileys-panel`;
  93. return (
  94. <div className = { `chat-input-container${this.state.message.trim().length ? ' populated' : ''}` }>
  95. <div id = 'chat-input' >
  96. <div className = 'smiley-input'>
  97. <div id = 'smileysarea'>
  98. <div id = 'smileys'>
  99. <div
  100. className = 'smiley-button'
  101. onClick = { this._onToggleSmileysPanel }>
  102. <Icon src = { IconSmile } />
  103. </div>
  104. </div>
  105. </div>
  106. <div className = { smileysPanelClassName }>
  107. <SmileysPanel
  108. onSmileySelect = { this._onSmileySelect } />
  109. </div>
  110. </div>
  111. <div className = 'usrmsg-form'>
  112. <TextareaAutosize
  113. id = 'usermsg'
  114. inputRef = { this._setTextAreaRef }
  115. maxRows = { 5 }
  116. onChange = { this._onMessageChange }
  117. onHeightChange = { this.props.onResize }
  118. onKeyDown = { this._onDetectSubmit }
  119. placeholder = { this.props.t('chat.messagebox') }
  120. value = { this.state.message } />
  121. </div>
  122. <div className = 'send-button-container'>
  123. <div
  124. className = 'send-button'
  125. onClick = { this._onSubmitMessage }>
  126. <Icon src = { IconPlane } />
  127. </div>
  128. </div>
  129. </div>
  130. </div>
  131. );
  132. }
  133. /**
  134. * Place cursor focus on this component's text area.
  135. *
  136. * @private
  137. * @returns {void}
  138. */
  139. _focus() {
  140. this._textArea && this._textArea.focus();
  141. }
  142. _onSubmitMessage: () => void;
  143. /**
  144. * Submits the message to the chat window.
  145. *
  146. * @returns {void}
  147. */
  148. _onSubmitMessage() {
  149. const trimmed = this.state.message.trim();
  150. if (trimmed) {
  151. this.props.onSend(trimmed);
  152. this.setState({ message: '' });
  153. // Keep the textarea in focus when sending messages via submit button.
  154. this._focus();
  155. }
  156. }
  157. _onDetectSubmit: (Object) => void;
  158. /**
  159. * Detects if enter has been pressed. If so, submit the message in the chat
  160. * window.
  161. *
  162. * @param {string} event - Keyboard event.
  163. * @private
  164. * @returns {void}
  165. */
  166. _onDetectSubmit(event) {
  167. if (event.keyCode === 13
  168. && event.shiftKey === false) {
  169. event.preventDefault();
  170. this._onSubmitMessage();
  171. }
  172. }
  173. _onMessageChange: (Object) => void;
  174. /**
  175. * Updates the known message the user is drafting.
  176. *
  177. * @param {string} event - Keyboard event.
  178. * @private
  179. * @returns {void}
  180. */
  181. _onMessageChange(event) {
  182. this.setState({ message: event.target.value });
  183. }
  184. _onSmileySelect: (string) => void;
  185. /**
  186. * Appends a selected smileys to the chat message draft.
  187. *
  188. * @param {string} smileyText - The value of the smiley to append to the
  189. * chat message.
  190. * @private
  191. * @returns {void}
  192. */
  193. _onSmileySelect(smileyText) {
  194. this.setState({
  195. message: `${this.state.message} ${smileyText}`,
  196. showSmileysPanel: false
  197. });
  198. this._focus();
  199. }
  200. _onToggleSmileysPanel: () => void;
  201. /**
  202. * Callback invoked to hide or show the smileys selector.
  203. *
  204. * @private
  205. * @returns {void}
  206. */
  207. _onToggleSmileysPanel() {
  208. this.setState({ showSmileysPanel: !this.state.showSmileysPanel });
  209. this._focus();
  210. }
  211. _setTextAreaRef: (?HTMLTextAreaElement) => void;
  212. /**
  213. * Sets the reference to the HTML TextArea.
  214. *
  215. * @param {HTMLAudioElement} textAreaElement - The HTML text area element.
  216. * @private
  217. * @returns {void}
  218. */
  219. _setTextAreaRef(textAreaElement: ?HTMLTextAreaElement) {
  220. this._textArea = textAreaElement;
  221. }
  222. }
  223. export default translate(connect()(ChatInput));