您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ChatMessageGroup.js 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { FlatList } from 'react-native';
  4. import { MESSAGE_TYPE_LOCAL, MESSAGE_TYPE_REMOTE } from '../../constants';
  5. import ChatMessage from './ChatMessage';
  6. import styles from './styles';
  7. type Props = {
  8. /**
  9. * The messages array to render.
  10. */
  11. messages: Array<Object>
  12. }
  13. /**
  14. * Implements a container to render all the chat messages in a conference.
  15. */
  16. export default class ChatMessageGroup extends Component<Props> {
  17. /**
  18. * Instantiates a new instance of the component.
  19. *
  20. * @inheritdoc
  21. */
  22. constructor(props: Props) {
  23. super(props);
  24. this._keyExtractor = this._keyExtractor.bind(this);
  25. this._renderMessage = this._renderMessage.bind(this);
  26. }
  27. /**
  28. * Implements {@code Component#render}.
  29. *
  30. * @inheritdoc
  31. */
  32. render() {
  33. return (
  34. <FlatList
  35. data = { this.props.messages }
  36. inverted = { true }
  37. keyExtractor = { this._keyExtractor }
  38. renderItem = { this._renderMessage }
  39. style = { styles.messageContainer } />
  40. );
  41. }
  42. _keyExtractor: Object => string
  43. /**
  44. * Key extractor for the flatlist.
  45. *
  46. * @param {Object} item - The flatlist item that we need the key to be
  47. * generated for.
  48. * @param {number} index - The index of the element.
  49. * @returns {string}
  50. */
  51. _keyExtractor(item, index) {
  52. return `key_${index}`;
  53. }
  54. _renderMessage: Object => React$Element<*>;
  55. /**
  56. * Renders a single chat message.
  57. *
  58. * @param {Object} message - The chat message to render.
  59. * @returns {React$Element<*>}
  60. */
  61. _renderMessage({ index, item: message }) {
  62. return (
  63. <ChatMessage
  64. message = { message }
  65. showAvatar = {
  66. this.props.messages[0].messageType !== MESSAGE_TYPE_LOCAL
  67. && index === this.props.messages.length - 1
  68. }
  69. showDisplayName = {
  70. this.props.messages[0].messageType === MESSAGE_TYPE_REMOTE
  71. && index === this.props.messages.length - 1
  72. }
  73. showTimestamp = { index === 0 } />
  74. );
  75. }
  76. }