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