import React, { Component } from 'react';
import { Upload, Button } from 'antd';
import { UploadOutlined } from '@ant-design/icons';
import { isArray } from 'lodash';
export default class index extends Component {
constructor(props) {
super(props);
this.state = {
file: '',
};
}
get progress() {
return {
strokeColor: {
'0%': globalCSS.primaryColor,
'100%': globalCSS.successColor,
},
strokeWidth: 3,
format: (percent) => `${parseFloat(percent.toFixed(2))}%`,
};
}
onChange = (file) => {
this.setState(
{
file,
},
() => {
const { onChange } = this.props;
if (onChange) {
onChange(file);
}
}
);
};
handleChange = (info) => {
const { file, fileList = [] } = info;
const { status } = file || {};
if (status === 'removed' && fileList.length === 0) {
this.onChange(null);
}
if (!status) {
this.onChange(file);
}
if (info.file.status !== 'uploading') {
console.log(file, fileList);
}
};
beforeUpload = () => {
return false;
};
render() {
const { value } = this.props;
const { file } = this.state;
let fileList;
if (value) {
fileList = isArray(value) ? value : [value];
} else {
fileList = file ? [file] : [];
}
const props = {
...this.props,
name: 'file',
action: '',
headers: {
authorization: 'authorization-text',
},
onChange: this.handleChange,
progress: this.progress,
beforeUpload: this.beforeUpload,
fileList,
};
return (
<Upload {...props}>
{this.props.children || (
<Button>
<UploadOutlined /> {t('Click to Upload')}
</Button>
)}
</Upload>
);
}
}