Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ChatInputBar.tsx 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import React, { Component } from 'react';
  2. import { WithTranslation } from 'react-i18next';
  3. import { Platform, ViewStyle } from 'react-native';
  4. import { SafeAreaView } from 'react-native-safe-area-context';
  5. import { translate } from '../../../base/i18n/functions';
  6. import { IconSend } from '../../../base/icons/svg';
  7. import IconButton from '../../../base/ui/components/native/IconButton';
  8. import Input from '../../../base/ui/components/native/Input';
  9. import { BUTTON_TYPES } from '../../../base/ui/constants.native';
  10. import styles from './styles';
  11. interface IProps extends WithTranslation {
  12. /**
  13. * Callback to invoke on message send.
  14. */
  15. onSend: Function;
  16. }
  17. interface IState {
  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<IProps, IState> {
  35. /**
  36. * Instantiates a new instance of the component.
  37. *
  38. * @inheritdoc
  39. */
  40. constructor(props: IProps) {
  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. <SafeAreaView
  59. edges = { [ 'bottom' ] }
  60. style = { [
  61. styles.inputBar,
  62. this.state.addPadding ? styles.extraBarPadding : null
  63. ] as ViewStyle[] }>
  64. <Input
  65. blurOnSubmit = { false }
  66. customStyles = {{ container: styles.customInputContainer }}
  67. multiline = { false }
  68. onBlur = { this._onFocused(false) }
  69. onChange = { this._onChangeText }
  70. onFocus = { this._onFocused(true) }
  71. onSubmitEditing = { this._onSubmit }
  72. placeholder = { this.props.t('chat.fieldPlaceHolder') }
  73. returnKeyType = 'send'
  74. value = { this.state.message } />
  75. <IconButton
  76. disabled = { !this.state.message }
  77. onPress = { this._onSubmit }
  78. src = { IconSend }
  79. style = { styles.sendButton }
  80. type = { BUTTON_TYPES.PRIMARY } />
  81. </SafeAreaView>
  82. );
  83. }
  84. /**
  85. * Callback to handle the change of the value of the text field.
  86. *
  87. * @param {string} text - The current value of the field.
  88. * @returns {void}
  89. */
  90. _onChangeText(text: string) {
  91. this.setState({
  92. message: text,
  93. showSend: Boolean(text)
  94. });
  95. }
  96. /**
  97. * Constructs a callback to be used to update the padding of the field if necessary.
  98. *
  99. * @param {boolean} focused - True of the field is focused.
  100. * @returns {Function}
  101. */
  102. _onFocused(focused: boolean) {
  103. return () => {
  104. Platform.OS === 'android' && this.setState({
  105. addPadding: focused
  106. });
  107. };
  108. }
  109. /**
  110. * Callback to handle the submit event of the text field.
  111. *
  112. * @returns {void}
  113. */
  114. _onSubmit() {
  115. const message = this.state.message.trim();
  116. message && this.props.onSend(message);
  117. this.setState({
  118. message: '',
  119. showSend: false
  120. });
  121. }
  122. }
  123. export default translate(ChatInputBar);