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 6.0KB

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