@@ -6,51 +6,102 @@ import LazyLoadComponent from './LazyLoadComponent.jsx';
66class LazyLoadImage extends React . Component {
77 constructor ( props ) {
88 super ( props ) ;
9+
10+ this . state = {
11+ loaded : false
12+ } ;
913 }
1014
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 ;
1529
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 (
1739 < LazyLoadComponent
1840 beforeLoad = { beforeLoad }
19- className = { this . props . className }
41+ className = { className }
2042 delayMethod = { delayMethod }
2143 delayTime = { delayTime }
22- height = { this . props . height }
44+ height = { height }
2345 placeholder = { placeholder }
2446 scrollPosition = { scrollPosition }
25- style = { this . props . style }
47+ style = { style }
2648 threshold = { threshold }
2749 visibleByDefault = { visibleByDefault }
28- width = { this . props . width } >
29- < img onLoad = { afterLoad } { ... imgProps } />
50+ width = { width } >
51+ { image }
3052 </ LazyLoadComponent >
3153 ) ;
54+ }
3255
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+ '' ;
3663
3764 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 }
4776 </ span >
4877 ) ;
4978 }
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+ }
5094}
5195
5296LazyLoadImage . propTypes = {
97+ afterLoad : PropTypes . func ,
98+ effect : PropTypes . string ,
5399 placeholderSrc : PropTypes . string
54100} ;
55101
102+ LazyLoadImage . defaultProps = {
103+ afterLoad : ( ) => ( { } ) ,
104+ effect : ''
105+ } ;
106+
56107export default LazyLoadImage ;
0 commit comments