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.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import React, { Component } from 'react';
  2. import { Platform } from 'react-native';
  3. import { SafeAreaView } from 'react-native-safe-area-context';
  4. import { translate } from '../../../base/i18n';
  5. import { IconSend } from '../../../base/icons/svg';
  6. import IconButton from '../../../base/ui/components/native/IconButton';
  7. import Input from '../../../base/ui/components/native/Input';
  8. import { BUTTON_TYPES } from '../../../base/ui/constants.native';
  9. import styles from './styles';
  10. type Props = {
  11. /**
  12. * Callback to invoke on message send.
  13. */
  14. onSend: Function,
  15. /**
  16. * Function to be used to translate i18n labels.
  17. */
  18. t: Function
  19. };
  20. type State = {
  21. /**
  22. * Boolean to show if an extra padding needs to be added to the bar.
  23. */
  24. addPadding: boolean,
  25. /**
  26. * The value of the input field.
  27. */
  28. message: string,
  29. /**
  30. * Boolean to show or hide the send button.
  31. */
  32. showSend: boolean
  33. };
  34. /**
  35. * Implements the chat input bar with text field and action(s).
  36. */
  37. class ChatInputBar extends Component<Props, State> {
  38. /**
  39. * Instantiates a new instance of the component.
  40. *
  41. * @inheritdoc
  42. */
  43. constructor(props: Props) {
  44. super(props);
  45. this.state = {
  46. addPadding: false,
  47. message: '',
  48. showSend: false
  49. };
  50. this._onChangeText = this._onChangeText.bind(this);
  51. this._onFocused = this._onFocused.bind(this);
  52. this._onSubmit = this._onSubmit.bind(this);
  53. }
  54. /**
  55. * Implements {@code Component#render}.
  56. *
  57. * @inheritdoc
  58. */
  59. render() {
  60. return (
  61. <SafeAreaView
  62. edges = { [ 'bottom' ] }
  63. style = { [
  64. styles.inputBar,
  65. this.state.addPadding ? styles.extraBarPadding : null
  66. ] }>
  67. <Input
  68. blurOnSubmit = { false }
  69. customStyles = {{ input: styles.customInput }}
  70. multiline = { false }
  71. onBlur = { this._onFocused(false) }
  72. onChange = { this._onChangeText }
  73. onFocus = { this._onFocused(true) }
  74. onSubmitEditing = { this._onSubmit }
  75. placeholder = { this.props.t('chat.fieldPlaceHolder') }
  76. returnKeyType = 'send'
  77. value = { this.state.message } />
  78. <IconButton
  79. disabled = { !this.state.message }
  80. onPress = { this._onSubmit }
  81. src = { IconSend }
  82. style = { styles.sendButton }
  83. type = { BUTTON_TYPES.PRIMARY } />
  84. </SafeAreaView>
  85. );
  86. }
  87. _onChangeText: string => void;
  88. /**
  89. * Callback to handle the change of the value of the text field.
  90. *
  91. * @param {string} text - The current value of the field.
  92. * @returns {void}
  93. */
  94. _onChangeText(text) {
  95. this.setState({
  96. message: text,
  97. showSend: Boolean(text)
  98. });
  99. }
  100. _onFocused: boolean => Function;
  101. /**
  102. * Constructs a callback to be used to update the padding of the field if necessary.
  103. *
  104. * @param {boolean} focused - True of the field is focused.
  105. * @returns {Function}
  106. */
  107. _onFocused(focused) {
  108. return () => {
  109. Platform.OS === 'android' && this.setState({
  110. addPadding: focused
  111. });
  112. };
  113. }
  114. _onSubmit: () => void;
  115. /**
  116. * Callback to handle the submit event of the text field.
  117. *
  118. * @returns {void}
  119. */
  120. _onSubmit() {
  121. const message = this.state.message.trim();
  122. message && this.props.onSend(message);
  123. this.setState({
  124. message: '',
  125. showSend: false
  126. });
  127. }
  128. }
  129. export default translate(ChatInputBar);