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

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