Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ChatInput.tsx 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import React, { Component, RefObject } from 'react';
  2. import { WithTranslation } from 'react-i18next';
  3. import { IReduxState, IStore } from '../../../app/types';
  4. import { isMobileBrowser } from '../../../base/environment/utils';
  5. import { translate } from '../../../base/i18n/functions';
  6. import { IconFaceSmile, IconSend } from '../../../base/icons/svg';
  7. import { connect } from '../../../base/redux/functions';
  8. import Button from '../../../base/ui/components/web/Button';
  9. import Input from '../../../base/ui/components/web/Input';
  10. import { areSmileysDisabled } from '../../functions';
  11. // @ts-ignore
  12. import SmileysPanel from './SmileysPanel';
  13. /**
  14. * The type of the React {@code Component} props of {@link ChatInput}.
  15. */
  16. interface IProps extends WithTranslation {
  17. /**
  18. * Whether chat emoticons are disabled.
  19. */
  20. _areSmileysDisabled: boolean;
  21. /**
  22. * Invoked to send chat messages.
  23. */
  24. dispatch: IStore['dispatch'];
  25. /**
  26. * Callback to invoke on message send.
  27. */
  28. onSend: Function;
  29. }
  30. /**
  31. * The type of the React {@code Component} state of {@link ChatInput}.
  32. */
  33. interface IState {
  34. /**
  35. * User provided nickname when the input text is provided in the view.
  36. */
  37. message: string;
  38. /**
  39. * Whether or not the smiley selector is visible.
  40. */
  41. showSmileysPanel: boolean;
  42. }
  43. /**
  44. * Implements a React Component for drafting and submitting a chat message.
  45. *
  46. * @augments Component
  47. */
  48. class ChatInput extends Component<IProps, IState> {
  49. _textArea?: RefObject<HTMLTextAreaElement>;
  50. state = {
  51. message: '',
  52. showSmileysPanel: false
  53. };
  54. /**
  55. * Initializes a new {@code ChatInput} instance.
  56. *
  57. * @param {Object} props - The read-only properties with which the new
  58. * instance is to be initialized.
  59. */
  60. constructor(props: IProps) {
  61. super(props);
  62. this._textArea = React.createRef<HTMLTextAreaElement>();
  63. // Bind event handlers so they are only bound once for every instance.
  64. this._onDetectSubmit = this._onDetectSubmit.bind(this);
  65. this._onMessageChange = this._onMessageChange.bind(this);
  66. this._onSmileySelect = this._onSmileySelect.bind(this);
  67. this._onSubmitMessage = this._onSubmitMessage.bind(this);
  68. this._toggleSmileysPanel = this._toggleSmileysPanel.bind(this);
  69. }
  70. /**
  71. * Implements React's {@link Component#componentDidMount()}.
  72. *
  73. * @inheritdoc
  74. */
  75. componentDidMount() {
  76. if (isMobileBrowser()) {
  77. // Ensure textarea is not focused when opening chat on mobile browser.
  78. this._textArea?.current && this._textArea.current.blur();
  79. }
  80. }
  81. /**
  82. * Implements React's {@link Component#render()}.
  83. *
  84. * @inheritdoc
  85. * @returns {ReactElement}
  86. */
  87. render() {
  88. return (
  89. <div className = { `chat-input-container${this.state.message.trim().length ? ' populated' : ''}` }>
  90. <div id = 'chat-input' >
  91. {!this.props._areSmileysDisabled && this.state.showSmileysPanel && (
  92. <div
  93. className = 'smiley-input'>
  94. <div
  95. className = 'smileys-panel' >
  96. <SmileysPanel
  97. onSmileySelect = { this._onSmileySelect } />
  98. </div>
  99. </div>
  100. )}
  101. <Input
  102. autoFocus = { true }
  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));