我们一起踩过的坑----react(antd)(二)

1.antd Upload默认值问题

需求是这样的,后台若没有图片默认值,则只有上传按钮,且只能上传一张图片;若有默认值,则显示默认头像图片, 可删除,删除之后有且只能添加一张图片,没有删除默认图片时不能添加图片

坑爹之路漫漫-----

 

图为无默认值时状态

图为有默认值状态,删除后可添加图片

首先设置defaultFileList,但是defaultFileList并不会默认添加到fileList里面

< Upload
accept = "image/*"
listType = 'picture'
onRemove = {this . onRemove }
beforeUpload = {this . beforeUpload }
defaultFileList = { fieldValue ?[{
uid: "-1" ,
name: ` ${ item . fieldName } .png` ,
status: 'done' ,
url: `/file-server/ ${ fieldValue } ` ,
}]: "" }
onChange = {this . handleChange }
>
< Button
style = { { width: 220 } }
disabled = {this . state . fileList . length === 1 ? true : false}
onMouseEnter = { ( e ) =>this . handleUpload ( fieldValue , e ) }
>
< Icon type = "upload" /> 点击上传
</ Button >
</ Upload >

猜你喜欢

转载自www.cnblogs.com/NatChen/p/10192001.html