Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ChatInput.tsx 7.7KB

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