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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { TextInput, View } from 'react-native';
  4. import { Platform } from '../../../base/react';
  5. import styles from './styles';
  6. type Props = {
  7. /**
  8. * Callback to invoke on message send.
  9. */
  10. onSend: Function
  11. };
  12. type State = {
  13. /**
  14. * Boolean to show if an extra padding needs to be added to the bar.
  15. */
  16. addPadding: boolean,
  17. /**
  18. * The value of the input field.
  19. */
  20. message: string
  21. };
  22. /**
  23. * Implements the chat input bar with text field and action(s).
  24. */
  25. export default class ChatInputBar extends Component<Props, State> {
  26. /**
  27. * Instantiates a new instance of the component.
  28. *
  29. * @inheritdoc
  30. */
  31. constructor(props: Props) {
  32. super(props);
  33. this.state = {
  34. addPadding: false,
  35. message: ''
  36. };
  37. this._onChangeText = this._onChangeText.bind(this);
  38. this._onFocused = this._onFocused.bind(this);
  39. this._onSubmit = this._onSubmit.bind(this);
  40. }
  41. /**
  42. * Implements {@code Component#render}.
  43. *
  44. * @inheritdoc
  45. */
  46. render() {
  47. return (
  48. <View
  49. style = { [
  50. styles.inputBar,
  51. this.state.addPadding ? styles.extraBarPadding : null
  52. ] }>
  53. <TextInput
  54. blurOnSubmit = { false }
  55. multiline = { false }
  56. onBlur = { this._onFocused(false) }
  57. onChangeText = { this._onChangeText }
  58. onFocus = { this._onFocused(true) }
  59. onSubmitEditing = { this._onSubmit }
  60. returnKeyType = 'send'
  61. style = { styles.inputField }
  62. value = { this.state.message } />
  63. </View>
  64. );
  65. }
  66. _onChangeText: string => void;
  67. /**
  68. * Callback to handle the change of the value of the text field.
  69. *
  70. * @param {string} text - The current value of the field.
  71. * @returns {void}
  72. */
  73. _onChangeText(text) {
  74. this.setState({
  75. message: text
  76. });
  77. }
  78. _onFocused: boolean => Function;
  79. /**
  80. * Constructs a callback to be used to update the padding of the field if necessary.
  81. *
  82. * @param {boolean} focused - True of the field is focused.
  83. * @returns {Function}
  84. */
  85. _onFocused(focused) {
  86. return () => {
  87. Platform.OS === 'android' && this.setState({
  88. addPadding: focused
  89. });
  90. };
  91. }
  92. _onSubmit: () => void;
  93. /**
  94. * Callback to handle the submit event of the text field.
  95. *
  96. * @returns {void}
  97. */
  98. _onSubmit() {
  99. const message = this.state.message.trim();
  100. message && this.props.onSend(message);
  101. this.setState({ message: '' });
  102. }
  103. }