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

ChatInput.js 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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._onEscHandler = this._onEscHandler.bind(this);
  72. this._onToggleSmileysPanelKeyPress = this._onToggleSmileysPanelKeyPress.bind(this);
  73. this._onSubmitMessageKeyPress = this._onSubmitMessageKeyPress.bind(this);
  74. this._setTextAreaRef = this._setTextAreaRef.bind(this);
  75. }
  76. /**
  77. * Implements React's {@link Component#componentDidMount()}.
  78. *
  79. * @inheritdoc
  80. */
  81. componentDidMount() {
  82. if (isMobileBrowser()) {
  83. // Ensure textarea is not focused when opening chat on mobile browser.
  84. this._textArea && this._textArea.blur();
  85. }
  86. }
  87. /**
  88. * Implements React's {@link Component#render()}.
  89. *
  90. * @inheritdoc
  91. * @returns {ReactElement}
  92. */
  93. render() {
  94. const smileysPanelClassName = `${this.state.showSmileysPanel
  95. ? 'show-smileys' : 'hide-smileys'} smileys-panel`;
  96. return (
  97. <div className = { `chat-input-container${this.state.message.trim().length ? ' populated' : ''}` }>
  98. <div id = 'chat-input' >
  99. <div className = 'smiley-input'>
  100. <div id = 'smileysarea'>
  101. <div id = 'smileys'>
  102. <div
  103. aria-expanded = { this.state.showSmileysPanel }
  104. aria-haspopup = 'smileysContainer'
  105. aria-label = { this.props.t('chat.smileysPanel') }
  106. className = 'smiley-button'
  107. onClick = { this._onToggleSmileysPanel }
  108. onKeyDown = { this._onEscHandler }
  109. onKeyPress = { this._onToggleSmileysPanelKeyPress }
  110. role = 'button'
  111. tabIndex = { 0 }>
  112. <Icon src = { IconSmile } />
  113. </div>
  114. </div>
  115. </div>
  116. <div className = { smileysPanelClassName }>
  117. <SmileysPanel
  118. onSmileySelect = { this._onSmileySelect } />
  119. </div>
  120. </div>
  121. <div className = 'usrmsg-form'>
  122. <TextareaAutosize
  123. autoComplete = 'off'
  124. autoFocus = { true }
  125. id = 'usermsg'
  126. maxRows = { 5 }
  127. onChange = { this._onMessageChange }
  128. onHeightChange = { this.props.onResize }
  129. onKeyDown = { this._onDetectSubmit }
  130. placeholder = { this.props.t('chat.messagebox') }
  131. ref = { this._setTextAreaRef }
  132. tabIndex = { 0 }
  133. value = { this.state.message } />
  134. </div>
  135. <div className = 'send-button-container'>
  136. <div
  137. aria-label = { this.props.t('chat.sendButton') }
  138. className = 'send-button'
  139. onClick = { this._onSubmitMessage }
  140. onKeyPress = { this._onSubmitMessageKeyPress }
  141. role = 'button'
  142. tabIndex = { this.state.message.trim() ? 0 : -1 } >
  143. <Icon src = { IconPlane } />
  144. </div>
  145. </div>
  146. </div>
  147. </div>
  148. );
  149. }
  150. /**
  151. * Place cursor focus on this component's text area.
  152. *
  153. * @private
  154. * @returns {void}
  155. */
  156. _focus() {
  157. this._textArea && this._textArea.focus();
  158. }
  159. _onSubmitMessage: () => void;
  160. /**
  161. * Submits the message to the chat window.
  162. *
  163. * @returns {void}
  164. */
  165. _onSubmitMessage() {
  166. const trimmed = this.state.message.trim();
  167. if (trimmed) {
  168. this.props.onSend(trimmed);
  169. this.setState({ message: '' });
  170. // Keep the textarea in focus when sending messages via submit button.
  171. this._focus();
  172. }
  173. }
  174. _onDetectSubmit: (Object) => void;
  175. /**
  176. * Detects if enter has been pressed. If so, submit the message in the chat
  177. * window.
  178. *
  179. * @param {string} event - Keyboard event.
  180. * @private
  181. * @returns {void}
  182. */
  183. _onDetectSubmit(event) {
  184. if (event.key === 'Enter'
  185. && event.shiftKey === false
  186. && event.ctrlKey === false) {
  187. event.preventDefault();
  188. event.stopPropagation();
  189. this._onSubmitMessage();
  190. }
  191. }
  192. _onSubmitMessageKeyPress: (Object) => void;
  193. /**
  194. * KeyPress handler for accessibility.
  195. *
  196. * @param {Object} e - The key event to handle.
  197. *
  198. * @returns {void}
  199. */
  200. _onSubmitMessageKeyPress(e) {
  201. if (e.key === ' ' || e.key === 'Enter') {
  202. e.preventDefault();
  203. this._onSubmitMessage();
  204. }
  205. }
  206. _onMessageChange: (Object) => void;
  207. /**
  208. * Updates the known message the user is drafting.
  209. *
  210. * @param {string} event - Keyboard event.
  211. * @private
  212. * @returns {void}
  213. */
  214. _onMessageChange(event) {
  215. this.setState({ message: event.target.value });
  216. }
  217. _onSmileySelect: (string) => void;
  218. /**
  219. * Appends a selected smileys to the chat message draft.
  220. *
  221. * @param {string} smileyText - The value of the smiley to append to the
  222. * chat message.
  223. * @private
  224. * @returns {void}
  225. */
  226. _onSmileySelect(smileyText) {
  227. if (smileyText) {
  228. this.setState({
  229. message: `${this.state.message} ${smileyText}`,
  230. showSmileysPanel: false
  231. });
  232. } else {
  233. this.setState({
  234. showSmileysPanel: false
  235. });
  236. }
  237. this._focus();
  238. }
  239. _onToggleSmileysPanel: () => void;
  240. /**
  241. * Callback invoked to hide or show the smileys selector.
  242. *
  243. * @private
  244. * @returns {void}
  245. */
  246. _onToggleSmileysPanel() {
  247. if (this.state.showSmileysPanel) {
  248. this._focus();
  249. }
  250. this.setState({ showSmileysPanel: !this.state.showSmileysPanel });
  251. }
  252. _onEscHandler: (Object) => void;
  253. /**
  254. * KeyPress handler for accessibility.
  255. *
  256. * @param {Object} e - The key event to handle.
  257. *
  258. * @returns {void}
  259. */
  260. _onEscHandler(e) {
  261. // Escape handling does not work in onKeyPress
  262. if (this.state.showSmileysPanel && e.key === 'Escape') {
  263. e.preventDefault();
  264. e.stopPropagation();
  265. this._onToggleSmileysPanel();
  266. }
  267. }
  268. _onToggleSmileysPanelKeyPress: (Object) => void;
  269. /**
  270. * KeyPress handler for accessibility.
  271. *
  272. * @param {Object} e - The key event to handle.
  273. *
  274. * @returns {void}
  275. */
  276. _onToggleSmileysPanelKeyPress(e) {
  277. if (e.key === ' ' || e.key === 'Enter') {
  278. e.preventDefault();
  279. this._onToggleSmileysPanel();
  280. }
  281. }
  282. _setTextAreaRef: (?HTMLTextAreaElement) => void;
  283. /**
  284. * Sets the reference to the HTML TextArea.
  285. *
  286. * @param {HTMLAudioElement} textAreaElement - The HTML text area element.
  287. * @private
  288. * @returns {void}
  289. */
  290. _setTextAreaRef(textAreaElement: ?HTMLTextAreaElement) {
  291. this._textArea = textAreaElement;
  292. }
  293. }
  294. export default translate(connect()(ChatInput));