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.

ChatInputBar.tsx 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import React, { Component } from 'react';
  2. import { WithTranslation } from 'react-i18next';
  3. import { Platform, View, ViewStyle } from 'react-native';
  4. import { connect } from 'react-redux';
  5. import { IReduxState } from '../../../app/types';
  6. import { translate } from '../../../base/i18n/functions';
  7. import { IconSend } from '../../../base/icons/svg';
  8. import { ASPECT_RATIO_WIDE } from '../../../base/responsive-ui/constants';
  9. import IconButton from '../../../base/ui/components/native/IconButton';
  10. import Input from '../../../base/ui/components/native/Input';
  11. import { BUTTON_TYPES } from '../../../base/ui/constants.native';
  12. import styles from './styles';
  13. interface IProps extends WithTranslation {
  14. /**
  15. * Application's aspect ratio.
  16. */
  17. aspectRatio: Symbol;
  18. /**
  19. * Callback to invoke on message send.
  20. */
  21. onSend: Function;
  22. }
  23. interface IState {
  24. /**
  25. * Boolean to show if an extra padding needs to be added to the bar.
  26. */
  27. addPadding: boolean;
  28. /**
  29. * The value of the input field.
  30. */
  31. message: string;
  32. /**
  33. * Boolean to show or hide the send button.
  34. */
  35. showSend: boolean;
  36. }
  37. /**
  38. * Implements the chat input bar with text field and action(s).
  39. */
  40. class ChatInputBar extends Component<IProps, IState> {
  41. /**
  42. * Instantiates a new instance of the component.
  43. *
  44. * @inheritdoc
  45. */
  46. constructor(props: IProps) {
  47. super(props);
  48. this.state = {
  49. addPadding: false,
  50. message: '',
  51. showSend: false
  52. };
  53. this._onChangeText = this._onChangeText.bind(this);
  54. this._onFocused = this._onFocused.bind(this);
  55. this._onSubmit = this._onSubmit.bind(this);
  56. }
  57. /**
  58. * Implements {@code Component#render}.
  59. *
  60. * @inheritdoc
  61. */
  62. render() {
  63. let inputBarStyles;
  64. if (this.props.aspectRatio === ASPECT_RATIO_WIDE) {
  65. inputBarStyles = styles.inputBarWide;
  66. } else {
  67. inputBarStyles = styles.inputBarNarrow;
  68. }
  69. return (
  70. <View
  71. id = 'chat-input'
  72. style = { [
  73. inputBarStyles,
  74. this.state.addPadding ? styles.extraBarPadding : null
  75. ] as ViewStyle[] }>
  76. <Input
  77. blurOnSubmit = { false }
  78. customStyles = {{ container: styles.customInputContainer }}
  79. id = 'chat-input-messagebox'
  80. multiline = { false }
  81. onBlur = { this._onFocused(false) }
  82. onChange = { this._onChangeText }
  83. onFocus = { this._onFocused(true) }
  84. onSubmitEditing = { this._onSubmit }
  85. placeholder = { this.props.t('chat.fieldPlaceHolder') }
  86. returnKeyType = 'send'
  87. value = { this.state.message } />
  88. <IconButton
  89. disabled = { !this.state.message }
  90. id = { this.props.t('chat.sendButton') }
  91. onPress = { this._onSubmit }
  92. src = { IconSend }
  93. type = { BUTTON_TYPES.PRIMARY } />
  94. </View>
  95. );
  96. }
  97. /**
  98. * Callback to handle the change of the value of the text field.
  99. *
  100. * @param {string} text - The current value of the field.
  101. * @returns {void}
  102. */
  103. _onChangeText(text: string) {
  104. this.setState({
  105. message: text,
  106. showSend: Boolean(text)
  107. });
  108. }
  109. /**
  110. * Constructs a callback to be used to update the padding of the field if necessary.
  111. *
  112. * @param {boolean} focused - True of the field is focused.
  113. * @returns {Function}
  114. */
  115. _onFocused(focused: boolean) {
  116. return () => {
  117. Platform.OS === 'android' && this.setState({
  118. addPadding: focused
  119. });
  120. };
  121. }
  122. /**
  123. * Callback to handle the submit event of the text field.
  124. *
  125. * @returns {void}
  126. */
  127. _onSubmit() {
  128. const message = this.state.message.trim();
  129. message && this.props.onSend(message);
  130. this.setState({
  131. message: '',
  132. showSend: false
  133. });
  134. }
  135. }
  136. /**
  137. * Maps part of the Redux state to the props of this component.
  138. *
  139. * @param {Object} state - The redux state.
  140. * @private
  141. * @returns {IProps}
  142. */
  143. function _mapStateToProps(state: IReduxState) {
  144. const { aspectRatio } = state['features/base/responsive-ui'];
  145. return {
  146. aspectRatio
  147. };
  148. }
  149. export default translate(connect(_mapStateToProps)(ChatInputBar));