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.tsx 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import React, { Component, RefObject } from 'react';
  2. import { WithTranslation } from 'react-i18next';
  3. import { connect } from 'react-redux';
  4. import { IReduxState, IStore } from '../../../app/types';
  5. import { isMobileBrowser } from '../../../base/environment/utils';
  6. import { translate } from '../../../base/i18n/functions';
  7. import { IconFaceSmile, IconSend } from '../../../base/icons/svg';
  8. import Button from '../../../base/ui/components/web/Button';
  9. import Input from '../../../base/ui/components/web/Input';
  10. import { areSmileysDisabled } from '../../functions';
  11. import SmileysPanel from './SmileysPanel';
  12. /**
  13. * The type of the React {@code Component} props of {@link ChatInput}.
  14. */
  15. interface IProps extends WithTranslation {
  16. /**
  17. * Whether chat emoticons are disabled.
  18. */
  19. _areSmileysDisabled: boolean;
  20. /**
  21. * Invoked to send chat messages.
  22. */
  23. dispatch: IStore['dispatch'];
  24. /**
  25. * Callback to invoke on message send.
  26. */
  27. onSend: Function;
  28. }
  29. /**
  30. * The type of the React {@code Component} state of {@link ChatInput}.
  31. */
  32. interface IState {
  33. /**
  34. * User provided nickname when the input text is provided in the view.
  35. */
  36. message: string;
  37. /**
  38. * Whether or not the smiley selector is visible.
  39. */
  40. showSmileysPanel: boolean;
  41. }
  42. /**
  43. * Implements a React Component for drafting and submitting a chat message.
  44. *
  45. * @augments Component
  46. */
  47. class ChatInput extends Component<IProps, IState> {
  48. _textArea?: RefObject<HTMLTextAreaElement>;
  49. state = {
  50. message: '',
  51. showSmileysPanel: false
  52. };
  53. /**
  54. * Initializes a new {@code ChatInput} instance.
  55. *
  56. * @param {Object} props - The read-only properties with which the new
  57. * instance is to be initialized.
  58. */
  59. constructor(props: IProps) {
  60. super(props);
  61. this._textArea = React.createRef<HTMLTextAreaElement>();
  62. // Bind event handlers so they are only bound once for every instance.
  63. this._onDetectSubmit = this._onDetectSubmit.bind(this);
  64. this._onMessageChange = this._onMessageChange.bind(this);
  65. this._onSmileySelect = this._onSmileySelect.bind(this);
  66. this._onSubmitMessage = this._onSubmitMessage.bind(this);
  67. this._toggleSmileysPanel = this._toggleSmileysPanel.bind(this);
  68. }
  69. /**
  70. * Implements React's {@link Component#componentDidMount()}.
  71. *
  72. * @inheritdoc
  73. */
  74. componentDidMount() {
  75. if (isMobileBrowser()) {
  76. // Ensure textarea is not focused when opening chat on mobile browser.
  77. this._textArea?.current && this._textArea.current.blur();
  78. }
  79. }
  80. /**
  81. * Implements React's {@link Component#render()}.
  82. *
  83. * @inheritdoc
  84. * @returns {ReactElement}
  85. */
  86. render() {
  87. return (
  88. <div className = { `chat-input-container${this.state.message.trim().length ? ' populated' : ''}` }>
  89. <div id = 'chat-input' >
  90. {!this.props._areSmileysDisabled && this.state.showSmileysPanel && (
  91. <div
  92. className = 'smiley-input'>
  93. <div
  94. className = 'smileys-panel' >
  95. <SmileysPanel
  96. onSmileySelect = { this._onSmileySelect } />
  97. </div>
  98. </div>
  99. )}
  100. <Input
  101. className = 'chat-input'
  102. icon = { this.props._areSmileysDisabled ? undefined : IconFaceSmile }
  103. iconClick = { this._toggleSmileysPanel }
  104. maxRows = { 5 }
  105. onChange = { this._onMessageChange }
  106. onKeyPress = { this._onDetectSubmit }
  107. placeholder = { this.props.t('chat.messagebox') }
  108. ref = { this._textArea }
  109. textarea = { true }
  110. value = { this.state.message } />
  111. <Button
  112. accessibilityLabel = { this.props.t('chat.sendButton') }
  113. disabled = { !this.state.message.trim() }
  114. icon = { IconSend }
  115. onClick = { this._onSubmitMessage }
  116. size = { isMobileBrowser() ? 'large' : 'medium' } />
  117. </div>
  118. </div>
  119. );
  120. }
  121. /**
  122. * Place cursor focus on this component's text area.
  123. *
  124. * @private
  125. * @returns {void}
  126. */
  127. _focus() {
  128. this._textArea?.current && this._textArea.current.focus();
  129. }
  130. /**
  131. * Submits the message to the chat window.
  132. *
  133. * @returns {void}
  134. */
  135. _onSubmitMessage() {
  136. const trimmed = this.state.message.trim();
  137. if (trimmed) {
  138. this.props.onSend(trimmed);
  139. this.setState({ message: '' });
  140. // Keep the textarea in focus when sending messages via submit button.
  141. this._focus();
  142. }
  143. }
  144. /**
  145. * Detects if enter has been pressed. If so, submit the message in the chat
  146. * window.
  147. *
  148. * @param {string} event - Keyboard event.
  149. * @private
  150. * @returns {void}
  151. */
  152. _onDetectSubmit(event: any) {
  153. // Composition events used to add accents to characters
  154. // despite their absence from standard US keyboards,
  155. // to build up logograms of many Asian languages
  156. // from their base components or categories and so on.
  157. if (event.isComposing || event.keyCode === 229) {
  158. // keyCode 229 means that user pressed some button,
  159. // but input method is still processing that.
  160. // This is a standard behavior for some input methods
  161. // like entering japanese or сhinese hieroglyphs.
  162. return;
  163. }
  164. if (event.key === 'Enter'
  165. && event.shiftKey === false
  166. && event.ctrlKey === false) {
  167. event.preventDefault();
  168. event.stopPropagation();
  169. this._onSubmitMessage();
  170. }
  171. }
  172. /**
  173. * Updates the known message the user is drafting.
  174. *
  175. * @param {string} value - Keyboard event.
  176. * @private
  177. * @returns {void}
  178. */
  179. _onMessageChange(value: string) {
  180. this.setState({ message: value });
  181. }
  182. /**
  183. * Appends a selected smileys to the chat message draft.
  184. *
  185. * @param {string} smileyText - The value of the smiley to append to the
  186. * chat message.
  187. * @private
  188. * @returns {void}
  189. */
  190. _onSmileySelect(smileyText: string) {
  191. if (smileyText) {
  192. this.setState({
  193. message: `${this.state.message} ${smileyText}`,
  194. showSmileysPanel: false
  195. });
  196. } else {
  197. this.setState({
  198. showSmileysPanel: false
  199. });
  200. }
  201. this._focus();
  202. }
  203. /**
  204. * Callback invoked to hide or show the smileys selector.
  205. *
  206. * @private
  207. * @returns {void}
  208. */
  209. _toggleSmileysPanel() {
  210. if (this.state.showSmileysPanel) {
  211. this._focus();
  212. }
  213. this.setState({ showSmileysPanel: !this.state.showSmileysPanel });
  214. }
  215. }
  216. /**
  217. * Function that maps parts of Redux state tree into component props.
  218. *
  219. * @param {Object} state - Redux state.
  220. * @private
  221. * @returns {{
  222. * _areSmileysDisabled: boolean
  223. * }}
  224. */
  225. const mapStateToProps = (state: IReduxState) => {
  226. return {
  227. _areSmileysDisabled: areSmileysDisabled(state)
  228. };
  229. };
  230. export default translate(connect(mapStateToProps)(ChatInput));