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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. style = { [
  72. inputBarStyles,
  73. this.state.addPadding ? styles.extraBarPadding : null
  74. ] as ViewStyle[] }>
  75. <Input
  76. blurOnSubmit = { false }
  77. customStyles = {{ container: styles.customInputContainer }}
  78. multiline = { false }
  79. onBlur = { this._onFocused(false) }
  80. onChange = { this._onChangeText }
  81. onFocus = { this._onFocused(true) }
  82. onSubmitEditing = { this._onSubmit }
  83. placeholder = { this.props.t('chat.fieldPlaceHolder') }
  84. returnKeyType = 'send'
  85. value = { this.state.message } />
  86. <IconButton
  87. disabled = { !this.state.message }
  88. onPress = { this._onSubmit }
  89. src = { IconSend }
  90. type = { BUTTON_TYPES.PRIMARY } />
  91. </View>
  92. );
  93. }
  94. /**
  95. * Callback to handle the change of the value of the text field.
  96. *
  97. * @param {string} text - The current value of the field.
  98. * @returns {void}
  99. */
  100. _onChangeText(text: string) {
  101. this.setState({
  102. message: text,
  103. showSend: Boolean(text)
  104. });
  105. }
  106. /**
  107. * Constructs a callback to be used to update the padding of the field if necessary.
  108. *
  109. * @param {boolean} focused - True of the field is focused.
  110. * @returns {Function}
  111. */
  112. _onFocused(focused: boolean) {
  113. return () => {
  114. Platform.OS === 'android' && this.setState({
  115. addPadding: focused
  116. });
  117. };
  118. }
  119. /**
  120. * Callback to handle the submit event of the text field.
  121. *
  122. * @returns {void}
  123. */
  124. _onSubmit() {
  125. const message = this.state.message.trim();
  126. message && this.props.onSend(message);
  127. this.setState({
  128. message: '',
  129. showSend: false
  130. });
  131. }
  132. }
  133. /**
  134. * Maps part of the Redux state to the props of this component.
  135. *
  136. * @param {Object} state - The redux state.
  137. * @private
  138. * @returns {IProps}
  139. */
  140. function _mapStateToProps(state: IReduxState) {
  141. const { aspectRatio } = state['features/base/responsive-ui'];
  142. return {
  143. aspectRatio
  144. };
  145. }
  146. export default translate(connect(_mapStateToProps)(ChatInputBar));