1 | import Knocking from './knocking'
|
2 | import React from 'react'
|
3 |
|
4 | class Image extends React.Component {
|
5 | constructor(props) {
|
6 | super(props)
|
7 | this.onLoad = this.onLoad.bind(this)
|
8 | this.state = {
|
9 | isLoading: true,
|
10 | }
|
11 | }
|
12 |
|
13 | componentWillReceiveProps(nextProps) {
|
14 | if (this.props.src !== nextProps.src) {
|
15 | this.setState({
|
16 | isLoading: true,
|
17 | })
|
18 | }
|
19 | }
|
20 |
|
21 | onLoad() {
|
22 | this.setState({
|
23 | isLoading: false,
|
24 | })
|
25 | }
|
26 |
|
27 | render() {
|
28 | const { isLoading } = this.state
|
29 | const { text, src, style, styleLoading, styleWrapper } = this.props
|
30 |
|
31 | return (
|
32 | <div style={styleWrapper}>
|
33 | {isLoading && <Knocking style={styleLoading} />}
|
34 | <img
|
35 | alt={text}
|
36 | onLoad={this.onLoad}
|
37 | src={src}
|
38 | style={style}
|
39 | title={text}
|
40 | />
|
41 | </div>
|
42 | )
|
43 | }
|
44 | }
|
45 |
|
46 | Image.defaultProps = {
|
47 | src: 'https://files.usepages.today/usepages.today/image-placeholder.svg',
|
48 | style: {},
|
49 | styleLoading: {
|
50 | position: 'absolute',
|
51 | },
|
52 | styleWrapper: {},
|
53 | text: 'Alternative text',
|
54 | }
|
55 |
|
56 | export default Image
|
57 |
|
\ | No newline at end of file |