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

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