react antd的form表单与upload一起使用时,label点击会触发上传事件

问题

antd的form表单与upload一起使用时,应该是点击上传按钮才会触发上传事件,但是实际上是点击label也会触发

代码如下:

jsx 复制代码
 <Form layout={"vertical"}>
      <Form.Item label={证件照}>
          {getFieldDecorator("attachments", {
              rules: [{ required: true, message: "请上传证件照" }],
                })(
                  <Upload
                    customRequest={() => { }}
                    fileList={fileList}
                    accept=".jpg, .jpeg, .png, .PNG, .JPG, .JPEG"
                    beforeUpload={(file) => this.beforeUpload(file)}
                    onChange={(file) => this.handleFileUploadChange(file)}
                    onRemove={(file) => this.handleFileUploadRemove(file)}
                  >
                    <Button disabled={fileList.length ? true : false}>
                      <Icon type="upload" />
                      点击上传
                    </Button>
                 </Upload>
           )}
      </Form.Item>
</Form>

原因:

在React Ant Design中,当使用Form组件和Upload组件时,点击label标签会触发上传事件的原因是,Upload组件内部使用了一个隐藏的input元素来处理文件选择。而label标签的for属性与input元素的id属性相对应,当点击label标签时,会触发对应id的input元素的点击事件,从而触发上传事件。

解决方法:

方法一

可以通过给label标签添加htmlFor属性,将其与对应的input元素的id属性进行绑定,从而避免点击label标签触发上传事件。

jsx 复制代码
 <Form layout={"vertical"}>
      <Form.Item htmlFor="upload-item" label={证件照}>
          {getFieldDecorator("attachments", {
              rules: [{ required: true, message: "请上传证件照" }],
                })(
                  <Upload
                    customRequest={() => { }}
                    fileList={fileList}
                    accept=".jpg, .jpeg, .png, .PNG, .JPG, .JPEG"
                    beforeUpload={(file) => this.beforeUpload(file)}
                    onChange={(file) => this.handleFileUploadChange(file)}
                    onRemove={(file) => this.handleFileUploadRemove(file)}
                  >
                    <Button disabled={fileList.length ? true : false}>
                      <Icon type="upload" />
                      点击上传
                    </Button>
                 </Upload>
           )}
      </Form.Item>
</Form>

方法二

再套一层<Form.Item>

jsx 复制代码
 <Form layout={"vertical"}>
      <Form.Item label={证件照}>
          <Form.Item>
              {getFieldDecorator("attachments", {
                  rules: [{ required: true, message: "请上传证件照" }],
                    })(
                      <Upload
                        customRequest={() => { }}
                        fileList={fileList}
                        accept=".jpg, .jpeg, .png, .PNG, .JPG, .JPEG"
                        beforeUpload={(file) => this.beforeUpload(file)}
                        onChange={(file) => this.handleFileUploadChange(file)}
                        onRemove={(file) => this.handleFileUploadRemove(file)}
                      >
                        <Button disabled={fileList.length ? true : false}>
                          <Icon type="upload" />
                          点击上传
                        </Button>
                     </Upload>
               )}
           </Form.Item>
      </Form.Item>
</Form>
相关推荐
hunterandroid8 小时前
[鸿蒙从零到一] HarmonyOS 任务调度与并发模型实战:taskpool、Worker 与可取消任务
前端
颜进强8 小时前
前端看后端 14:什么是 CORS?
前端·后端
sugar__salt9 小时前
Vue3 自定义指令与插槽(Slot)技术详解
前端·javascript·vue.js·前端框架·vue
Csvn9 小时前
🎯 Flex 布局的 `min-width: auto` 陷阱:为什么内容总是撑破容器?
前端
果然_9 小时前
纯前端图片压缩怎么做?不用上传服务器,浏览器里跑完所有逻辑
前端
Csvn9 小时前
🖼️ OffscreenCanvas:把 Canvas 绘制搬出主线程,动画卡顿的终极解药
前端
用户69371750013849 小时前
了解一下 Agent Harness
android·前端·后端
swipe9 小时前
16|(前端转全栈)前端人排查后端问题:curl、traceId、日志、MySQL、Redis 怎么用?
前端·后端·面试
anyup9 小时前
uni-app 没有根组件?仅需几行代码实现全局 Toast 和 Modal
前端·架构·uni-app