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

MessageContainer.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // @flow
  2. import React from 'react';
  3. import AbstractMessageContainer, { type Props }
  4. from '../AbstractMessageContainer';
  5. import ChatMessageGroup from './ChatMessageGroup';
  6. /**
  7. * Displays all received chat messages, grouped by sender.
  8. *
  9. * @extends AbstractMessageContainer
  10. */
  11. export default class MessageContainer extends AbstractMessageContainer {
  12. /**
  13. * Whether or not chat has been scrolled to the bottom of the screen. Used
  14. * to determine if chat should be scrolled automatically to the bottom when
  15. * the {@code ChatInput} resizes.
  16. */
  17. _isScrolledToBottom: boolean;
  18. /**
  19. * Reference to the HTML element at the end of the list of displayed chat
  20. * messages. Used for scrolling to the end of the chat messages.
  21. */
  22. _messagesListEndRef: Object;
  23. /**
  24. * A React ref to the HTML element containing all {@code ChatMessageGroup}
  25. * instances.
  26. */
  27. _messageListRef: Object;
  28. /**
  29. * Initializes a new {@code MessageContainer} instance.
  30. *
  31. * @param {Props} props - The React {@code Component} props to initialize
  32. * the new {@code MessageContainer} instance with.
  33. */
  34. constructor(props: Props) {
  35. super(props);
  36. this._isScrolledToBottom = true;
  37. this._messageListRef = React.createRef();
  38. this._messagesListEndRef = React.createRef();
  39. this._onChatScroll = this._onChatScroll.bind(this);
  40. }
  41. /**
  42. * Implements {@code Component#render}.
  43. *
  44. * @inheritdoc
  45. */
  46. render() {
  47. const groupedMessages = this._getMessagesGroupedBySender();
  48. const messages = groupedMessages.map((group, index) => {
  49. const messageType = group[0] && group[0].messageType;
  50. return (
  51. <ChatMessageGroup
  52. className = { messageType || 'remote' }
  53. key = { index }
  54. messages = { group } />
  55. );
  56. });
  57. return (
  58. <div
  59. id = 'chatconversation'
  60. onScroll = { this._onChatScroll }
  61. ref = { this._messageListRef }>
  62. { messages }
  63. <div ref = { this._messagesListEndRef } />
  64. </div>
  65. );
  66. }
  67. /**
  68. * Scrolls to the bottom again if the instance had previously been scrolled
  69. * to the bottom. This method is used when a resize has occurred below the
  70. * instance and bottom scroll needs to be maintained.
  71. *
  72. * @returns {void}
  73. */
  74. maybeUpdateBottomScroll() {
  75. if (this._isScrolledToBottom) {
  76. this.scrollToBottom(false);
  77. }
  78. }
  79. /**
  80. * Automatically scrolls the displayed chat messages down to the latest.
  81. *
  82. * @param {boolean} withAnimation - Whether or not to show a scrolling
  83. * animation.
  84. * @returns {void}
  85. */
  86. scrollToBottom(withAnimation: boolean) {
  87. this._messagesListEndRef.current.scrollIntoView({
  88. behavior: withAnimation ? 'smooth' : 'auto',
  89. block: 'nearest'
  90. });
  91. }
  92. _getMessagesGroupedBySender: () => Array<Array<Object>>;
  93. _onChatScroll: () => void;
  94. /**
  95. * Callback invoked to listen to the current scroll location.
  96. *
  97. * @private
  98. * @returns {void}
  99. */
  100. _onChatScroll() {
  101. const element = this._messageListRef.current;
  102. this._isScrolledToBottom
  103. = element.scrollHeight - element.scrollTop === element.clientHeight;
  104. }
  105. }