Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

当Form组件设置为disabled时,FormItem中的Upload仍能上传文件 #36554

Closed
simonNeo opened this issue Jul 16, 2022 · 3 comments
Closed
Labels
help wanted The suggestion or request has been accepted, we need you to help us by sending a pull request.

Comments

@simonNeo
Copy link

Reproduction link

Edit on CodeSandbox

Steps to reproduce

  1. 添加一个Form组件,设置为disabled
  2. 添加一个Upload组件(在FormItem内),listType设置为picture-card
  3. 点击该自定义元素
import { Upload, Form } from "antd";
import { UploadOutlined } from "@ant-design/icons";

export default function App() {
  const uploadProps = {
    name: "file",
    listType: "picture-card",
    action: "https://www.mocky.io/v2/5cc8019d300000980a055e76"
  };
  return (
    <div className="App">
      <Form disabled>
        <Form.Item>
          <Upload {...uploadProps}>
            <UploadOutlined />
          </Upload>
        </Form.Item>
      </Form>
    </div>
  );
}

What is expected?

点击后不响应点击事件

What is actually happening?

点击后唤起了系统文件选择,并且选择文件后仍能触发上传。

Environment Info
antd 4.21.4
React 17.0.2
System Mac 12.4
Browser 103.0.5060.53(正式版本) (arm64)

不知道是不是因为标题的原因,发布了之后没有回复,重新发一遍。如果重复还请关闭。

场景: 业务数据状态是动态的,当数据为只读时不允许更新表单里面的数据。

一些想法:手动将Upload设置为disabled可以实现不允许选择文件,但是无法获取表单当前是否禁用。也尝试了用ant Button来包裹上传图标,但是listType为picture-card时,children基本无法填充满整个区域,点card外圈仍能触发文件选择和上传。

@Wxh16144
Copy link
Member

Wxh16144 commented Jul 17, 2022

借楼回复一下我的观点,估计是因为周末的原因。维护人员没注意 issue。

至于你说的无法获取表单禁用状态,大概看了一下官方其他 antd 组件都是通过 context 获取禁用的。

我写了一个 codesandbox demo,不知道是否符合你的需求。

import { Upload } from "antd";
import DisabledContext from "antd/es/config-provider/DisabledContext";
import { UploadOutlined } from "@ant-design/icons";

const CustomUpload = (props) => {
  const disabled = React.useContext(DisabledContext);

  return (
    <Upload disabled={disabled} {...props}>
      <UploadOutlined />
    </Upload>
  );
};

使用

import { Form } from "antd";
import CustomUpload from "./CustomUpload";

export default function App() {
  const uploadProps = {
    name: "file",
    listType: "picture-card",
    action: "https://www.mocky.io/v2/5cc8019d300000980a055e76"
  };

  return (
    <div className="App">
      <Form disabled>
        <Form.Item label="custom_upload">
          <CustomUpload {...uploadProps} />
        </Form.Item>
      </Form>
    </div>
  );
}

@MadCcc MadCcc added the help wanted The suggestion or request has been accepted, we need you to help us by sending a pull request. label Jul 18, 2022
@github-actions
Copy link
Contributor

Hello @simonNeo. We totally like your proposal/feedback, welcome to send us a Pull Request for it. Please send your Pull Request to proper branch (feature branch for the new feature, master for bugfix and other changes), fill the Pull Request Template here, provide changelog/TypeScript/documentation/test cases if needed and make sure CI passed, we will review it soon. We appreciate your effort in advance and looking forward to your contribution!

你好 @simonNeo,我们完全同意你的提议/反馈,欢迎直接在此仓库 创建一个 Pull Request 来解决这个问题。请将 Pull Request 发到正确的分支(新特性发到 feature 分支,其他发到 master 分支),务必填写 Pull Request 内的预设模板,提供改动所需相应的 changelog、TypeScript 定义、测试用例、文档等,并确保 CI 通过,我们会尽快进行 Review,提前感谢和期待您的贡献。

giphy

@simonNeo
Copy link
Author

@Wxh16144 谢谢回复,最近业务比较忙,不然也不会当伸手党LOL。我看已经有大佬修复这个问题了,也谢谢官方大佬。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted The suggestion or request has been accepted, we need you to help us by sending a pull request.
Projects
None yet
Development

No branches or pull requests

3 participants