-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
169 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,45 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import ImageLoader from '../src'; | ||
|
||
class Demo extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
value: null | ||
}; | ||
} | ||
|
||
onChange = (e) => { | ||
this.setState({ | ||
value: e.target.value | ||
}); | ||
} | ||
|
||
render() { | ||
const {value} = this.state; | ||
return ( | ||
<div> | ||
<h1>Copy a image URL here</h1> | ||
<input onChange={this.onChange}/> | ||
<p> | ||
value: {value} | ||
</p> | ||
<div> | ||
{value && ( | ||
<ImageLoader | ||
src={value} | ||
loading={() => <div>Loading...</div>} | ||
error={() => <div>Error</div>} | ||
/> | ||
)} | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
ReactDOM.render( | ||
<div/> | ||
<Demo/> | ||
, document.getElementById('root')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,89 @@ | ||
// @flow | ||
import React from 'react'; | ||
import Spinner from './Spinner'; | ||
import * as React from 'react'; | ||
|
||
export default class ImageLoader extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
type Props = { | ||
src: string, | ||
onLoad?: (img: Image) => void, | ||
onError?: (err: Event) => void, | ||
loading?: () => React.Element<*>, | ||
error?: (err: Event) => React.Element<*> | ||
} | ||
|
||
type State = { | ||
isLoading: boolean, | ||
isError: boolean, | ||
src: ?string, | ||
errMsg: ?any, | ||
} | ||
|
||
this.state = { | ||
isLoading: true, | ||
src: '' | ||
} | ||
export default class ImageLoader extends React.Component<Props, State> { | ||
constructor(props: Props) { | ||
super(props); | ||
(this: any).reload = this.reload.bind(this); | ||
|
||
this.state = { | ||
isLoading: true, | ||
isError: false, | ||
src: null, | ||
errMsg: null | ||
} | ||
} | ||
|
||
componentWillReceiveProps(nextProps: Props) { | ||
this.reload(nextProps); | ||
} | ||
|
||
componentDidMount() { | ||
const image = new Image(); | ||
image.src = this.props.src; | ||
image.onload = () => { | ||
this.setState({ | ||
src: image.src, | ||
isLoading: false | ||
}); | ||
if (this.props.onLoad) { | ||
this.props.onLoad(image); | ||
} | ||
}; | ||
image.onerror = (err) => { | ||
this.setState({ | ||
src: '', | ||
isLoading: false | ||
}); | ||
if (this.props.onError) { | ||
this.props.onError(err); | ||
} | ||
} | ||
componentDidMount() { | ||
this.reload(this.props); | ||
} | ||
|
||
reload(props: Props) { | ||
// initialize | ||
this.setState({ | ||
isLoading: true, | ||
isError: false, | ||
src: null, | ||
errMsg: null | ||
}); | ||
|
||
const image = new Image(); | ||
image.src = props.src; | ||
image.onload = () => { | ||
this.setState({ | ||
src: image.src, | ||
isLoading: false, | ||
isError: false, | ||
errMsg: null | ||
}); | ||
if (props.onLoad) { | ||
props.onLoad(image); | ||
} | ||
}; | ||
image.onerror = (err) => { | ||
this.setState({ | ||
src: null, | ||
isLoading: false, | ||
isError: true, | ||
errMsg: err | ||
}); | ||
if (props.onError) { | ||
props.onError(err); | ||
} | ||
} | ||
} | ||
|
||
render() { | ||
if (this.state.isLoading) { | ||
return ( | ||
<Spinner src={this.props.spinnerSrc}/> | ||
); | ||
} else { | ||
return ( | ||
<img className='ril--image' src={this.state.src} alt={this.props.alt} {...this.props}/> | ||
); | ||
} | ||
render() { | ||
const {loading, error} = this.props; | ||
const {src, isLoading, isError, errMsg} = this.state; | ||
|
||
if (loading && isLoading) { | ||
return loading(); | ||
} else if (error && isError && errMsg) { | ||
return error(errMsg); | ||
} else if (src) { | ||
return <img {...this.props} src={src}/> | ||
} | ||
|
||
return null; | ||
} | ||
} |