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

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