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.6KB

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