1- import React , { Children , Component } from 'react' ;
1+ import React , { Component } from 'react' ;
22import PropTypes from 'prop-types' ;
33import { findDOMNode } from 'react-dom' ;
44import { 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' ;
77import parentScroll from './utils/parentScroll' ;
88import inViewport from './utils/inViewport' ;
99
@@ -107,20 +107,28 @@ export default class LazyLoad extends Component {
107107 }
108108
109109 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 ;
112112
113113 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+ } ;
119127
120128 return React . createElement ( this . props . elementType , {
121129 className : elClasses ,
122130 style : elStyles ,
123- } , visible && Children . only ( children ) ) ;
131+ } , visible && [ spinner , offline , React . cloneElement ( children , props ) ] ) ;
124132 }
125133}
126134
@@ -133,13 +141,15 @@ LazyLoad.propTypes = {
133141 PropTypes . string ,
134142 PropTypes . number ,
135143 ] ) ,
144+ offline : PropTypes . node ,
136145 offset : PropTypes . number ,
137146 offsetBottom : PropTypes . number ,
138147 offsetHorizontal : PropTypes . number ,
139148 offsetLeft : PropTypes . number ,
140149 offsetRight : PropTypes . number ,
141150 offsetTop : PropTypes . number ,
142151 offsetVertical : PropTypes . number ,
152+ spinner : PropTypes . node ,
143153 threshold : PropTypes . number ,
144154 throttle : PropTypes . number ,
145155 width : PropTypes . oneOfType ( [
@@ -160,4 +170,6 @@ LazyLoad.defaultProps = {
160170 offsetTop : 0 ,
161171 offsetVertical : 0 ,
162172 throttle : 250 ,
173+ spinner : < div className = "spinner" key = "spinner" > </ div > ,
174+ offline : < div className = "offline" key = "offline" > </ div > ,
163175} ;
0 commit comments