Skip to content

Commit

Permalink
Framework: ImagePreloader: Add onLoad prop for additional onload events
Browse files Browse the repository at this point in the history
  • Loading branch information
sirbrillig committed Dec 3, 2015
1 parent 7fb3afe commit 0acacbc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
14 changes: 14 additions & 0 deletions client/components/image-preloader/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,17 @@ A React element to render while the image `src` is being loaded.
- __Required:__ No

If a child is passed, it will be used as substitute content in the case that the image fails to load.

### `onLoad`

- __Type:__ function
- __Required:__ No

A custom function to call when the image loading is complete.

### `onError`

- __Type:__ function
- __Required:__ No

A custom function to call if the image loading fails.
18 changes: 15 additions & 3 deletions client/components/image-preloader/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ module.exports = React.createClass( {
propTypes: {
src: React.PropTypes.string.isRequired,
placeholder: React.PropTypes.element.isRequired,
children: React.PropTypes.node
children: React.PropTypes.node,
onLoad: React.PropTypes.func,
onError: React.PropTypes.func
},

getInitialState: function() {
Expand Down Expand Up @@ -72,8 +74,18 @@ module.exports = React.createClass( {
onLoadComplete: function( event ) {
this.destroyLoader();

this.setState( {
status: 'load' === event.type ? LoadStatus.LOADED : LoadStatus.FAILED
if ( event.type !== 'load' ) {
return this.setState( { status: LoadStatus.FAILED }, () => {
if ( this.props.onError ) {
this.props.onError( event );
}
} );
}

this.setState( { status: LoadStatus.LOADED }, () => {
if ( this.props.onLoad ) {
this.props.onLoad( event );
}
} );
},

Expand Down

0 comments on commit 0acacbc

Please sign in to comment.