|
@@ -27,6 +27,12 @@ class Chat extends AbstractChat<Props> {
|
27
|
27
|
*/
|
28
|
28
|
_isExited: boolean;
|
29
|
29
|
|
|
30
|
+ /**
|
|
31
|
+ * Reference to the React Component for displaying chat messages. Used for
|
|
32
|
+ * scrolling to the end of the chat messages.
|
|
33
|
+ */
|
|
34
|
+ _messageContainerRef: Object;
|
|
35
|
+
|
30
|
36
|
/**
|
31
|
37
|
* Initializes a new {@code Chat} instance.
|
32
|
38
|
*
|
|
@@ -37,11 +43,34 @@ class Chat extends AbstractChat<Props> {
|
37
|
43
|
super(props);
|
38
|
44
|
|
39
|
45
|
this._isExited = true;
|
|
46
|
+ this._messageContainerRef = React.createRef();
|
40
|
47
|
|
41
|
48
|
// Bind event handlers so they are only bound once for every instance.
|
42
|
49
|
this._renderPanelContent = this._renderPanelContent.bind(this);
|
43
|
50
|
}
|
44
|
51
|
|
|
52
|
+ /**
|
|
53
|
+ * Implements {@code Component#componentDidMount}.
|
|
54
|
+ *
|
|
55
|
+ * @inheritdoc
|
|
56
|
+ */
|
|
57
|
+ componentDidMount() {
|
|
58
|
+ this._scrollMessageContainerToBottom(true);
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+ /**
|
|
62
|
+ * Implements {@code Component#componentDidUpdate}.
|
|
63
|
+ *
|
|
64
|
+ * @inheritdoc
|
|
65
|
+ */
|
|
66
|
+ componentDidUpdate(prevProps) {
|
|
67
|
+ if (this.props._messages !== prevProps._messages) {
|
|
68
|
+ this._scrollMessageContainerToBottom(true);
|
|
69
|
+ } else if (this.props._isOpen && !prevProps._isOpen) {
|
|
70
|
+ this._scrollMessageContainerToBottom(false);
|
|
71
|
+ }
|
|
72
|
+ }
|
|
73
|
+
|
45
|
74
|
/**
|
46
|
75
|
* Implements React's {@link Component#render()}.
|
47
|
76
|
*
|
|
@@ -99,7 +128,9 @@ class Chat extends AbstractChat<Props> {
|
99
|
128
|
_renderChat() {
|
100
|
129
|
return (
|
101
|
130
|
<>
|
102
|
|
- <MessageContainer messages = { this.props._messages } />
|
|
131
|
+ <MessageContainer
|
|
132
|
+ messages = { this.props._messages }
|
|
133
|
+ ref = { this._messageContainerRef } />
|
103
|
134
|
<ChatInput />
|
104
|
135
|
</>
|
105
|
136
|
);
|
|
@@ -162,6 +193,20 @@ class Chat extends AbstractChat<Props> {
|
162
|
193
|
</div>
|
163
|
194
|
);
|
164
|
195
|
}
|
|
196
|
+
|
|
197
|
+ /**
|
|
198
|
+ * Scrolls the chat messages so the latest message is visible.
|
|
199
|
+ *
|
|
200
|
+ * @param {boolean} withAnimation - Whether or not to show a scrolling
|
|
201
|
+ * animation.
|
|
202
|
+ * @private
|
|
203
|
+ * @returns {void}
|
|
204
|
+ */
|
|
205
|
+ _scrollMessageContainerToBottom(withAnimation) {
|
|
206
|
+ if (this._messageContainerRef.current) {
|
|
207
|
+ this._messageContainerRef.current.scrollToBottom(withAnimation);
|
|
208
|
+ }
|
|
209
|
+ }
|
165
|
210
|
}
|
166
|
211
|
|
167
|
212
|
export default translate(connect(_mapStateToProps, _mapDispatchToProps)(Chat));
|