You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChatInput.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 { areSmileysDisabled } from '../../functions';
  10. import SmileysPanel from './SmileysPanel';
  11. /**
  12. * The type of the React {@code Component} props of {@link ChatInput}.
  13. */
  14. type Props = {
  15. /**
  16. * Invoked to send chat messages.
  17. */
  18. dispatch: Dispatch<any>,
  19. /**
  20. * Optional callback to invoke when the chat textarea has auto-resized to
  21. * fit overflowing text.
  22. */
  23. onResize: ?Function,
  24. /**
  25. * Callback to invoke on message send.
  26. */
  27. onSend: Function,
  28. /**
  29. * Invoked to obtain translated strings.
  30. */
  31. t: Function,
  32. /**
  33. * Whether chat emoticons are disabled.
  34. */
  35. _areSmileysDisabled: boolean
  36. };
  37. /**
  38. * The type of the React {@code Component} state of {@link ChatInput}.
  39. */
  40. type State = {
  41. /**
  42. * User provided nickname when the input text is provided in the view.
  43. */
  44. message: string,
  45. /**
  46. * Whether or not the smiley selector is visible.
  47. */
  48. showSmileysPanel: boolean
  49. };
  50. /**
  51. * Implements a React Component for drafting and submitting a chat message.
  52. *
  53. * @extends Component
  54. */
  55. class ChatInput extends Component<Props, State> {
  56. _textArea: ?HTMLTextAreaElement;
  57. state = {
  58. message: '',
  59. showSmileysPanel: false
  60. };
  61. /**
  62. * Initializes a new {@code ChatInput} instance.
  63. *
  64. * @param {Object} props - The read-only properties with which the new
  65. * instance is to be initialized.
  66. */
  67. constructor(props: Props) {
  68. super(props);
  69. this._textArea = null;
  70. // Bind event handlers so they are only bound once for every instance.
  71. this._onDetectSubmit = this._onDetectSubmit.bind(this);
  72. this._onMessageChange = this._onMessageChange.bind(this);
  73. this._onSmileySelect = this._onSmileySelect.bind(this);
  74. this._onSubmitMessage = this._onSubmitMessage.bind(this);
  75. this._onToggleSmileysPanel = this._onToggleSmileysPanel.bind(this);
  76. this._onEscHandler = this._onEscHandler.bind(this);
  77. this._onToggleSmileysPanelKeyPress = this._onToggleSmileysPanelKeyPress.bind(this);
  78. this._onSubmitMessageKeyPress = this._onSubmitMessageKeyPress.bind(this);
  79. this._setTextAreaRef = this._setTextAreaRef.bind(this);
  80. }
  81. /**
  82. * Implements React's {@link Component#componentDidMount()}.
  83. *
  84. * @inheritdoc
  85. */
  86. componentDidMount() {
  87. if (isMobileBrowser()) {
  88. // Ensure textarea is not focused when opening chat on mobile browser.
  89. this._textArea && this._textArea.blur();
  90. }
  91. }
  92. /**
  93. * Implements React's {@link Component#render()}.
  94. *
  95. * @inheritdoc
  96. * @returns {ReactElement}
  97. */
  98. render() {
  99. const smileysPanelClassName = `${this.state.showSmileysPanel
  100. ? 'show-smileys' : 'hide-smileys'} smileys-panel`;
  101. return (
  102. <div className = { `chat-input-container${this.state.message.trim().length ? ' populated' : ''}` }>
  103. <div id = 'chat-input' >
  104. { this.props._areSmileysDisabled ? null : (
  105. <div className = 'smiley-input'>
  106. <div id = 'smileysarea'>
  107. <div id = 'smileys'>
  108. <div
  109. aria-expanded = { this.state.showSmileysPanel }
  110. aria-haspopup = 'smileysContainer'
  111. aria-label = { this.props.t('chat.smileysPanel') }
  112. className = 'smiley-button'
  113. onClick = { this._onToggleSmileysPanel }
  114. onKeyDown = { this._onEscHandler }
  115. onKeyPress = { this._onToggleSmileysPanelKeyPress }
  116. role = 'button'
  117. tabIndex = { 0 }>
  118. <Icon src = { IconSmile } />
  119. </div>
  120. </div>
  121. </div>
  122. <div
  123. className = { smileysPanelClassName } >
  124. <SmileysPanel
  125. onSmileySelect = { this._onSmileySelect } />
  126. </div>
  127. </div>
  128. ) }
  129. <div className = 'usrmsg-form'>
  130. <TextareaAutosize
  131. autoComplete = 'off'
  132. autoFocus = { true }
  133. id = 'usermsg'
  134. maxRows = { 5 }
  135. onChange = { this._onMessageChange }
  136. onHeightChange = { this.props.onResize }
  137. onKeyDown = { this._onDetectSubmit }
  138. placeholder = { this.props.t('chat.messagebox') }
  139. ref = { this._setTextAreaRef }
  140. tabIndex = { 0 }
  141. value = { this.state.message } />
  142. </div>
  143. <div className = 'send-button-container'>
  144. <div
  145. aria-label = { this.props.t('chat.sendButton') }
  146. className = 'send-button'
  147. onClick = { this._onSubmitMessage }
  148. onKeyPress = { this._onSubmitMessageKeyPress }
  149. role = 'button'
  150. tabIndex = { this.state.message.trim() ? 0 : -1 } >
  151. <Icon src = { IconPlane } />
  152. </div>
  153. </div>
  154. </div>
  155. </div>
  156. );
  157. }
  158. /**
  159. * Place cursor focus on this component's text area.
  160. *
  161. * @private
  162. * @returns {void}
  163. */
  164. _focus() {
  165. this._textArea && this._textArea.focus();
  166. }
  167. _onSubmitMessage: () => void;
  168. /**
  169. * Submits the message to the chat window.
  170. *
  171. * @returns {void}
  172. */
  173. _onSubmitMessage() {
  174. const trimmed = this.state.message.trim();
  175. if (trimmed) {
  176. this.props.onSend(trimmed);
  177. this.setState({ message: '' });
  178. // Keep the textarea in focus when sending messages via submit button.
  179. this._focus();
  180. }
  181. }
  182. _onDetectSubmit: (Object) => void;
  183. /**
  184. * Detects if enter has been pressed. If so, submit the message in the chat
  185. * window.
  186. *
  187. * @param {string} event - Keyboard event.
  188. * @private
  189. * @returns {void}
  190. */
  191. _onDetectSubmit(event) {
  192. if (event.key === 'Enter'
  193. && event.shiftKey === false
  194. && event.ctrlKey === false) {
  195. event.preventDefault();
  196. event.stopPropagation();
  197. this._onSubmitMessage();
  198. }
  199. }
  200. _onSubmitMessageKeyPress: (Object) => void;
  201. /**
  202. * KeyPress handler for accessibility.
  203. *
  204. * @param {Object} e - The key event to handle.
  205. *
  206. * @returns {void}
  207. */
  208. _onSubmitMessageKeyPress(e) {
  209. if (e.key === ' ' || e.key === 'Enter') {
  210. e.preventDefault();
  211. this._onSubmitMessage();
  212. }
  213. }
  214. _onMessageChange: (Object) => void;
  215. /**
  216. * Updates the known message the user is drafting.
  217. *
  218. * @param {string} event - Keyboard event.
  219. * @private
  220. * @returns {void}
  221. */
  222. _onMessageChange(event) {
  223. this.setState({ message: event.target.value });
  224. }
  225. _onSmileySelect: (string) => void;
  226. /**
  227. * Appends a selected smileys to the chat message draft.
  228. *
  229. * @param {string} smileyText - The value of the smiley to append to the
  230. * chat message.
  231. * @private
  232. * @returns {void}
  233. */
  234. _onSmileySelect(smileyText) {
  235. if (smileyText) {
  236. this.setState({
  237. message: `${this.state.message} ${smileyText}`,
  238. showSmileysPanel: false
  239. });
  240. } else {
  241. this.setState({
  242. showSmileysPanel: false
  243. });
  244. }
  245. this._focus();
  246. }
  247. _onToggleSmileysPanel: () => void;
  248. /**
  249. * Callback invoked to hide or show the smileys selector.
  250. *
  251. * @private
  252. * @returns {void}
  253. */
  254. _onToggleSmileysPanel() {
  255. if (this.state.showSmileysPanel) {
  256. this._focus();
  257. }
  258. this.setState({ showSmileysPanel: !this.state.showSmileysPanel });
  259. }
  260. _onEscHandler: (Object) => void;
  261. /**
  262. * KeyPress handler for accessibility.
  263. *
  264. * @param {Object} e - The key event to handle.
  265. *
  266. * @returns {void}
  267. */
  268. _onEscHandler(e) {
  269. // Escape handling does not work in onKeyPress
  270. if (this.state.showSmileysPanel && e.key === 'Escape') {
  271. e.preventDefault();
  272. e.stopPropagation();
  273. this._onToggleSmileysPanel();
  274. }
  275. }
  276. _onToggleSmileysPanelKeyPress: (Object) => void;
  277. /**
  278. * KeyPress handler for accessibility.
  279. *
  280. * @param {Object} e - The key event to handle.
  281. *
  282. * @returns {void}
  283. */
  284. _onToggleSmileysPanelKeyPress(e) {
  285. if (e.key === ' ' || e.key === 'Enter') {
  286. e.preventDefault();
  287. this._onToggleSmileysPanel();
  288. }
  289. }
  290. _setTextAreaRef: (?HTMLTextAreaElement) => void;
  291. /**
  292. * Sets the reference to the HTML TextArea.
  293. *
  294. * @param {HTMLAudioElement} textAreaElement - The HTML text area element.
  295. * @private
  296. * @returns {void}
  297. */
  298. _setTextAreaRef(textAreaElement: ?HTMLTextAreaElement) {
  299. this._textArea = textAreaElement;
  300. }
  301. }
  302. /**
  303. * Function that maps parts of Redux state tree into component props.
  304. *
  305. * @param {Object} state - Redux state.
  306. * @private
  307. * @returns {{
  308. * _areSmileysDisabled: boolean
  309. * }}
  310. */
  311. const mapStateToProps = state => {
  312. return {
  313. _areSmileysDisabled: areSmileysDisabled(state)
  314. };
  315. };
  316. export default translate(connect(mapStateToProps)(ChatInput));