Skip to content

Commit 8a0af78

Browse files
Merge pull request #88 from Detaysoft/lazy-feature
LazyLoadingImage feature added.
2 parents 66cee7c + 05a9f33 commit 8a0af78

6 files changed

Lines changed: 76 additions & 3 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ import { ChatItem } from 'react-chat-elements'
9292
| onContextMenu | none | function | ChatItem on context menu |
9393
| statusColor | none | color | ChatItem status color |
9494
| statusText | none | color | ChatItem status text |
95+
| lazyLoadingImage | none | image path | lazy loading image |
9596

9697

9798

@@ -237,6 +238,7 @@ import { ChatList } from 'react-chat-elements'
237238
| onClick | none | function | chat list item on click (chat(object) is returned) |
238239
| onContextMenu | none | function | chat list item on context menu (chat(object) is returned) |
239240
| onAvatarError | none | function | chat list item on error avatar img |
241+
| lazyLoadingImage | none | image path | lazy loading image |
240242

241243
## Input Component
242244

@@ -448,6 +450,7 @@ import { Avatar } from 'react-chat-elements'
448450
| type | default | string | types: default, circle, rounded(border radius 5px), flexible |
449451
| sideElement | none | component | avatar side element |
450452
| onError | none | component | avatar img onerror |
453+
| lazyLoadingImage | none | image path | lazy loading image |
451454

452455

453456
## LocationMessage Component

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-chat-elements",
3-
"version": "10.2.0",
3+
"version": "10.3.0",
44
"description": "Reactjs chat components",
55
"author": "Avare Kodcu <abdurrahmaneker58@gmail.com>",
66
"main": "dist/main.js",

src/Avatar/Avatar.css

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,19 @@
5555
width: 55px;
5656
height: 55px;
5757
}
58+
59+
@keyframes avatarLazy {
60+
0% {
61+
opacity:1;
62+
}
63+
50% {
64+
opacity:.5;
65+
}
66+
100% {
67+
opacity:1;
68+
}
69+
}
70+
71+
.rce-avatar-lazy {
72+
animation: avatarLazy normal 2s infinite ease-in-out;
73+
}

src/Avatar/Avatar.js

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,64 @@ import './Avatar.css';
33

44
const classNames = require('classnames');
55

6+
const loadedAvatars = [];
7+
68
export class Avatar extends Component {
9+
constructor(props) {
10+
super(props);
11+
12+
this.requestImage = this.requestImage.bind(this);
13+
}
14+
15+
isLoaded(src) {
16+
return loadedAvatars.indexOf(src) !== -1;
17+
}
18+
19+
requestImage(src) {
20+
var self = this;
21+
this.loading = true;
22+
23+
var loaded = () => {
24+
loadedAvatars.push(src);
25+
delete self.loading;
26+
self.setState({});
27+
};
28+
29+
var img = document.createElement('img');
30+
img.src = src;
31+
img.onload = loaded;
32+
img.onerror = loaded;
33+
}
34+
735
render() {
36+
var src = this.props.src;
37+
var isLazyImage = false;
38+
39+
if (this.props.lazyLoadingImage) {
40+
isLazyImage = true;
41+
42+
if (!this.isLoaded(src)) {
43+
src = this.props.lazyLoadingImage; // lazy image
44+
45+
if (!this.loading) {
46+
this.requestImage(this.props.src);
47+
}
48+
}
49+
else {
50+
isLazyImage = false;
51+
}
52+
}
53+
854
return (
955
<div className={classNames('rce-avatar-container', this.props.type, this.props.size, this.props.className)}>
1056
<img
1157
alt={this.props.alt}
12-
src={this.props.src}
58+
src={src}
1359
onError={this.props.onError}
14-
className={'rce-avatar'} />
60+
className={classNames(
61+
'rce-avatar',
62+
{'rce-avatar-lazy': isLazyImage},
63+
)} />
1564
{this.props.sideElement}
1665
</div>
1766
);
@@ -24,6 +73,7 @@ Avatar.defaultProps = {
2473
src: '',
2574
alt: '',
2675
sideElement: null,
76+
lazyLoadingImage: undefined,
2777
onError: () => void(0),
2878
};
2979

src/ChatItem/ChatItem.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export class ChatItem extends Component {
2727
</span>
2828
}
2929
onError={this.props.onAvatarError}
30+
lazyLoadingImage={this.props.lazyLoadingImage}
3031
type={classNames('circle', {'flexible': this.props.avatarFlexible})}/>
3132
</div>
3233

@@ -78,6 +79,7 @@ ChatItem.defaultProps = {
7879
statusColor: null,
7980
statusText: null,
8081
dateString: null,
82+
lazyLoadingImage: undefined,
8183
onAvatarError: () => void(0),
8284
}
8385

src/ChatList/ChatList.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export class ChatList extends Component {
3333
<ChatItem
3434
id={x.id || i}
3535
key={i}
36+
lazyLoadingImage={this.props.lazyLoadingImage}
3637
{...x}
3738
onAvatarError={(e) => this.onAvatarError(x,i,e)}
3839
onContextMenu={(e) => this.onContextMenu(x,i,e)}
@@ -47,6 +48,7 @@ export class ChatList extends Component {
4748
ChatList.defaultProps = {
4849
dataSource: [],
4950
onClick: null,
51+
lazyLoadingImage: undefined,
5052
};
5153

5254
export default ChatList;

0 commit comments

Comments
 (0)