Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ChatInput.tsx 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. } else {
  79. this._focus();
  80. }
  81. }
  82. /**
  83. * Implements React's {@link Component#render()}.
  84. *
  85. * @inheritdoc
  86. * @returns {ReactElement}
  87. */
  88. render() {
  89. return (
  90. <div className = { `chat-input-container${this.state.message.trim().length ? ' populated' : ''}` }>
  91. <div id = 'chat-input' >
  92. {!this.props._areSmileysDisabled && this.state.showSmileysPanel && (
  93. <div
  94. className = 'smiley-input'>
  95. <div
  96. className = 'smileys-panel' >
  97. <SmileysPanel
  98. onSmileySelect = { this._onSmileySelect } />
  99. </div>
  100. </div>
  101. )}
  102. <Input
  103. className = 'chat-input'
  104. icon = { this.props._areSmileysDisabled ? undefined : IconFaceSmile }
  105. iconClick = { this._toggleSmileysPanel }
  106. maxRows = { 5 }
  107. onChange = { this._onMessageChange }
  108. onKeyPress = { this._onDetectSubmit }
  109. placeholder = { this.props.t('chat.messagebox') }
  110. ref = { this._textArea }
  111. textarea = { true }
  112. value = { this.state.message } />
  113. <Button
  114. accessibilityLabel = { this.props.t('chat.sendButton') }
  115. disabled = { !this.state.message.trim() }
  116. icon = { IconSend }
  117. onClick = { this._onSubmitMessage }
  118. size = { isMobileBrowser() ? 'large' : 'medium' } />
  119. </div>
  120. </div>
  121. );
  122. }
  123. /**
  124. * Place cursor focus on this component's text area.
  125. *
  126. * @private
  127. * @returns {void}
  128. */
  129. _focus() {
  130. this._textArea?.current && this._textArea.current.focus();
  131. }
  132. /**
  133. * Submits the message to the chat window.
  134. *
  135. * @returns {void}
  136. */
  137. _onSubmitMessage() {
  138. const trimmed = this.state.message.trim();
  139. if (trimmed) {
  140. this.props.onSend(trimmed);
  141. this.setState({ message: '' });
  142. // Keep the textarea in focus when sending messages via submit button.
  143. this._focus();
  144. }
  145. }
  146. /**
  147. * Detects if enter has been pressed. If so, submit the message in the chat
  148. * window.
  149. *
  150. * @param {string} event - Keyboard event.
  151. * @private
  152. * @returns {void}
  153. */
  154. _onDetectSubmit(event: any) {
  155. // Composition events used to add accents to characters
  156. // despite their absence from standard US keyboards,
  157. // to build up logograms of many Asian languages
  158. // from their base components or categories and so on.
  159. if (event.isComposing || event.keyCode === 229) {
  160. // keyCode 229 means that user pressed some button,
  161. // but input method is still processing that.
  162. // This is a standard behavior for some input methods
  163. // like entering japanese or сhinese hieroglyphs.
  164. return;
  165. }
  166. if (event.key === 'Enter'
  167. && event.shiftKey === false
  168. && event.ctrlKey === false) {
  169. event.preventDefault();
  170. event.stopPropagation();
  171. this._onSubmitMessage();
  172. }
  173. }
  174. /**
  175. * Updates the known message the user is drafting.
  176. *
  177. * @param {string} value - Keyboard event.
  178. * @private
  179. * @returns {void}
  180. */
  181. _onMessageChange(value: string) {
  182. this.setState({ message: value });
  183. }
  184. /**
  185. * Appends a selected smileys to the chat message draft.
  186. *
  187. * @param {string} smileyText - The value of the smiley to append to the
  188. * chat message.
  189. * @private
  190. * @returns {void}
  191. */
  192. _onSmileySelect(smileyText: string) {
  193. if (smileyText) {
  194. this.setState({
  195. message: `${this.state.message} ${smileyText}`,
  196. showSmileysPanel: false
  197. });
  198. } else {
  199. this.setState({
  200. showSmileysPanel: false
  201. });
  202. }
  203. this._focus();
  204. }
  205. /**
  206. * Callback invoked to hide or show the smileys selector.
  207. *
  208. * @private
  209. * @returns {void}
  210. */
  211. _toggleSmileysPanel() {
  212. if (this.state.showSmileysPanel) {
  213. this._focus();
  214. }
  215. this.setState({ showSmileysPanel: !this.state.showSmileysPanel });
  216. }
  217. }
  218. /**
  219. * Function that maps parts of Redux state tree into component props.
  220. *
  221. * @param {Object} state - Redux state.
  222. * @private
  223. * @returns {{
  224. * _areSmileysDisabled: boolean
  225. * }}
  226. */
  227. const mapStateToProps = (state: IReduxState) => {
  228. return {
  229. _areSmileysDisabled: areSmileysDisabled(state)
  230. };
  231. };
  232. export default translate(connect(mapStateToProps)(ChatInput));