import React from 'react'
import { Upload, Button, Icon, Form } from 'antd';
const FormItem = Form.Item
class MyUpload extends React.Component {
state = {
fileList: [{
uid: -1,
name: 'xxx.png',
status: 'done',
url: 'http://www.baidu.com/xxx.png',
}],
}
handleChange = (info) => {
let { file, fileList } = info;
fileList = fileList.slice(-2);
fileList = fileList.map((file) => {
if (file.response) {
file.url = file.response.url;
}
return file;
});
fileList = fileList.filter((file) => {
if (file.response) {
return file.response.status === 'success';
}
return true;
});
this.setState({ fileList });
}
render() {
const { getFieldDecorator } = this.props.form
const { fileList } = this.state
const uploadProps = {
action: '//jsonplaceholder.typicode.com/posts/',
listType: "picture",
multiple: true,
onRemove: () => {
if (canDelete) {
message.info('不可删除')
return false
}
return true
}
};
return (
<Form>
<FormItem>
{
getFieldDecorator('upload', {
rules: [{ required: true, message: 'upload file please!' }],
valuePropName: 'fileList', //子节点的值的属性,默认‘value’
getValueFromEvent: handleChange, //可以把 onChange 的参数转化为控件的值
initialValue: fileList
})(
<Upload {...uploadProps}>
<Button>
<Icon type="upload" /> upload
</Button>
</Upload>
)
}
</FormItem>
</Form>
);
}
}
export default Form.create()(MyUpload);