1
- import React , { Children , Component } from 'react' ;
1
+ import React , { Component } from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
3
import { findDOMNode } from 'react-dom' ;
4
4
import { add , remove } from 'eventlistener' ;
5
- import debounce from 'lodash. debounce' ;
6
- import throttle from 'lodash. throttle' ;
5
+ import debounce from 'lodash/ debounce' ;
6
+ import throttle from 'lodash/ throttle' ;
7
7
import parentScroll from './utils/parentScroll' ;
8
8
import inViewport from './utils/inViewport' ;
9
9
@@ -107,20 +107,28 @@ export default class LazyLoad extends Component {
107
107
}
108
108
109
109
render ( ) {
110
- const { children, className, height, width } = this . props ;
111
- const { visible } = this . state ;
110
+ const { children, className, height, width, spinner , offline } = this . props ;
111
+ const { visible, loaded , errored } = this . state ;
112
112
113
113
const elStyles = { height, width } ;
114
- const elClasses = (
115
- 'LazyLoad' +
116
- ( visible ? ' is-visible' : '' ) +
117
- ( className ? ` ${ className } ` : '' )
118
- ) ;
114
+ const elClasses = [
115
+ 'LazyLoad' ,
116
+ ( visible ? ' is-visible' : '' ) ,
117
+ ( loaded ? ' is-loaded' : '' ) ,
118
+ ( errored ? ' is-errored' : '' ) ,
119
+ ( className ? ` ${ className } ` : '' ) ,
120
+ ] . join ( '' ) ;
121
+
122
+ const props = {
123
+ key : 'child' ,
124
+ onLoad : ( ) => { this . setState ( { loaded : true } ) ; } ,
125
+ onError : ( ) => { this . setState ( { errored : true } ) ; } ,
126
+ } ;
119
127
120
128
return React . createElement ( this . props . elementType , {
121
129
className : elClasses ,
122
130
style : elStyles ,
123
- } , visible && Children . only ( children ) ) ;
131
+ } , visible && [ spinner , offline , React . cloneElement ( children , props ) ] ) ;
124
132
}
125
133
}
126
134
@@ -133,13 +141,15 @@ LazyLoad.propTypes = {
133
141
PropTypes . string ,
134
142
PropTypes . number ,
135
143
] ) ,
144
+ offline : PropTypes . node ,
136
145
offset : PropTypes . number ,
137
146
offsetBottom : PropTypes . number ,
138
147
offsetHorizontal : PropTypes . number ,
139
148
offsetLeft : PropTypes . number ,
140
149
offsetRight : PropTypes . number ,
141
150
offsetTop : PropTypes . number ,
142
151
offsetVertical : PropTypes . number ,
152
+ spinner : PropTypes . node ,
143
153
threshold : PropTypes . number ,
144
154
throttle : PropTypes . number ,
145
155
width : PropTypes . oneOfType ( [
@@ -160,4 +170,6 @@ LazyLoad.defaultProps = {
160
170
offsetTop : 0 ,
161
171
offsetVertical : 0 ,
162
172
throttle : 250 ,
173
+ spinner : < div className = "spinner" key = "spinner" > </ div > ,
174
+ offline : < div className = "offline" key = "offline" > </ div > ,
163
175
} ;
0 commit comments