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

FormItem required 为 undefined 时会使 必选样式 丢失 #41621

Closed
liixing opened this issue Apr 4, 2023 · 9 comments · Fixed by #41623
Closed

FormItem required 为 undefined 时会使 必选样式 丢失 #41621

liixing opened this issue Apr 4, 2023 · 9 comments · Fixed by #41623

Comments

@liixing
Copy link

liixing commented Apr 4, 2023

Reproduction link

https://stackblitz.com/edit/react-ts-kxpvis?file=App.tsx

Steps to reproduce

FormItem required 为 undefined 时会使 必选样式 丢失

What is expected?

rules 里面有 required 后,required 传 undefined 时不会影响必选样式

What is actually happening?

FormItem required 为 undefined 时会使 rules 里的必选样式丢失

Environment Info
antd 5.4.0
React 18.1.0
System macos Ventura version 13.2.1
Browser chrome Version 111.0.5563.146 (Official Build) (x86_64)
@Wxh16144
Copy link
Member

Wxh16144 commented Apr 4, 2023

应该是漏掉了某些 props 导致的内部判断有些问题。你可以先这样使用。

image

interface MyFormInputProps extends FormItemProps {
  inputProps: InputProps;
}

function MyFormInput(props: MyFormInputProps) {
  const { inputProps, ...formProps } = props;
  return (
    <Form.Item {...formProps}>
      <Input {...inputProps} />
    </Form.Item>
  );
}

@liixing
Copy link
Author

liixing commented Apr 4, 2023

@Wxh16144 不是漏传,是 required 传了 undefined 后就会丢失必选样式

@liixing liixing changed the title FormItem 封装以后必选样式丢失 FormItem required 为 undefined 时会使 rules 里的 required 失效 Apr 4, 2023
@Yuiai01
Copy link
Contributor

Yuiai01 commented Apr 4, 2023

@liixing 是指 required 的样式丢失了吗?

@liixing liixing changed the title FormItem required 为 undefined 时会使 rules 里的 required 失效 FormItem required 为 undefined 时会使 必选样式 丢失 Apr 4, 2023
@liixing
Copy link
Author

liixing commented Apr 4, 2023

@Yuiai01 是的

@Wxh16144

This comment was marked as off-topic.

@Yuiai01
Copy link
Contributor

Yuiai01 commented Apr 4, 2023

不是漏传,是 required 传了 undefined 后就会变成可选

感谢你的指出,根据你提供的 demo, 我找到了问题。参数透传下去有些默认的值会被覆盖。这里还需要处理一下 formItemProps,如下所示

 Object.keys(formItemProps).forEach((propName) => {
    if (typeof formItemProps[propName] === 'undefined') {
      delete formItemProps[propName];
    }
  });

应该是这里被覆盖了
image

@zombieJ
Copy link
Member

zombieJ commented Apr 4, 2023

嗯,见上。这相当于自己覆盖了一个 required

@zombieJ zombieJ closed this as completed Apr 4, 2023
@liixing
Copy link
Author

liixing commented Apr 4, 2023

@zombieJ 这感觉不妥吧, isRequred 判断了 required 为 undefined 情况但是下面却被覆盖了,感觉这应该 merge 一下

@Wxh16144
Copy link
Member

Wxh16144 commented Apr 4, 2023

感觉不妥吧, isRequred 判断了 required 为 undefined 情况但是下面却被覆盖了,感觉这应该 merge 一下

我和你看法一致,我这边已经在做了,带会儿提交 PR 看看用例通过情况。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants