Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ChatInputBar.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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._onFocused = this._onFocused.bind(this);
  49. this._onSubmit = this._onSubmit.bind(this);
  50. }
  51. /**
  52. * Implements {@code Component#render}.
  53. *
  54. * @inheritdoc
  55. */
  56. render() {
  57. return (
  58. <View
  59. style = { [
  60. styles.inputBar,
  61. this.state.addPadding ? styles.extraBarPadding : null
  62. ] }>
  63. <TextInput
  64. blurOnSubmit = { false }
  65. multiline = { false }
  66. onBlur = { this._onFocused(false) }
  67. onChangeText = { this._onChangeText }
  68. onFocus = { this._onFocused(true) }
  69. onSubmitEditing = { this._onSubmit }
  70. placeholder = { this.props.t('chat.fieldPlaceHolder') }
  71. returnKeyType = 'send'
  72. style = { styles.inputField }
  73. value = { this.state.message } />
  74. {
  75. this.state.showSend && <TouchableOpacity onPress = { this._onSubmit }>
  76. <Icon
  77. src = { IconChatSend }
  78. style = { styles.sendButtonIcon } />
  79. </TouchableOpacity>
  80. }
  81. </View>
  82. );
  83. }
  84. _onChangeText: string => void;
  85. /**
  86. * Callback to handle the change of the value of the text field.
  87. *
  88. * @param {string} text - The current value of the field.
  89. * @returns {void}
  90. */
  91. _onChangeText(text) {
  92. this.setState({
  93. message: text,
  94. showSend: Boolean(text)
  95. });
  96. }
  97. _onFocused: boolean => Function;
  98. /**
  99. * Constructs a callback to be used to update the padding of the field if necessary.
  100. *
  101. * @param {boolean} focused - True of the field is focused.
  102. * @returns {Function}
  103. */
  104. _onFocused(focused) {
  105. return () => {
  106. Platform.OS === 'android' && this.setState({
  107. addPadding: focused
  108. });
  109. };
  110. }
  111. _onSubmit: () => void;
  112. /**
  113. * Callback to handle the submit event of the text field.
  114. *
  115. * @returns {void}
  116. */
  117. _onSubmit() {
  118. const message = this.state.message.trim();
  119. message && this.props.onSend(message);
  120. this.setState({
  121. message: '',
  122. showSend: false
  123. });
  124. }
  125. }
  126. export default translate(ChatInputBar);