@@ -6,51 +6,102 @@ import LazyLoadComponent from './LazyLoadComponent.jsx';
6
6
class LazyLoadImage extends React . Component {
7
7
constructor ( props ) {
8
8
super ( props ) ;
9
+
10
+ this . state = {
11
+ loaded : false
12
+ } ;
9
13
}
10
14
11
- render ( ) {
12
- const { afterLoad, beforeLoad, delayMethod, delayTime, placeholder,
13
- placeholderSrc, scrollPosition, threshold, visibleByDefault,
14
- ...imgProps } = this . props ;
15
+ onImageLoad ( ) {
16
+ return ( ) => {
17
+ this . props . afterLoad ( ) ;
18
+
19
+ this . setState ( {
20
+ loaded : true
21
+ } ) ;
22
+ } ;
23
+ }
24
+
25
+ getImg ( ) {
26
+ const { afterLoad, beforeLoad, delayMethod, delayTime, effect,
27
+ placeholder, placeholderSrc, scrollPosition, threshold,
28
+ visibleByDefault, ...imgProps } = this . props ;
15
29
16
- const lazyLoadComponent = (
30
+ return < img onLoad = { this . onImageLoad ( ) } { ...imgProps } /> ;
31
+ }
32
+
33
+ getLazyLoadImage ( image ) {
34
+ const { beforeLoad, className, delayMethod, delayTime,
35
+ height, placeholder, scrollPosition, style, threshold,
36
+ visibleByDefault, width } = this . props ;
37
+
38
+ return (
17
39
< LazyLoadComponent
18
40
beforeLoad = { beforeLoad }
19
- className = { this . props . className }
41
+ className = { className }
20
42
delayMethod = { delayMethod }
21
43
delayTime = { delayTime }
22
- height = { this . props . height }
44
+ height = { height }
23
45
placeholder = { placeholder }
24
46
scrollPosition = { scrollPosition }
25
- style = { this . props . style }
47
+ style = { style }
26
48
threshold = { threshold }
27
49
visibleByDefault = { visibleByDefault }
28
- width = { this . props . width } >
29
- < img onLoad = { afterLoad } { ... imgProps } />
50
+ width = { width } >
51
+ { image }
30
52
</ LazyLoadComponent >
31
53
) ;
54
+ }
32
55
33
- if ( ! placeholderSrc || visibleByDefault ) {
34
- return lazyLoadComponent ;
35
- }
56
+ getWrappedLazyLoadImage ( lazyLoadImage ) {
57
+ const { effect, height, placeholderSrc, width } = this . props ;
58
+ const { loaded } = this . state ;
59
+
60
+ const loadedClassName = loaded ?
61
+ ' lazy-load-image-loaded' :
62
+ '' ;
36
63
37
64
return (
38
- < span style = { {
39
- backgroundImage : 'url( ' + placeholderSrc + ')' ,
40
- backgroundSize : '100% 100%' ,
41
- color : 'transparent' ,
42
- display : 'inline-block' ,
43
- height : this . props . height ,
44
- width : this . props . width
45
- } } >
46
- { lazyLoadComponent }
65
+ < span
66
+ className = { 'lazy-load-image-background ' + effect + loadedClassName }
67
+ style = { {
68
+ backgroundImage : 'url( ' + placeholderSrc + ')' ,
69
+ backgroundSize : '100% 100%' ,
70
+ color : 'transparent' ,
71
+ display : 'inline-block' ,
72
+ height : height ,
73
+ width : width
74
+ } } >
75
+ { lazyLoadImage }
47
76
</ span >
48
77
) ;
49
78
}
79
+
80
+ render ( ) {
81
+ const { effect, placeholderSrc, visibleByDefault } = this . props ;
82
+ const { loaded } = this . state ;
83
+
84
+ const image = this . getImg ( ) ;
85
+ const lazyLoadImage = loaded ?
86
+ image : this . getLazyLoadImage ( image ) ;
87
+
88
+ if ( ( ! effect && ! placeholderSrc ) || visibleByDefault ) {
89
+ return lazyLoadImage ;
90
+ }
91
+
92
+ return this . getWrappedLazyLoadImage ( lazyLoadImage ) ;
93
+ }
50
94
}
51
95
52
96
LazyLoadImage . propTypes = {
97
+ afterLoad : PropTypes . func ,
98
+ effect : PropTypes . string ,
53
99
placeholderSrc : PropTypes . string
54
100
} ;
55
101
102
+ LazyLoadImage . defaultProps = {
103
+ afterLoad : ( ) => ( { } ) ,
104
+ effect : ''
105
+ } ;
106
+
56
107
export default LazyLoadImage ;
0 commit comments