chore(client): refactor ImageLoader (#39691)

This commit is contained in:
max-voronov
2020-09-30 12:22:20 +03:00
committed by GitHub
parent d23feadc1f
commit 55f3df293a

View File

@ -1,4 +1,4 @@
import React from 'react'; import React, { useState } from 'react';
import './image-loader.css'; import './image-loader.css';
import LazyLoad from 'react-lazy-load'; import LazyLoad from 'react-lazy-load';
@ -14,39 +14,19 @@ const propTypes = {
width: PropTypes.number width: PropTypes.number
}; };
class ImageLoader extends React.Component { const ImageLoader = ({
// initial state: image loaded stage className = '',
state = { loadedClassName = 'img-loaded',
loaded: false loadingClassName = 'img-loading',
};
// define our loading and loaded image classes
static defaultProps = {
className: '',
loadingClassName: 'img-loading',
loadedClassName: 'img-loaded'
};
// image onLoad handler to update state to loaded
onLoad = () => {
this.setState(() => ({ loaded: true }));
};
render() {
let {
className,
loadedClassName,
loadingClassName,
alt, alt,
src, src,
width, width,
height height
} = this.props; }) => {
const [loaded, setLoaded] = useState(false);
className = `${className} ${ const fullClassName = `${className} ${
this.state.loaded ? loadedClassName : loadingClassName loaded ? loadedClassName : loadingClassName
}`; }`;
return ( return (
<LazyLoad <LazyLoad
debounce={false} debounce={false}
@ -55,11 +35,14 @@ class ImageLoader extends React.Component {
width={width} width={width}
> >
{/* eslint-disable jsx-a11y/no-noninteractive-element-interactions */} {/* eslint-disable jsx-a11y/no-noninteractive-element-interactions */}
<img alt={alt} className={className} onLoad={this.onLoad} src={src} /> <img
{/* eslint-enable jsx-a11y/no-noninteractive-element-interactions */} alt={alt}
className={fullClassName}
onLoad={() => setLoaded(true)}
src={src}
/>
</LazyLoad> </LazyLoad>
); );
} };
}
ImageLoader.propTypes = propTypes; ImageLoader.propTypes = propTypes;
export default ImageLoader; export default ImageLoader;