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 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { TextInput, TouchableOpacity, View } from 'react-native';
  4. import { translate } from '../../../base/i18n';
  5. import { Icon, IconChatSend } from '../../../base/icons';
  6. import { Platform } from '../../../base/react';
  7. import styles from './styles';
  8. type Props = {
  9. /**
  10. * Callback to invoke on message send.
  11. */
  12. onSend: Function,
  13. /**
  14. * Function to be used to translate i18n labels.
  15. */
  16. t: Function
  17. };
  18. type State = {
  19. /**
  20. * Boolean to show if an extra padding needs to be added to the bar.
  21. */
  22. addPadding: boolean,
  23. /**
  24. * The value of the input field.
  25. */
  26. message: string,
  27. /**
  28. * Boolean to show or hide the send button.
  29. */
  30. showSend: boolean
  31. };
  32. /**
  33. * Implements the chat input bar with text field and action(s).
  34. */
  35. class ChatInputBar extends Component<Props, State> {
  36. /**
  37. * Instantiates a new instance of the component.
  38. *
  39. * @inheritdoc
  40. */
  41. constructor(props: Props) {
  42. super(props);
  43. this.state = {
  44. addPadding: false,
  45. message: '',
  46. showSend: false
  47. };
  48. this._onChangeText = this._onChangeText.bind(this);
  49. this._onFieldReferenceAvailable = this._onFieldReferenceAvailable.bind(this);
  50. this._onFocused = this._onFocused.bind(this);
  51. this._onSubmit = this._onSubmit.bind(this);
  52. }
  53. /**
  54. * Implements {@code Component#render}.
  55. *
  56. * @inheritdoc
  57. */
  58. render() {
  59. return (
  60. <View
  61. style = { [
  62. styles.inputBar,
  63. this.state.addPadding ? styles.extraBarPadding : null
  64. ] }>
  65. <TextInput
  66. blurOnSubmit = { false }
  67. multiline = { false }
  68. onBlur = { this._onFocused(false) }
  69. onChangeText = { this._onChangeText }
  70. onFocus = { this._onFocused(true) }
  71. onSubmitEditing = { this._onSubmit }
  72. placeholder = { this.props.t('chat.fieldPlaceHolder') }
  73. ref = { this._onFieldReferenceAvailable }
  74. returnKeyType = 'send'
  75. style = { styles.inputField }
  76. value = { this.state.message } />
  77. {
  78. this.state.showSend && <TouchableOpacity onPress = { this._onSubmit }>
  79. <Icon
  80. src = { IconChatSend }
  81. style = { styles.sendButtonIcon } />
  82. </TouchableOpacity>
  83. }
  84. </View>
  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. _onFieldReferenceAvailable: Object => void;
  101. /**
  102. * Callback to be invoked when the field reference is available.
  103. *
  104. * @param {Object} field - The reference to the field.
  105. * @returns {void}
  106. */
  107. _onFieldReferenceAvailable(field) {
  108. field && field.focus();
  109. }
  110. _onFocused: boolean => Function;
  111. /**
  112. * Constructs a callback to be used to update the padding of the field if necessary.
  113. *
  114. * @param {boolean} focused - True of the field is focused.
  115. * @returns {Function}
  116. */
  117. _onFocused(focused) {
  118. return () => {
  119. Platform.OS === 'android' && this.setState({
  120. addPadding: focused
  121. });
  122. };
  123. }
  124. _onSubmit: () => void;
  125. /**
  126. * Callback to handle the submit event of the text field.
  127. *
  128. * @returns {void}
  129. */
  130. _onSubmit() {
  131. const message = this.state.message.trim();
  132. message && this.props.onSend(message);
  133. this.setState({
  134. message: '',
  135. showSend: false
  136. });
  137. }
  138. }
  139. export default translate(ChatInputBar);