import { Component } from 'react'; import { observer } from 'mobx-react'; import { Field } from 'mobx-react-form'; import classnames from 'classnames'; import Dropzone, { DropzoneRef } from 'react-dropzone'; import { isWindows } from '../../environment'; type Props = { field: typeof Field; className: string; multiple: boolean; textDelete: string; textUpload: string; }; @observer class ImageUpload extends Component { static defaultProps = { multiple: false, }; state = { path: null, }; dropzoneRef: DropzoneRef | null = null; onDrop(acceptedFiles) { const { field } = this.props; for (const file of acceptedFiles) { const imgPath = isWindows ? file.path.replace(/\\/g, '/') : file.path; this.setState({ path: imgPath, }); this.props.field.onDrop(file); } field.set(''); } render() { const { field, className, multiple, textDelete, textUpload } = this.props; const cssClasses = classnames({ 'image-upload__dropzone': true, [`${className}`]: className, }); return (
{(field.value && field.value !== 'delete') || this.state.path ? ( <>
) : ( { this.dropzoneRef = node; }} onDrop={this.onDrop.bind(this)} multiple={multiple} accept="image/jpeg, image/png, image/svg+xml" > {({ getRootProps, getInputProps }) => (

{textUpload}

)}
)}
); } } export default ImageUpload;