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.9KB

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