11// @flow
22/**
33 * @class ShimmerImage
4- * @version 1.2 .0
4+ * @version 2.0 .0
55 * @author github.com/gokcan
66 */
77
8- import React , { Component } from 'react'
8+ import * as React from 'react'
99import * as PropTypes from 'prop-types'
1010import cl from './styles.css'
1111import 'regenerator-runtime/runtime'
@@ -21,7 +21,7 @@ type Props = {
2121 style ?: Object ,
2222 onError ?: ( err : string ) => void ,
2323 onLoad ?: ( image : Image ) => void ,
24- loadingIndicatorSource ?: string ,
24+ fallback ?: React . Element < * > ,
2525 delay ? : number ,
2626}
2727
@@ -31,17 +31,17 @@ type State = {
3131 error ?: string
3232}
3333
34- export default class ShimmerImage extends Component < Props , State > {
34+ export default class ShimmerImage extends React . Component < Props , State > {
3535 static propTypes = {
3636 src : PropTypes . string . isRequired ,
3737 color : PropTypes . string ,
3838 duration : PropTypes . number ,
39- width : PropTypes . number . isRequired ,
40- height : PropTypes . number . isRequired ,
39+ width : PropTypes . number ,
40+ height : PropTypes . number ,
4141 style : PropTypes . object ,
4242 onError : PropTypes . func ,
4343 onLoad : PropTypes . func ,
44- loadingIndicatorSource : PropTypes . string ,
44+ fallback : PropTypes . element ,
4545 delay : PropTypes . number
4646 }
4747
@@ -75,8 +75,8 @@ export default class ShimmerImage extends Component<Props, State> {
7575 }
7676
7777 startImageLoadingProcess = async ( ) => {
78- const { src, delay, width, height } = this . props
79- if ( ! ( width & & height ) ) {
78+ const { src, delay, width, height, fallback } = this . props
79+ if ( ! fallback && ! ( width & height ) ) {
8080 this . setState ( { error : 'Height and Width props must be provided!' } )
8181 }
8282 /*
@@ -147,7 +147,7 @@ export default class ShimmerImage extends Component<Props, State> {
147147
148148 render ( ) {
149149 const { src, error, isLoading } = this . state
150- const { width, height, color, duration, style, onError, loadingIndicatorSource , ...passed } = this . props
150+ const { width, height, color, duration, style, onError, fallback , ...passed } = this . props
151151 const { ...passedStyles } = style
152152 const passedProps = { ...passed , ...{ src, width, height } }
153153 const backgroundSize = `${ width * 10 } px ${ height } px`
@@ -156,15 +156,15 @@ export default class ShimmerImage extends Component<Props, State> {
156156 backgroundColor : color ,
157157 backgroundSize,
158158 // $FlowFixMe
159- animationDuration : `${ duration } s`
159+ animationDuration : `${ ( duration / 1000 ) . toFixed ( 1 ) } s`
160160 }
161161
162162 if ( error ) {
163163 // $FlowFixMe
164164 return onError ? onError ( error ) : null
165165 } else if ( isLoading ) {
166- if ( loadingIndicatorSource ) {
167- return < img src = { loadingIndicatorSource } />
166+ if ( fallback ) {
167+ return fallback
168168 } else {
169169 return (
170170 < div className = { cl . shimmerdiv } style = { { ...passedStyles } } >
0 commit comments