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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. * Removes cursor focus on this component's text area.
  116. *
  117. * @returns {void}
  118. */
  119. blur() {
  120. this._textArea && this._textArea.blur();
  121. }
  122. /**
  123. * Place cursor focus on this component's text area.
  124. *
  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.dispatch(sendMessage(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. if (this.props.getChatInputRef) {
  199. this.props.getChatInputRef(textAreaElement);
  200. }
  201. }
  202. }
  203. export default translate(connect()(ChatInput));