1- import React , { Component } from 'react' ;
1+ import React , { Component , } from 'react' ;
22import './MessageList.css' ;
33
4- import MessageBox from '../MessageBox/MessageBox' ;
4+ import {
5+ MessageBox ,
6+ } from '../MessageBox/MessageBox' ;
57
68import FaChevronDown from 'react-icons/lib/fa/chevron-down' ;
79
@@ -14,10 +16,24 @@ export class MessageList extends Component {
1416 this . state = {
1517 scrollBottom : 0 ,
1618 downButton : false ,
19+ messageFocus : false ,
1720 } ;
1821
1922 this . loadRef = this . loadRef . bind ( this ) ;
23+ this . createReferenceOfMessage = this . createReferenceOfMessage . bind ( this ) ;
2024 this . onScroll = this . onScroll . bind ( this ) ;
25+ this . messageRefs = [ ] ;
26+ }
27+
28+ scrollIntoMessage ( focusedMessage ) {
29+ var message = this . messageRefs . find ( x => x . messageId === focusedMessage ) ;
30+ if ( message !== undefined ) {
31+ this . setState ( {
32+ messageFocus : true ,
33+ } , ( ) => {
34+ message . ref . scrollIntoView ( { block : this . props . scrollBlock , behavior : 'smooth' , } ) ;
35+ } ) ;
36+ }
2137 }
2238
2339 checkScroll ( ) {
@@ -35,12 +51,15 @@ export class MessageList extends Component {
3551 }
3652 }
3753
38- componentWillReceiveProps ( ) {
54+ componentWillReceiveProps ( nextProps ) {
3955 if ( ! this . mlistRef )
4056 return ;
4157 this . setState ( {
4258 scrollBottom : this . getBottom ( this . mlistRef ) ,
4359 } , this . checkScroll . bind ( this ) ) ;
60+
61+ if ( nextProps . focusedMessage )
62+ this . scrollIntoMessage ( nextProps . focusedMessage ) ;
4463 }
4564
4665 getBottom ( e ) {
@@ -83,6 +102,22 @@ export class MessageList extends Component {
83102 this . props . cmpRef ( ref ) ;
84103 }
85104
105+ createReferenceOfMessage ( ref , messageId ) {
106+ var check = this . messageRefs . find ( res => res . messageId === messageId ) ;
107+
108+ if ( check !== undefined ) {
109+ var index = this . messageRefs . indexOf ( check ) ;
110+ if ( index !== - 1 ) {
111+ this . messageRefs . splice ( index , 1 ) ;
112+ }
113+ }
114+
115+ this . messageRefs . push ( {
116+ ref : ref ,
117+ messageId : messageId ,
118+ } ) ;
119+ }
120+
86121 onScroll ( e ) {
87122 var bottom = this . getBottom ( e . target ) ;
88123 this . state . scrollBottom = bottom ;
@@ -92,15 +127,15 @@ export class MessageList extends Component {
92127 this . setState ( {
93128 downButton : true ,
94129 scrollBottom : bottom ,
95- } )
130+ } ) ;
96131 }
97132 } else {
98133 if ( this . state . downButton !== false ) {
99134 this . state . downButton = false ;
100135 this . setState ( {
101136 downButton : false ,
102137 scrollBottom : bottom ,
103- } )
138+ } ) ;
104139 }
105140 }
106141
@@ -128,16 +163,22 @@ export class MessageList extends Component {
128163 className = 'rce-mlist' >
129164 {
130165 this . props . dataSource . map ( ( x , i ) => (
131- < MessageBox
132- key = { i }
133- { ...x }
134- onOpen = { this . props . onOpen && ( ( e ) => this . onOpen ( x , i , e ) ) }
135- onDownload = { this . props . onDownload && ( ( e ) => this . onDownload ( x , i , e ) ) }
136- onTitleClick = { this . props . onTitleClick && ( ( e ) => this . onTitleClick ( x , i , e ) ) }
137- onForwardClick = { this . props . onForwardClick && ( ( e ) => this . onForwardClick ( x , i , e ) ) }
138- onClick = { this . props . onClick && ( ( e ) => this . onClick ( x , i , e ) ) }
139- onContextMenu = { this . props . onContextMenu && ( ( e ) => this . onContextMenu ( x , i , e ) ) }
166+ < div
167+ ref = { ref => this . createReferenceOfMessage ( ref , x . id ) } >
168+ < MessageBox
169+ key = { i }
170+ { ...x }
171+ focusedMessage = { this . props . focusedMessage }
172+ messageFocus = { x . id === this . props . focusedMessage && this . state . messageFocus }
173+ onOpen = { this . props . onOpen && ( ( e ) => this . onOpen ( x , i , e ) ) }
174+ onFocus = { this . props . onFocus && ( ( e ) => this . onFocus ( x , i , e ) ) }
175+ onDownload = { this . props . onDownload && ( ( e ) => this . onDownload ( x , i , e ) ) }
176+ onTitleClick = { this . props . onTitleClick && ( ( e ) => this . onTitleClick ( x , i , e ) ) }
177+ onForwardClick = { this . props . onForwardClick && ( ( e ) => this . onForwardClick ( x , i , e ) ) }
178+ onClick = { this . props . onClick && ( ( e ) => this . onClick ( x , i , e ) ) }
179+ onContextMenu = { this . props . onContextMenu && ( ( e ) => this . onContextMenu ( x , i , e ) ) }
140180 />
181+ </ div >
141182 ) )
142183 }
143184 </ div >
@@ -175,6 +216,8 @@ MessageList.defaultProps = {
175216 toBottomHeight : 300 ,
176217 downButton : true ,
177218 downButtonBadge : null ,
219+ focusedMessage : null ,
220+ scrollBlock : 'center' ,
178221} ;
179222
180223export default MessageList ;
0 commit comments