2018-02-08 15:50:12 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* ImgPreview
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-02-12 15:12:58 +01:00
|
|
|
import { cloneDeep, get, isEmpty, isObject, size } from 'lodash';
|
2018-02-08 15:50:12 +01:00
|
|
|
import cn from 'classnames';
|
|
|
|
|
|
|
|
import styles from './styles.scss';
|
|
|
|
|
2018-02-12 11:23:02 +01:00
|
|
|
class ImgPreview extends React.Component {
|
2018-02-12 18:20:43 +01:00
|
|
|
state = { imgURL: '', position: 0, isImg: false };
|
2018-02-12 11:23:02 +01:00
|
|
|
|
|
|
|
componentDidMount() {
|
2018-02-12 15:12:58 +01:00
|
|
|
// We don't need the generateImgURL function here since the compo will
|
|
|
|
// always have an init value here
|
2018-02-12 11:23:02 +01:00
|
|
|
this.setState({
|
2018-02-12 15:12:58 +01:00
|
|
|
imgURL: get(this.props.files, ['0', 'url'], ''),
|
2018-02-12 18:46:45 +01:00
|
|
|
isImg: this.isPictureType(get(this.props.files, ['0', 'name'], '')),
|
2018-02-12 11:23:02 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
if (nextProps.isUploading !== this.props.isUploading) {
|
|
|
|
const lastFile = nextProps.files.slice(-1)[0];
|
|
|
|
this.generateImgURL(lastFile);
|
2018-02-12 18:20:43 +01:00
|
|
|
this.updateFilePosition(nextProps.files.length - 1);
|
2018-02-12 11:23:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-12 18:46:45 +01:00
|
|
|
getFileType = (fileName) => fileName.split('.').slice(-1)[0];
|
|
|
|
|
2018-02-12 11:23:02 +01:00
|
|
|
/**
|
|
|
|
* [generateImgURL description]
|
|
|
|
* @param {FileList} files
|
|
|
|
* @return {URL}
|
|
|
|
*/
|
|
|
|
generateImgURL = (file) => {
|
2018-02-12 18:46:45 +01:00
|
|
|
if ( /\.(jpe?g|png|gif)$/i.test(file.name) ) {
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.onloadend = () => {
|
|
|
|
this.setState({
|
|
|
|
imgURL: reader.result,
|
|
|
|
isImg: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
reader.readAsDataURL(file);
|
|
|
|
} else {
|
|
|
|
this.setState({ isImg: false, imgURL: file.name });
|
2018-02-12 11:23:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-12 15:12:58 +01:00
|
|
|
handleClick = (type) => {
|
2018-02-12 11:23:02 +01:00
|
|
|
const { position } = this.state;
|
|
|
|
const { files } = this.props;
|
|
|
|
let file;
|
|
|
|
let nextPosition;
|
|
|
|
|
2018-02-12 15:12:58 +01:00
|
|
|
switch (type) {
|
|
|
|
case 'right':
|
2018-02-12 11:23:02 +01:00
|
|
|
file = files[position + 1] || files[0];
|
|
|
|
nextPosition = files[position + 1] ? position + 1 : 0;
|
|
|
|
break;
|
2018-02-12 15:12:58 +01:00
|
|
|
case 'left':
|
2018-02-12 11:23:02 +01:00
|
|
|
file = files[position - 1] || files[files.length - 1];
|
|
|
|
nextPosition = files[position - 1] ? position - 1 : files.length - 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file.url) {
|
|
|
|
this.generateImgURL(file)
|
|
|
|
} else {
|
|
|
|
this.setState({ imgURL: file.url });
|
|
|
|
}
|
|
|
|
|
2018-02-12 18:20:43 +01:00
|
|
|
this.updateFilePosition(nextPosition);
|
2018-02-12 11:23:02 +01:00
|
|
|
}
|
|
|
|
|
2018-02-12 18:46:45 +01:00
|
|
|
isPictureType = (fileName) => /\.(jpe?g|png|gif)$/i.test(fileName);
|
|
|
|
|
2018-02-12 15:12:58 +01:00
|
|
|
removeFile = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
const value = cloneDeep(this.props.files);
|
|
|
|
value.splice(this.state.position, 1);
|
|
|
|
|
|
|
|
const target = {
|
|
|
|
name: this.props.name,
|
|
|
|
type: 'file',
|
|
|
|
value,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.props.onChange({ target });
|
2018-02-12 18:20:43 +01:00
|
|
|
|
|
|
|
if (size(value) === 0) {
|
|
|
|
this.setState({ position: 0, imgURL: '' });
|
|
|
|
return this.props.updateFilePosition(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display the last file
|
|
|
|
const nextFile = value.slice(-1)[0];
|
|
|
|
const nextPosition = value.length -1;
|
|
|
|
|
|
|
|
this.updateFilePosition(nextPosition);
|
|
|
|
|
|
|
|
if (!nextFile.url) {
|
|
|
|
this.generateImgURL(nextFile);
|
|
|
|
} else {
|
|
|
|
this.setState({ imgURL: nextFile.url });
|
|
|
|
}
|
2018-02-12 15:12:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
renderArrow = (type = 'left') => (
|
|
|
|
<div
|
|
|
|
className={cn(
|
|
|
|
styles.arrowContainer,
|
|
|
|
type === 'left' && styles.arrowLeft,
|
|
|
|
type !== 'left' && styles.arrowRight,
|
|
|
|
)}
|
|
|
|
onClick={(e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
this.handleClick(type);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
|
|
|
|
renderIconRemove = () => (
|
|
|
|
<div className={styles.iconContainer} onClick={this.removeFile}>
|
|
|
|
<i className="fa fa-times" />
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
|
2018-02-12 18:20:43 +01:00
|
|
|
updateFilePosition = (newPosition) => {
|
|
|
|
this.setState({ position: newPosition });
|
|
|
|
this.props.updateFilePosition(newPosition);
|
|
|
|
}
|
|
|
|
|
2018-02-12 11:23:02 +01:00
|
|
|
render() {
|
2018-02-12 15:12:58 +01:00
|
|
|
const { files, multiple } = this.props;
|
2018-02-12 18:46:45 +01:00
|
|
|
|
2018-02-12 11:23:02 +01:00
|
|
|
return (
|
2018-02-12 15:12:58 +01:00
|
|
|
<div className={styles.imgPreviewContainer}>
|
2018-02-12 18:20:43 +01:00
|
|
|
{ !isEmpty(files) && this.renderIconRemove() }
|
2018-02-12 18:46:45 +01:00
|
|
|
{ !isEmpty(this.state.imgURL) && this.state.isImg && <img src={this.state.imgURL} /> }
|
2018-02-12 18:20:43 +01:00
|
|
|
{ multiple && size(files) > 1 && this.renderArrow('right') }
|
|
|
|
{ multiple && size(files) > 1 && this.renderArrow('left') }
|
2018-02-12 11:23:02 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2018-02-08 15:50:12 +01:00
|
|
|
}
|
|
|
|
|
2018-02-12 11:23:02 +01:00
|
|
|
ImgPreview.defaultProps = {
|
|
|
|
files: [{}],
|
|
|
|
isUploading: false,
|
2018-02-12 15:12:58 +01:00
|
|
|
multiple: false,
|
|
|
|
name: '',
|
|
|
|
onChange: () => {},
|
2018-02-12 18:20:43 +01:00
|
|
|
updateFilePosition: () => {},
|
2018-02-12 11:23:02 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
ImgPreview.propTypes = {
|
|
|
|
files: PropTypes.array,
|
|
|
|
isUploading: PropTypes.bool,
|
2018-02-12 15:12:58 +01:00
|
|
|
multiple: PropTypes.bool,
|
|
|
|
name: PropTypes.string,
|
|
|
|
onChange: PropTypes.func,
|
2018-02-12 18:20:43 +01:00
|
|
|
updateFilePosition: PropTypes.func,
|
2018-02-12 11:23:02 +01:00
|
|
|
};
|
2018-02-08 15:50:12 +01:00
|
|
|
|
|
|
|
export default ImgPreview;
|